/** * @Draw.java * * Utgår från ip4.2.1 * Förändringar har gjorts för att passa worm spelet. * * @version 1.0 06/10/01 * * */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import java.awt.image.BufferedImage; public class Draw extends JFrame { private Paper p; public Communicator myCommunicator; private Thread commThread; private ClientUI myUI; //Konstruktor, gör inget märkvärdigt public Draw(int starter, String remHost, ClientUI aUI) { myUI = aUI; this.setResizable(false); this.setLocation(new Point(300, 300)); p = new Paper(myUI); setDefaultCloseOperation(DISPOSE_ON_CLOSE); setSize(400, 400); p.setSize(400,400); setVisible(true); // Stäng alla sockets vid avstängning, gös i slutet av runmetoderna // vi sätter run till false, så kommer sockets att stängas this.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(WindowEvent winEvt) { myCommunicator.setRun(false); myUI.playing = false; dispose(); } }); int aport = 4321; int remotePort = 4321; myCommunicator = new Communicator(aport, p, remHost, remotePort,starter,commThread,this); p.setCommunicator(myCommunicator); commThread = new Thread(myCommunicator); commThread.start(); p.setBounds(0,0,400,400); getContentPane().add(p); p.setFocusable(true); } //Sätter playing i myUI till abool public void setPlay(boolean abool){ myUI.playing = abool; } } class Paper extends JPanel { private HashSet hs; private HashSet hs2; private Communicator myCommunicator; private ClientUI myUI; private boolean doOnce=true; //Graphics myGraph=null; BufferedImage offscreen=null; public Paper(ClientUI aUI) { hs = new HashSet(); hs2 = new HashSet(); myUI = aUI; setBackground(Color.white); this.addKeyListener(new KeyMon()); this.setFocusable (true); } public void setCommunicator(Communicator c){ myCommunicator = c; } //Sätter playing i myUI till abool public void setPlay(boolean abool){ myUI.playing = abool; } //MÅLA MASKEN public void paintComponent(Graphics g) { if(offscreen==null) initOffscreen(); super.paintComponent(g); g.drawImage(offscreen, 0, 0, this); g.setColor(Color.black); Iterator i = hs.iterator(); Iterator i2 = hs2.iterator(); try{ while(i.hasNext()) { Point p = (Point)i.next(); g.fillOval(p.x, p.y, 2, 2); } while(i2.hasNext()) { Point pp = (Point)i2.next(); g.fillOval(pp.x, pp.y, 2, 2); } }catch(ConcurrentModificationException cEX){} } //Initierar offscreenbuffer image private void initOffscreen() { int w = getWidth(); int h = getHeight(); offscreen = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = offscreen.createGraphics(); g2.setColor(UIManager.getColor("Panel.background")); g2.fillRect(0,0,w,h); g2.dispose(); } //Uppdateratr offscreenbuffer private void updateOffscreen(Point p) { Graphics g = offscreen.getGraphics(); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setPaint(new Color(240,200,240)); g2.fillOval((int)p.getX(), (int)p.getY(), 2, 2); g2.dispose(); } /* * POints för att rita... * */ private void addPoint(Point p) { drawPoint(p); //myCommunicator.packAndsend(p); } /* * Skickar directionändring till ansluten fiende. * Ändrar också för oss, så vi vet. */ public void setDir(Direction d){ myCommunicator.myGame.setMyDir(d); myCommunicator.packAndsend(d); } /* * Metod som ritar din orm. */ public void drawPoint(Point p) { if(offscreen==null) initOffscreen(); updateOffscreen(p); hs.add(p); //repaint(); } /* * Metod som ritar fiendens orm. * * Endast denna, den sist anoropade, som anropar repaint. */ public void drawEnemyPoint(Point p) { if(!this.hasFocus()){ requestFocus(); } if(offscreen==null) initOffscreen(); updateOffscreen(p); hs2.add(p); repaint(); } class KeyMon extends KeyAdapter{ public void keyReleased(KeyEvent e){ Direction aDir=null; //System.out.println(e.getKeyCode()); if(e.getKeyCode() == KeyEvent.VK_DOWN ){ //System.out.println("DOWN"); aDir = new Direction(1); } else if(e.getKeyCode() == KeyEvent.VK_UP ){ aDir = new Direction(2); //System.out.println("UP"); }else if(e.getKeyCode() == KeyEvent.VK_LEFT ){ aDir = new Direction(3); //System.out.println("LEFT"); }else if(e.getKeyCode() == KeyEvent.VK_RIGHT ){ aDir = new Direction(4); //System.out.println("RIGHT"); } if(aDir!=null){ setDir(aDir); //myCommunicator.packAndsend(aDir); } } } }