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