Hallo,
in der initialisierungs Phase moechte ich ein Bild zeichnen. Dann sollen neue Elemente nach einer bestimmten Zeit eingefuegt werden ohne, dass das initialisierungs Bild neu gemalt werden muss (CPU schonen).
Wie verhindert man ein neu zeichen, aber es soll noch moeglich sein neue Elemente ins urspruengliche Bild einzufuegen?
Viele Gruesse
in der initialisierungs Phase moechte ich ein Bild zeichnen. Dann sollen neue Elemente nach einer bestimmten Zeit eingefuegt werden ohne, dass das initialisierungs Bild neu gemalt werden muss (CPU schonen).
Wie verhindert man ein neu zeichen, aber es soll noch moeglich sein neue Elemente ins urspruengliche Bild einzufuegen?
Viele Gruesse
Code:
import java.awt.*;
import javax.swing.*;
public class BallRoom extends JApplet implements Runnable {
private final int WIDTH = 350;
private final int HEIGHT = 300;
private PaintSurface canvas;
Thread th;
public void init() {
this.setSize(WIDTH, HEIGHT);
canvas = new PaintSurface();
this.add(canvas, BorderLayout.CENTER);
}
public void start() {
th = new Thread(this);
th.start();
}
public void run() {
while (true){
repaint();
try {
Thread.sleep(100);
}
catch (InterruptedException ex){
}
}
}
public void update(Graphics g)
{
paint(g);
}
}
Code:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import javax.swing.JComponent;
class PaintSurface extends JComponent {
int ball1_x_pos = 0; // the starting X position
int ball1_y_pos = 150; // the starting Y position
int ball2_x_pos = 60; // the starting X position
int ball2_y_pos = 150; // the starting Y position
int d = 20; // the diameter of the ball
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
ball1_x_pos++; // move ball1 right one pixel
ball2_x_pos--; // move ball1 left one pixel
Shape ball1 = new Ellipse2D.Float(ball1_x_pos, ball1_y_pos, d, d);
Shape ball2 = new Ellipse2D.Float(ball2_x_pos, ball2_y_pos, d, d);
g2.setColor(Color.RED);
g2.fill(ball1);
g2.fill(ball2);
}
}