在Java弹跳球中闪烁

在Java弹跳球中闪烁,java,swing,awt,Java,Swing,Awt,嗨,我的背景弹跳球程序有问题,问题是球闪烁,我不知道如何停止它: 类Graficos: public class Graficos extends JFrame implements ActionListener { private JButton play, pause, exit; private Timer reloj; private Pelota pelota; private JPanel panel2, panel1; public Gr

嗨,我的背景弹跳球程序有问题,问题是球闪烁,我不知道如何停止它:

Graficos

public class Graficos extends JFrame implements ActionListener {

    private JButton play, pause, exit;
    private Timer reloj;
    private Pelota pelota;
    private JPanel panel2, panel1;

    public Graficos(){
        super();
        reloj = new Timer(200, this);
        this.setLayout(new BorderLayout());
        pelota = new Pelota();
        play = new JButton("Play");
        play.setFont(new Font("ARIAL", Font.BOLD, 20));
        play.setBackground(Color.GREEN);
        play.setSize(20, 20);
        pause = new JButton("Pause");
        pause.setFont(new Font("ARIAL", Font.BOLD, 20));
        pause.setBackground(Color.ORANGE);
        pause.setSize(20, 20);
        exit = new JButton("Exit");
        exit.setFont(new Font("ARIAL", Font.BOLD, 20));
        exit.setBackground(Color.RED);
        exit.setSize(20, 20);
        panel1 = new JPanel();
        panel2 = new JPanel();
        panel2.setLayout(new GridLayout(1, 3));

        this.add(panel1, BorderLayout.CENTER);
        panel1.add(pelota, BorderLayout.CENTER);
        this.add(panel2, BorderLayout.SOUTH);
        panel2.add(play, BorderLayout.EAST);
        panel2.add(pause, BorderLayout.CENTER);
        panel2.add(exit, BorderLayout.WEST);

        this.setSize(440, 460);
        this.setVisible(true);

        reloj.start();

        play.addActionListener(this);
        pause.addActionListener(this);
        exit.addActionListener(this);
    }

    public void paint(Graphics g){
        super.paint(g);
        pelota.paintComponent(g);
        panel1.paintComponents(g);
    }

    public static void main(String args[]){
        Graficos x = new Graficos();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

        if(e.getSource().equals(play))reloj.start();
        else{
            if(e.getSource().equals(pause))reloj.stop();
            else{
                if(e.getSource().equals(exit)){
                    reloj.stop();
                    System.exit(0);
                }
                else if(e.getSource().equals(reloj)){
                    pelota.muevete();
                    this.repaint();
                }
            }
        }
    }
}
Pelota

public class Pelota extends JPanel {

    private int x, y, dx=5, dy=5, ancho, alto;
    private ImageIcon foto, fondo;

    public Pelota(){

        x = 5;
        y = 5;
        fondo = new ImageIcon(getClass().getResource("genious.jpg"));
        foto = new ImageIcon(getClass().getResource("ball.png"));
        ancho = foto.getIconWidth();
        alto = foto.getIconHeight();
        this.setVisible(true);
        this.setSize(fondo.getIconWidth(), fondo.getIconHeight());
    }

    /*public void pintate(Graphics g){
        super.paint(g);
        g.drawImage(fondo.getImage(),0,0,null);
        g.drawImage(foto.getImage(),x,y,null);
    }
    */

    public void paintComponent(Graphics g) { 
         super.paintComponent(g); 
         g.drawImage(fondo.getImage(),0,0,this);
         g.drawImage(foto.getImage(),x,y,this);
         }

    public void muevete(){
        x += dx;
        y += dy;
        if(x+ancho >=fondo.getIconWidth()+30){
            dx *= -1;
        }
        if(y+alto >= fondo.getIconHeight()+40){
            dy *= -1;
        }
        if(x<=-40){
            dx *= -1;
        }
        if(y<=-10){
            dy *= -1;
        }
        //repaint();
    }

    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 getDx() {
        return dx;
    }

    public void setDx(int dx) {
        this.dx = dx;
    }

    public int getDy() {
        return dy;
    }

    public void setDy(int dy) {
        this.dy = dy;
    }

    public int getAncho() {
        return ancho;
    }

    public void setAncho(int ancho) {
        this.ancho = ancho;
    }

    public int getAlto() {
        return alto;
    }

    public void setAlto(int alto) {
        this.alto = alto;
    }

    public ImageIcon getFoto() {
        return foto;
    }

    public void setFoto(ImageIcon foto) {
        this.foto = foto;
    }

    public ImageIcon getFondo() {
        return fondo;
    }

    public void setFondo(ImageIcon fondo) {
        this.fondo = fondo;
    }
}
公共类Pelota扩展了JPanel{
私人INTX,y,dx=5,dy=5,安科,阿尔托;
私人图像图标foto,fondo;
公共佩洛塔(){
x=5;
y=5;
fondo=newImageIcon(getClass().getResource(“genious.jpg”);
foto=newImageIcon(getClass().getResource(“ball.png”);
ancho=foto.getIconWidth();
alto=foto.getIconHeight();
此.setVisible(true);
this.setSize(fondo.getIconWidth(),fondo.getIconHeight());
}
/*公共空间平面图(图g){
超级油漆(g);
g、 drawImage(fondo.getImage(),0,0,null);
g、 drawImage(foto.getImage(),x,y,null);
}
*/
公共组件(图形g){
超级组件(g);
g、 drawImage(fondo.getImage(),0,0,this);
g、 drawImage(foto.getImage(),x,y,this);
}
公共无效muevete(){
x+=dx;
y+=dy;
如果(x+ancho>=fondo.getIconWidth()+30){
dx*=-1;
}
如果(y+alto>=fondo.getIconHeight()+40){
dy*=-1;
}

如果(x我听到了。像这样的斗争可能是漫长而艰难的


闪烁可能是通过双缓冲来修复的。如果你不尝试,你可能会将图形换成一些真正低级的图形,将显示的语句隔离到不同的程序中,尝试不同的机器,等等。我经常发现这样的问题来自意料之外的角度。

你编程时到底在想什么

public void paint(Graphics g){
    super.paint(g);
    pelota.paintComponent(g);
    panel1.paintComponents(g);
}
它完全破坏了油漆链。移除它以消除闪烁

那么代码将有分层问题和透明度问题。我猜面板需要按相反顺序添加。例如,而不是:

pelota
→ <代码>面板1
→ <代码>JFrame

……应该是

panel1
→ <代码>佩洛塔→ <代码>JFrame

最上面的面板需要透明

作为更一般的建议:

  • 为了更快地获得更好的帮助,请发布一个(最少完整的可验证示例)
  • 例如,获取图像的一种方法是热链接到中看到的图像

  • 我建议对按钮使用专用的
    ActionListeners
    ,这样可以从计时器触发器
    actionPerformed
    方法中删除类似于
    e.getSource().equals(play)
    的比较。此方法应仅用于动画,而不用于其他内容。如中所示,“Swing程序应该重写
    paintComponent()
    而不是重写
    paint()
    ”——。默认情况下,JPanel是双缓冲的。感谢您的更正。我们:-)@peeskillet怎么样?在我理解之前,我会小心任何改进(见鬼)用户应该看到了。在这一点上,我真的不清楚。