在Java Bouncing球中闪烁

时间:2014-11-07 18:23:10

标签: 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 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;
    }
}

2 个答案:

答案 0 :(得分:1)

当你编程时,你在想什么?

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

它彻底打破了油漆链。将其取下以消除闪烁。

然后代码将出现分层问题和透明度问题。我的猜测是需要以相反的顺序添加面板。例如。而不是:

pelotapanel1JFrame

..它应该是......

panel1pelotaJFrame

最顶层的面板需要透明。

作为更一般的建议:

  1. 为了更好地提供帮助,请发布MCVE(最小完整可验证示例)。
  2. 获取示例图片的一种方法是热链接到this Q&A中显示的图片。

答案 1 :(得分:0)

我听到了你的声音。像这样的斗争可能是漫长而艰难的。

闪烁可以通过双缓冲来修复。如果您没有尝试过,可能会将图形更换为一些非常低级的图形,将显示的语句隔离到单独的程序,尝试不同的机器等。通常我发现这些问题来自意想不到的角度。