0

hi i have a problem with my bouncing ball with background program, the problem is that the ball flickers and i dont know hot to stop it:

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

Class 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;
    }
}
John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • I recommend using dedicated `ActionListeners` for the buttons, so comparisions like this `e.getSource().equals(play)` can be removed from the timer trigger `actionPerformed` method. This method should solely be used for the animation and nothing else. – Tom Nov 07 '14 at 18:54
  • As shown in the [duplicate](http://stackoverflow.com/q/9849950/230513), "Swing programs should override `paintComponent()` instead of overriding `paint()`."—[*Painting in AWT and Swing: The Paint Methods*](http://www.oracle.com/technetwork/java/painting-140037.html#callbacks). – trashgod Nov 08 '14 at 09:10

2 Answers2

1

What on Earth were you thinking when you programmed this?

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

It completely breaks the paint chain. Remove it to remove the flickering.

Then the code will have layering issues and transparency issues. My guess is that the panels need to be added in reverse order. E.G. instead of:

pelotapanel1JFrame

..it should be..

panel1pelotaJFrame

And the topmost panel needs to be transparent.

As more general advice:

  1. For better help sooner, post an MCVE (Minimal Complete Verifiable Example).
  2. One way to get images for an example, is to hot link to images seen in this Q&A.
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    How about we [Refine the Design](http://docs.oracle.com/javase/tutorial/uiswing/painting/refining.html) :-) – Paul Samsotha Nov 08 '14 at 05:03
  • @peeskillet I'd be cautious of any refinement before I understood what (the heck) the user is supposed to be seeing. At this point, I'm really unclear about that. – Andrew Thompson Nov 08 '14 at 05:36
0

I hear you. Struggles like this can be long and hard.

The flicker may be fixed with double buffering. If you not tried it, you might swap out the graphics for some really low grade ones, isolate the statements that display to separate programs, try a different machine, etc. Often I find such issues are coming from unexpected angles.

Jeff
  • 475
  • 1
  • 4
  • 7