java - java中的repaint()方法没有调用paint()方法?

标签 java swing graphics

这是我的代码

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();
        }
    }
}

更新方法被更新值的函数调用,但绘制方法在两次后没有被调用。

最佳答案

您正在另一个实例上调用重绘!

改变...

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。

关于java - java中的repaint()方法没有调用paint()方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19270926/

相关文章:

java - 如何访问 ZKoss 中的 session 范围实例

java - 编码 Sonar 插件2.13

java - 使用 configMap 和 Volumes 从 kubernetes 读取配置文件

Java:水平滚动时 JTable 列不适合 JScrollPane

linux - 为什么 X11 Composite 扩展与 Stereo 视觉效果不兼容?

java - 有什么方法可以将音频发送到 android 通话中使用的小扬声器吗?

java - Java 桌面应用程序与 SQLite 的捆绑安装(或任何数据库,例如)

java - 使用按钮作为列表的渲染器

c# - 使用 graphics.DrawImage 进行非平滑升级

python - 玛雅Python : Check to see if the attribute has any keyframes