Reputation: 1572
Here is my code
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 method is getting called by function which updates the value but the paint method is not getting called after 2 times.
Upvotes: 0
Views: 3160
Reputation: 16059
You are calling repaint on a different instance!
Change ...
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();
}
}
to
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();
}
}
And see VGR's comment ... +1 for him.
Upvotes: 1