/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * SpelApplet.java * * Created on 2009-feb-17, 09:27:49 */ package mittforstaspel; import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.Random; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author karlssoj */ public class SpelApplet extends Applet implements Runnable, MouseListener { //GLOBALA VARIABLER, SÄTT IN I BÖRJAN AV APPLET-KLASSEN Image dbImage; //deklarerar en Image variabel dit en osynlig kopia av applet-grafiken sparas Graphics dbg; //deklarerar Graphics variabel dit alla grafik-egenskaper för appleten sparas /** Initializes the applet appletKlass */ @Override public void init() { try { java.awt.EventQueue.invokeAndWait(new Runnable() { public void run() { //initComponents(); } }); } catch (Exception ex) { ex.printStackTrace(); } setBackground(Color.CYAN); Boll.tr = Executors.newCachedThreadPool(); Lista.forsta = new Lista(); Lista nuv = Lista.forsta; Boll.applet = this; for(int i = 0; i < 6; i++, nuv = nuv.nasta) { Lista foljande = new Lista(); nuv.b = new Boll(); nuv.nasta = foljande; } nuv.nasta = null; Boll.tr.submit(this); addMouseListener(this); } //Denna metod anropas av repaint( @Override public void update (Graphics g) { //Initialiserar den osynliga kopian av grafiken if (dbImage == null) { //skapar en tom "image" med samma storlek som innevarande applet dbImage = createImage(this.getSize().width, this.getSize().height); //hämtar grafikegenskaper dbg = dbImage.getGraphics(); } //Tömmer skärmen i bakgrunden dbg.setColor (getBackground()); dbg.fillRect (0, 0, this.getSize().width, this.getSize().height); //Ritar grafiken pånytt i bakgrunden dbg.setColor(getForeground()); paint(dbg); //Ritar den uppdaterade bilden på bildskärmen g.drawImage(dbImage, 0, 0, this); } @Override public void paint(Graphics grafik) { //grafikfunktioner hit for(Lista nuv = Lista.forsta; nuv.nasta != null; nuv = nuv.nasta) { grafik.setColor(nuv.b.farg); grafik.fillOval(nuv.b.x, nuv.b.y, nuv.b.diameter, nuv.b.diameter); } grafik.setColor(Color.BLACK); grafik.drawString(Integer.toString(Boll.antal), 10, 10); } public void run() { while(true) { try { repaint(); Thread.sleep(Boll.vanteTid); } catch (InterruptedException ex) { //Logger.getLogger(SpelApplet.class.getName()).log(Level.SEVERE, null, ex); } } } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { Lista foregaende = Lista.forsta; for(Lista nuv = Lista.forsta; nuv.nasta != null; nuv = nuv.nasta) { if( (e.getPoint().x - nuv.b.x) <= nuv.b.diameter && (e.getPoint().y - nuv.b.y) <= nuv.b.diameter) { if( (e.getPoint().x - nuv.b.x) >= 0 && (e.getPoint().y - nuv.b.y) >= 0) { nuv.b.stat.cancel(true); foregaende.nasta = nuv.nasta; Boll.antal--; if(nuv == Lista.forsta) Lista.forsta = nuv.nasta; nuv = null; } } foregaende = nuv; } } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } /** This method is called from within the init() method to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ /* @SuppressWarnings("unchecked") // private void initComponents() { org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 400, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 300, Short.MAX_VALUE) ); }// */ // Variables declaration - do not modify // End of variables declaration } class Lista { Boll b; Lista nasta; static Lista forsta; } class Boll implements Runnable { int x, y, diameter, xrikt, yrikt, xy[][] = new int[4][2], id = 0; static int vanteTid, antal = 0; Future stat; Color farg; static Applet applet; static ExecutorService tr; Random generator = new Random(); public Boll() { id = antal; antal++; do { xrikt = generator.nextInt(4) - 2; yrikt = generator.nextInt(4) - 2; } while(xrikt == 0 && yrikt == 0); vanteTid = 10; diameter = generator.nextInt(30) + 30; do { x = generator.nextInt(applet.getWidth() - diameter); y = generator.nextInt(applet.getWidth() - diameter); } while(krocktest(true)); farg = new Color(generator.nextInt(256),generator.nextInt(256),generator.nextInt(256)); stat = tr.submit(this); } public int diff(int a, int b) { if(a > b) return a - b; else return b - a; } public void move() { if(x > applet.getWidth() - diameter) {x = applet.getWidth() - diameter; xrikt*=-1;} if(x < 0) {x = 0; xrikt*=-1;} if(y > applet.getHeight() - diameter) {y = applet.getHeight() - diameter; yrikt*=-1;} if(y < 0) {y = 0; yrikt*=-1;} x+=xrikt; y+=yrikt; } public boolean krocktest(boolean forsta) { for(Lista nuv = Lista.forsta; nuv.nasta != null; nuv = nuv.nasta) { int mittpX1 = nuv.b.x + nuv.b.diameter/2; int mittpY1 = nuv.b.y + nuv.b.diameter/2; int mittpX2 = x + diameter/2; int mittpY2 = y + diameter/2; int minAvst = nuv.b.diameter/2 + diameter/2; if(diff(mittpX1, mittpX2) <= minAvst && diff(mittpY1, mittpY2) <= minAvst && this != nuv.b) { if(antal < 20 && !forsta) { Lista nyForsta = new Lista(); nyForsta.b = new Boll(); nyForsta.nasta = Lista.forsta; Lista.forsta = nyForsta; } xrikt *= -1; yrikt *= -1; return true; } } return false; } public void run() { while(true) { move(); krocktest(false); try { Thread.sleep(vanteTid); } catch (InterruptedException ex) { //Logger.getLogger(Boll.class.getName()).log(Level.SEVERE, null, ex); break; } } } }