如何使用Swing JPanel强制在Java中重绘?

时间:2013-10-10 15:37:35

标签: java swing jpanel repaint

为什么我的图片不会重新绘制一个简单的动画?我从两个不同的方法调用repaint(),一个导致重绘,但另一个不重复。强制重绘的方法是从事件侦听器生成的。没有的是定时动画线程。我知道动画线程正在运行,只要我不断滑动滑块,它就会完美显示。请帮助!
PS是的,我在这里看到了很多类似的问题,我尝试过验证,重新验证,并使用paint vs paintComponent。构成代码的四个类如下:

import javax.swing.*;

public class gutz{

    public static int windowWidth = 640;
    public static int windowHeight = 480;

    public static void main(String[] args){

        hBod hb1 = new hBod(50, 30, 21, 111, 7, -11);   //mass, radius, xpos, ypos, xvel, yvel
        Thread t1 = new Thread(hb1);

        windowmakr w = new windowmakr();
        w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        w.setSize(windowWidth, windowHeight);
        w.setVisible(true);

        t1.start();
    }
}

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

public class windowmakr extends JFrame {

    private JSlider slider;
    private drawr panel;

    public windowmakr(){
        super("Orbit Explorer");
        panel = new drawr();
        panel.setBackground(Color.BLACK);

        slider = new JSlider(SwingConstants.HORIZONTAL, 0, 200, 10);
        slider.setMajorTickSpacing(10);
        slider.setPaintTicks(true);

        slider.addChangeListener(
                new ChangeListener(){
                    public void stateChanged(ChangeEvent e){
                        panel.setSpeed(slider.getValue());
                    }
                }
        );

        add(slider, BorderLayout.SOUTH);
        add(panel, BorderLayout.CENTER);

    }

}

import java.awt.*;
import javax.swing.*;

public class drawr extends JPanel{

    private int diameter = 10;
    private static int rad;
    private static int xpos;
    private static int ypos;

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.ORANGE);
        g.fillOval((gutz.windowWidth-diameter)/2, ((gutz.windowHeight-diameter)/2)-40, diameter, diameter);
        g.setColor(Color.CYAN);
        g.fillOval(xpos, ypos, rad, rad);

    }

/*  public void paint(Graphics g){
        super.paint(g);
        g.setColor(Color.ORANGE);
        g.fillOval((gutz.windowWidth-diameter)/2, ((gutz.windowHeight-diameter)/2)-40, diameter, diameter);
        g.setColor(Color.CYAN);
        g.fillOval(xpos, ypos, rad, rad);   
    }
*/  
    public void setSpeed(int newD){
        diameter = (newD >= 0 ? newD : 10);
        repaint();
    }

    public void renderImage(int r, int xp, int yp){
        rad=r;
        xpos=xp;
        ypos=yp;
        repaint();
    }

    public Dimension getPreferredSize(){
        return new Dimension(200,200);
    }

    public Dimension getMinimumSize(){
        return getPreferredSize();
    }
}

public class hBod implements Runnable{

    private int mass;
    private int rad;
    private int xpos;
    private int ypos;
    private double xvel;
    private double yvel;

    public drawr render;

    public hBod(int m, int r, int xp, int yp, double xv, double yv){
        mass=m;
        rad=r;
        xpos=xp;
        ypos=yp;
        xvel=xv;
        yvel=yv;

        render = new drawr();
    }

    public void run(){
        try{
            while(true){
                xpos+=xvel;
                ypos+=yvel;
                yvel+=1;
                System.out.printf("rad - %d, xpos - %d, ypos - %d\n", rad, xpos, ypos);
                render.renderImage(rad, xpos, ypos);

                Thread.sleep(1000);
            }
        }catch(Exception e){}
    }
}

2 个答案:

答案 0 :(得分:3)

就像我之前说的那样,问题在于render = new drawr();构造函数中的这一行hBod。在private drawr panel = new drawr()类中创建一个类似gutz的实例,并使用其构造函数将其传递给其他两个类,例如hBod hb1 = new hBod(50, 30, 21, 111, 7, -11, panel)windowmakr w = new windowmakr(panel),只需使用此引用指向{ {1}}来自panel.renderImage(...)类,hBod来自panel.setSpeed(...)类。

以下是修改后的代码,请学习Java编码约定:

windowmakr

答案 1 :(得分:2)

您可以在JFrame类中使用getContentPane()。repaint()方法