Wie bewegen Sie die Form in Java?
Ich Formen zeichnen auf JPanel in einem separaten thread. Ich möchte zu bewegen, diese Formen durch Aufruf der Methode move()
aber die Figur nicht ändern seine Lage.
Diese meine CustomShape
public class CustomShape {
private static final int Y_STEP = 5;
private static final int X_STEP = 5;
public String name;
public Shape shape;
public Color color;
private Point newLocation;
public void move() {
newLocation.x += X_STEP;
newLocation.y += Y_STEP;
//How set new location ?
//It doesn't work
this.shape.getBounds().setLocation(newLocation);
System.out.println(String.format("New location is [%d,%d]",newLocation.x, newLocation.y));
System.out.println(String.format("Move to [%d,%d]", this.shape.getBounds().getLocation().x, this.shape.getBounds().getLocation().y));
}
public CustomShape(Shape shape, Color color, String name) {
this.shape = shape;
this.color = color;
this.name = name;
newLocation = this.shape.getBounds().getLocation();
}
Beispiel für die Ausgabe auf der Konsole
New location is [15,15]
Move to [10,10]
New location is [20,20]
Move to [10,10]
New location is [25,25]
Move to [10,10]
Mein JPanel
public class ViewPanel extends JPanel {
private static final long serialVersionUID = 5252479726227082794L;
private List<CustomShape> shapeList = new ArrayList<CustomShape>();
private Map<String, Thread> threads = new HashMap<String, Thread>();
private Timer timer;
private static final int TIMER_SPEED = 1000;
public ViewPanel() {
super();
this.setDoubleBuffered(true);
timer = new Timer(TIMER_SPEED, null);
timer.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
testMove();
}
});
}
private void testMove() {
for (CustomShape shape : shapeList) {
shape.move();
}
}
public void startMove() {
timer.start();
}
public void stopMove() {
timer.stop();
}
public void addShape(CustomShape shape) {
shapeList.add(shape);
if (!threads.containsKey(shape.getName())) {
Thread t = new Thread(new DrawThread(shape, this.getGraphics()),
shape.getName());
threads.put(shape.getName(), t);
t.start();
}
this.repaint();
}
public void removeShape(CustomShape shape) {
if (threads.containsKey(shape.getName())) {
Thread t = threads.remove(shape.getName());
t.interrupt();
shapeList.remove(shape);
}
this.repaint();
}
}
Thread für das zeichnen von Formen
public class DrawThread implements Runnable {
private static final int THREAD_SLEEP = 100;
private CustomShape shape;
private Graphics2D g2d;
private boolean interrupted = false;
public DrawThread(CustomShape shape, Graphics g) {
this.shape = shape;
this.g2d = (Graphics2D)g;
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(THREAD_SLEEP);
g2d.setColor(this.shape.getColor());
g2d.draw(this.shape.getShape());
} catch (InterruptedException e) {
System.out.println(String.format("interrupt %s", Thread
.currentThread().getName()));
interrupted = true;
} finally {
if (interrupted)
break;
}
}
}
}
Sie verpasste das repaint-Aufrufe in der Bewegung-Funktion.
Ich repaint Komponente im thread. Warum
Ich kann nicht sehen, keine repaint-Aufrufe außer für diejenigen, die addShape-und removeShape, aber vielleicht hast du Recht und ich habe es nicht bemerkt. Blick auf diese Frage: stackoverflow.com/questions/3695673/... vielleicht gibt es einige Hilfe.
Ich repaint Komponente im thread. Warum
this.shape.getBounds().setLocation(newLocation);
ändert sich nicht die Lage ?Ich kann nicht sehen, keine repaint-Aufrufe außer für diejenigen, die addShape-und removeShape, aber vielleicht hast du Recht und ich habe es nicht bemerkt. Blick auf diese Frage: stackoverflow.com/questions/3695673/... vielleicht gibt es einige Hilfe.
InformationsquelleAutor BILL | 2013-01-07
Du musst angemeldet sein, um einen Kommentar abzugeben.
Seine viel code zu Lesen,schwer zu finden der Fehler,so ist es besser, nach einem SSCCE.Aber hier ist ein Schuss, Z.B. um zu zeigen, wie Sie können verschieben von shapes durch eine Taste, die
JApplet
konzentriert sich auf einen Mausklick.Wenn Sie nicht möchten, verwenden Sie eine der Pfeiltasten, können Sie implementieren, Maus-Bewegung.Sehen
comments
im Programm für besseres Verständnis.Hier ist der code:
InformationsquelleAutor joey rohan