这是我的代码
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.leappainter.model.GetXYCoordinatesPOJO;
public class DrawXY extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private int x;
private int y;
private int d;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getD() {
return d;
}
public void setD(int d) {
this.d = d;
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.RED);
g2d.drawOval(getX(), getY(), 30, 30);
System.out.println("painting");
}
public void draw() {
JFrame frame = new JFrame("Leap Draw");
frame.add(new DrawXY());
frame.setSize(600, 600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void update(GetXYCoordinatesPOJO getxy) {
// TODO Auto-generated method stub
setX((int) getxy.getX());
setY((int) getxy.getY());
setD((int) getxy.getD());
//System.out.println(getX());
DrawXY draw = new DrawXY();
draw.repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
通过更新值的函数调用Update方法,但两次后未调用paint方法。
最佳答案
您正在其他实例上调用重绘!
改变...
public void update(GetXYCoordinatesPOJO getxy) {
// TODO Auto-generated method stub
setX((int) getxy.getX());
setY((int) getxy.getY());
setD((int) getxy.getD());
//System.out.println(getX());
DrawXY draw = new DrawXY(); // < -- YOU CREATE NEW INSTANCE HERE
draw.repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
至
public void update(GetXYCoordinatesPOJO getxy) {
// TODO Auto-generated method stub
setX((int) getxy.getX());
setY((int) getxy.getY());
setD((int) getxy.getD());
//System.out.println(getX());
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
并查看VGR的评论... +1。