|
| 1 | +import java.awt.BorderLayout; |
| 2 | +import java.awt.Image; |
| 3 | +import java.awt.datatransfer.DataFlavor; |
| 4 | +import java.awt.datatransfer.Transferable; |
| 5 | +import java.awt.datatransfer.UnsupportedFlavorException; |
| 6 | +import java.awt.event.ActionEvent; |
| 7 | +import java.awt.image.BufferedImage; |
| 8 | +import java.io.BufferedReader; |
| 9 | +import java.io.IOException; |
| 10 | +import java.io.InputStreamReader; |
| 11 | +import java.net.ServerSocket; |
| 12 | +import java.net.Socket; |
| 13 | +import java.net.URL; |
| 14 | +import javax.imageio.ImageIO; |
| 15 | +import javax.swing.*; |
| 16 | + |
| 17 | +public class CardWindow extends JFrame { |
| 18 | + private static final long serialVersionUID = 1L; |
| 19 | + private static volatile JLabel label; |
| 20 | + ImageIcon defaultIcon; |
| 21 | + |
| 22 | + public CardWindow() throws Exception { |
| 23 | + super("Card Display"); |
| 24 | + this.setLayout(new BorderLayout()); |
| 25 | + URL url = new URL("http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=-1&type=card"); |
| 26 | + BufferedImage img = ImageIO.read(url); |
| 27 | + ImageIcon defaultIcon = new ImageIcon(img); |
| 28 | + label = new JLabel(defaultIcon); |
| 29 | + JButton button = new JButton(new AbstractAction("Clear") { |
| 30 | + private static final long serialVersionUID = 1L; |
| 31 | + |
| 32 | + public void actionPerformed(ActionEvent e) { |
| 33 | + label.setIcon(defaultIcon); |
| 34 | + label.validate(); |
| 35 | + } |
| 36 | + }); |
| 37 | + this.add(label, BorderLayout.CENTER); |
| 38 | + this.add(button, BorderLayout.SOUTH); |
| 39 | + this.setResizable(false); |
| 40 | + this.pack(); |
| 41 | + this.setVisible(true); |
| 42 | + this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); |
| 43 | + if(!ImageUpdateThread.created){ |
| 44 | + new ImageUpdateThread(label).start(); |
| 45 | + }else{ |
| 46 | + ImageUpdateThread.setLabel(label); |
| 47 | + } |
| 48 | + label.setTransferHandler(createTransferHandler()); |
| 49 | + } |
| 50 | + |
| 51 | + public static class ImageUpdateThread extends Thread { |
| 52 | + private ServerSocket s; |
| 53 | + private static JLabel lab; |
| 54 | + public static boolean created = false; |
| 55 | + |
| 56 | + public ImageUpdateThread(JLabel l) { |
| 57 | + super(); |
| 58 | + lab = l; |
| 59 | + created = true; |
| 60 | + } |
| 61 | + |
| 62 | + public static void setLabel(JLabel l){ |
| 63 | + lab = l; |
| 64 | + } |
| 65 | + |
| 66 | + public void run() { |
| 67 | + try { |
| 68 | + s = new ServerSocket(7777); |
| 69 | + } catch (IOException e) { |
| 70 | + e.printStackTrace(); |
| 71 | + } |
| 72 | + |
| 73 | + while (true) { |
| 74 | + try { |
| 75 | + Socket sock = s.accept(); |
| 76 | + BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream())); |
| 77 | + |
| 78 | + for (;;) { |
| 79 | + String line = in.readLine(); |
| 80 | + if (line != null) { |
| 81 | + if (line.startsWith("GET ")) { |
| 82 | + String id = line.split(" ")[1].replace("/", ""); |
| 83 | + URL url = new URL("http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=" + id |
| 84 | + + "&type=card"); |
| 85 | + BufferedImage img = ImageIO.read(url); |
| 86 | + ImageIcon icon = new ImageIcon(img); |
| 87 | + lab.setIcon(icon); |
| 88 | + sock.close(); |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } catch (IOException e) { |
| 94 | + e.printStackTrace(); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private static TransferHandler createTransferHandler() { |
| 101 | + return new TransferHandler() { |
| 102 | + private static final long serialVersionUID = 1L; |
| 103 | + |
| 104 | + @Override |
| 105 | + public boolean importData(JComponent comp, Transferable aTransferable) { |
| 106 | + try { |
| 107 | + String st = (String) aTransferable.getTransferData(DataFlavor.stringFlavor); |
| 108 | + URL url = new URL(st); |
| 109 | + BufferedImage img = ImageIO.read(url); |
| 110 | + ImageIcon icon = new ImageIcon(img); |
| 111 | + label.setIcon(icon); |
| 112 | + label.validate(); |
| 113 | + } catch (UnsupportedFlavorException e) { |
| 114 | + |
| 115 | + try { |
| 116 | + Image i = (Image) aTransferable.getTransferData(DataFlavor.imageFlavor); |
| 117 | + label.setIcon(new ImageIcon(i)); |
| 118 | + label.validate(); |
| 119 | + } catch (UnsupportedFlavorException a) { |
| 120 | + } catch (IOException a) { |
| 121 | + } |
| 122 | + } catch (IOException e) { |
| 123 | + } |
| 124 | + return true; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) { |
| 129 | + return true; |
| 130 | + } |
| 131 | + }; |
| 132 | + } |
| 133 | + |
| 134 | + public static void main(String[] args) throws Exception { |
| 135 | + new CardWindow(); |
| 136 | + } |
| 137 | +} |
0 commit comments