/* * Zeichenmethode zum Zeichnen der Bälle, etc. */ @Override public void paint(Graphics g) { /* * FPS-Zäler (Ausgabe findet erst am Ende von <code>paint(Graphics g)</code>) */ if (frames == 1) { time = System.currentTimeMillis(); } if (System.currentTimeMillis() - time != 0) { fps = (int) ((1000 * frames) / ((System.currentTimeMillis() - time))); } if (frames > 1000) { frames = 0; } frames++; /* * Bild für Doublebuffering erstellen */ dbImage = createImage(phys.getSize().getX().intValue() + 2, phys.getSize().getY().intValue() + 2); dbGraphics = dbImage.getGraphics(); /* * <code>phys.tick()</code> gehört nicht in die <code>paint(Graphics g)</code>-Methode * in Draw-Panel, es ist jedoch nicht sehr sinvoll diesen Abschnitt auszulagern, * da man sonst die Synchronisation der Threads (diese werden von Swing (GUI-Bibliothek) * automatisch erstellt) selbst schreiben müsste. Dies geschieht durch den Aufruf in * <code>paint(Graphics g)</code> automatisch. */ if (this.isPaused() == false) { phys.tick(); } /* * Bälle zeichnen */ Stack<Ball> baelle = phys.getBaelle(); int anzBaelle = baelle.size(); for (int i = 0; i < anzBaelle; i++) { dbGraphics.setColor(baelle.elementAt(i).getColor()); int x = baelle.elementAt(i).getPosition().getX().intValue() - baelle.elementAt(i).getRadius().intValue(); int y = baelle.elementAt(i).getPosition().getY().intValue() - baelle.elementAt(i).getRadius().intValue(); int d = baelle.elementAt(i).getRadius().intValue() * 2; dbGraphics.drawOval(x, y, d, d); /* * Pfad des Balls zeichnen, falls aktiviert */ if (history == true) { Stack<Coordinate> hist = baelle.elementAt(i).getHistory(); int oldX = baelle.elementAt(i).getPosition().getX().intValue(); int oldY = baelle.elementAt(i).getPosition().getY().intValue(); for (int h = hist.size() - 1; h != 0; h--) { Color c = new Color( baelle.elementAt(i).getColor().getRed(), baelle.elementAt(i).getColor().getGreen(), baelle.elementAt(i).getColor().getBlue(), (255 * h / hist.size())); dbGraphics.setColor(c); dbGraphics.drawLine( oldX, oldY, hist.get(h).getX().intValue(), hist.get(h).getY().intValue()); oldX = hist.get(h).getX().intValue(); oldY = hist.get(h).getY().intValue(); } } /* * Richtungspfeil zeichnen, falls aktiviert */ if (directionArrow == true) { dbGraphics.setColor(baelle.elementAt(i).getColor()); drawArrow( (Graphics2D) dbGraphics, baelle.elementAt(i).getPosition().getX().intValue(), baelle.elementAt(i).getPosition().getY().intValue(), baelle.elementAt(i).getPosition().getX() + (baelle.elementAt(i).getSpeed().getX() * 50), baelle.elementAt(i).getPosition().getY() + (baelle.elementAt(i).getSpeed().getY() * 50)); } } /* * Erfassen der Mausposition und Zeichnen der Vorlage für den nächsten * zu erzeugenden Ball */ int x = newBall.getPosition().getX().intValue() - newBall.getRadius().intValue(); int y = newBall.getPosition().getY().intValue() - newBall.getRadius().intValue(); int d = newBall.getRadius().intValue() * 2; if (mousePressed == false) { dbGraphics.setColor(Color.red); } else { dbGraphics.setColor(Color.green); } dbGraphics.drawOval(x, y, d, d); if (mousePressed == true) { dbGraphics.drawLine( newBall.getPosition().getX().intValue(), newBall.getPosition().getY().intValue(), secondMouseCoordinate.getX().intValue(), secondMouseCoordinate.getY().intValue()); } dbGraphics.setColor(Color.black); dbGraphics.drawRect(0, 0, phys.getSize().getX().intValue(), phys.getSize().getY().intValue()); Point mousePos = getMousePosition(); if (mousePos != null && mousePressed == false) { newBall.setPosition(new Coordinate(new Double(mousePos.x), new Double(mousePos.y))); } if (mousePos != null && mousePressed == true) { secondMouseCoordinate = new Coordinate(new Double(mousePos.x), new Double(mousePos.y)); } /* * Ausgeben der FPS-Zahl */ dbGraphics.drawString("FPS: " + String.valueOf(fps), 10, 20); g.drawImage(dbImage, 0, 0, this); }