Simple moving ball
Could you take a quick look at this code
all it does is
move the image left when it touches the right wall
move the image right when it touches the left wall
when you click the JButton
I am now looking to add javax.swing.Timer to move the ball in an animation
Just wondering how I should go about this...?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
publicclass Ball
{
publicstaticvoid main(String args[])
{
Runnable r;
r =new Runnable()
{
publicvoid run()
{
startGUI();
}
};
SwingUtilities.invokeLater(r);
}
publicstaticvoid startGUI()
{
final JFrame frame;
JLabel label;
final JPanel panel;
JPanel ballPanel;
JButton button;
final DrawBall ball1;
ball1 =new DrawBall();
frame =new JFrame();
frame.setTitle("Ball");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
panel =new JPanel();
button =new JButton("Click me");
button.addActionListener(
new ActionListener()
{publicvoid actionPerformed(ActionEvent e)
{
ball1.moveBall();
frame.repaint();
}
}
);
panel.add(button);
frame.add(panel, BorderLayout.NORTH);
ballPanel =new JPanel();
ballPanel.add(ball1);
frame.add(ballPanel, BorderLayout.CENTER);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
class DrawBallextends JPanel
{
int x;
int y;
boolean goToRightWall;
boolean goToLeftWall;
public DrawBall()
{
x = 10;
y = 10;
goToRightWall =true;
goToLeftWall =false;
}
publicvoid moveBall()
{
//When ball touches left wall
if(x == 10){
goToRightWall =true;
goToLeftWall =false;
}
//When ball touches right wall
if(x == 200){
goToRightWall =false;
goToLeftWall =true;
}
if(goToRightWall)
moveBallR();
if(goToLeftWall)
moveBallL();
}
publicvoid moveBallR()
{
x=x+10;
}
publicvoid moveBallL()
{
x=x-10;
}
publicvoid paint(Graphics g)
{
Graphics2D g2;
g2 =(Graphics2D) g;
g.setColor(Color.RED);
g.fillOval(x,y,40,40);
}
public Dimension getPreferredSize()
{
returnnew Dimension(300, 200);
}
}
[5348 byte] By [
Chillia] at [2007-11-27 1:32:09]

# 1
A few pointers:
a) the default layout of a JFrame is a BorderLayout, so you don't need to reset it.
b) custom painting should be done by overriding the paintComponent() method, not the paint() method and you should have a super.paintComponent(...) as the first statement.
c) you only need to repaint the panel when the ball moves, not the entire frame.
[url http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html]How to Use Swing Timers[/url]
You already have the code for your ActionListener, so you just create the Timer using an ActionListener and then start the Timer.
# 2
Hows the coding looking so far are there any noobie mistakes?
Ive gotten the ball to move with the timer just left and right.. at certain x cords..
Now i want it to
Display a blank window when started with a JButton.
When button is pressed the ball starts its animation.
Am just not 100% sure how to go about this.. like where abouts to put code
eg button pressed triggers addActionListener then what frame.add(ballPanel);
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Ball
{
public static void main(String args[])
{
Runnable r;
r = new Runnable()
{
public void run()
{
startGUI();
}
};
SwingUtilities.invokeLater(r);
}
public static void startGUI()
{
JFrame frame;
JLabel label;
JPanel panel;
JPanel ballPanel;
JButton button;
DrawBall ball1;
ball1 = new DrawBall();
frame = new JFrame();
frame.setTitle("Ball");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
button = new JButton("Click me");
button.addActionListener(
new ActionListener()
{public void actionPerformed(ActionEvent e)
{
}
}
);
panel.add(button);
frame.add(panel, BorderLayout.NORTH);
ballPanel = new JPanel();
ballPanel.add(ball1);
frame.add(ballPanel);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
class DrawBall extends JPanel
{
int x;
int y;
boolean goToRightWall;
boolean goToLeftWall;
Timer timer;
public DrawBall()
{
x = 10;
y = 10;
goToRightWall = true;
goToLeftWall = false;
timer = new Timer(1, new leftToRight());
timer.start();
}
public void moveBall()
{
//When ball touches left wall
if(x == 10){
goToRightWall = true;
goToLeftWall = false;
}
//When ball touches right wall
if(x == 200){
goToRightWall = false;
goToLeftWall = true;
}
if(goToRightWall)
moveBallR();
if(goToLeftWall)
moveBallL();
}
public void moveBallR()
{
x=x+2;
}
public void moveBallL()
{
x=x-2;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2;
g2 =(Graphics2D) g;
g.setColor(Color.RED);
g.fillOval(x,y,40,40);
}
private class leftToRight implements ActionListener{
public void actionPerformed(ActionEvent e) {
moveBall();
repaint();
}
}
public Dimension getPreferredSize()
{
return new Dimension(300, 200);
}
}