i hope you can modify my simple bouncing ball :(
its flickering.... i dont know how to solve it.
import javax.swing.*; // For JPanel, etc.
import java.awt.*; // For Graphics, etc.
import java.awt.event.*;
import java.awt.geom.*; // For Ellipse2D, etc.
//import java.util.*;
import java.io.*;
import java.util.Random;
public class aball extends JFrame
{
static Graphics g;
static int x = 5;
static int y = 30;
Timer timer;
Timer timerDown;
Timer timerUp;
Timer timerLeftRight;
Timer timerLeft;
Timer timerRight;
public aball()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(500,300);
setLocationRelativeTo(null);
setBackground(Color.blue);
add(new modJPanel());
timer = new Timer(10, new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
timerLeftRight.start();
if(y >= getHeight())
{
up();
System.out.println("go up!");
timer.stop();
}
else
{
down();
System.out.println("go down!");
timer.stop();
}
setTitle("" + y + " HEIGHT!!!: " + getHeight());
repaint();
}
});
timer.start();
timerDown = new Timer(1, new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
y +=4;
repaint();
setTitle("Y: " + y + " HEIGHT: " + getHeight() + " X: " + x + " WIDTH: " + getWidth());
if(y > getHeight() - 25)
{
timerDown.stop();
timerUp.start();
}
}
});
timerUp = new Timer(1, new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
y -=3;
repaint();
setTitle("Y: " + y + " HEIGHT: " + getHeight() + " X: " + x + " WIDTH: " + getWidth());
if(y < 25)
{
timerUp.stop();
timerDown.start();
}
}
});
timerLeft = new Timer(1 , new ActionListener()
{
public void actionPerformed(ActionEvent goLeft)
{
x -= 3;
repaint();
if(x <= 5)
{
timerRight.start();
timerLeft.stop();
}
}
});
timerRight = new Timer(1 , new ActionListener()
{
public void actionPerformed(ActionEvent goLeft)
{
x += 5;
repaint();
if(x >= getWidth() - 25)
{
timerRight.stop();
timerLeft.start();
}
}
});
timerLeftRight = new Timer(3, new ActionListener()
{
public void actionPerformed(ActionEvent ok)
{
if(x >= 500)
{
goLeft();
}
else
{
goRight();
}
//System.out.println("Im running");
timerLeftRight.stop();
}
});
} // constructor end
public void update(Graphics g)
{
paint(g);
System.out.println("Im running");
}
public void goLeft()
{
timerLeft.start();
}
public void goRight()
{
timerRight.start();
}
public void up()
{
timerUp.start();
}
public void down()
{
timerDown.start();
}
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.cyan);
g.fillOval(x,y,20,20);
}
public static void main(String[] arhs)
{
new aball();
}
}
class modJPanel extends JPanel
{
public modJPanel()
{
this.setBackground(Color.BLUE);
this.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "HELP THE NEWBIE"));
}
}