Simple question - adding shapes to the frame

Say i have a class 'TheBall' which will draw a ball and make it move left to right in an animation

and i have another class 'ThePaddle' which will draw a paddle and follow the mouse pointer.

class TheBall

public void paintComponent(Graphics g)

{ //draw the ball}

class ThePaddle

public void paintComponent(Graphics g)

{ //draw the paddle}

Now in the main application i got a JButton "Start" which when pressed will basiclly display the 'TheBall' animation

and then the 'ThePaddle' (will be adding collisions later)

What i am stuck on is adding these two class files at the same time

Doing this works fine for adding one of the shapes to the frame

TheBall ball = new TheBall();

ballPanel.add(ball);

frame.add(ballPanel);

but then how do i go about adding another shape from another class the paddle along with the 'TheBall' animtaion

ThePaddle paddle = new ThePaddle();

ballPanel.add(paddle );

frame.add(ballPanel); ?

[1042 byte] By [Chillia] at [2007-11-27 1:41:33]
# 1

If you want to make a game with multiple objects on tha panel then you need to use a null layout and control the location of your objects manually. Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html]Layout Managers[/url] and check out the section on "Absolute Positioning" for more information.

You already know how to use a Timer to move a ball around the panel.

If you want to add a paddle that is controlled by the mouse, then you need to add a MouseListener to the panel and handle the mouseMoved() event to reposition the paddle. The tutorial also has a section on writing MouseListeners.

camickra at 2007-7-12 0:57:05 > top of Java-index,Desktop,Core GUI APIs...
# 2

Trying to get mutiple shapes onto the panel then add it to the frame

so ballPanel.setLayout(null);//null layout to control the location of my shapes manually.

but doing that doesnt seem to work for me

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class example

{

public static void main(String args[])

{

Runnable r;

r = new Runnable()

{

public void run()

{

start();

}

};

SwingUtilities.invokeLater(r);

}

public static void start()

{

JFrame frame;

JLabel label;

JPanel panel;

JPanel ballPanel;

JButton button;

TheFirstBall ball1;

ball1 = new TheFirstBall();

TheSecondBall ball2;

ball2 = new TheSecondBall();

frame = new JFrame();

frame.setTitle("Ball");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLayout(new BorderLayout());

button = new JButton("Click me");

button.addActionListener(

new ActionListener()

{public void actionPerformed(ActionEvent e)

{

}

}

);

panel = new JPanel();

panel.add(button);

frame.add(panel, BorderLayout.NORTH);

ballPanel = new JPanel();

//ballPanel.setLayout(null); ?

ballPanel.add(ball1);

//ballPanel.setBounds(20,20,40,40);?

ballPanel.add(ball2);

frame.add(ballPanel, BorderLayout.CENTER);

frame.setSize(300, 200);

frame.setVisible(true);

}

}

class TheFirstBall extends JPanel

{

public void paintComponent(Graphics g)

{

super.paintComponent(g);

Graphics2D g2;

g2 =(Graphics2D) g;

g.setColor(Color.RED);

g.fillOval(20,20,40,40);

}

public Dimension getPreferredSize()

{

return new Dimension(300, 200);

}

}

class TheSecondBall extends JPanel

{

public void paintComponent(Graphics g)

{

super.paintComponent(g);

Graphics2D g2;

g2 =(Graphics2D) g;

g.setColor(Color.RED);

g.fillOval(60,60,40,40);

}

public Dimension getPreferredSize()

{

return new Dimension(300, 200);

}

}

Chillia at 2007-7-12 0:57:05 > top of Java-index,Desktop,Core GUI APIs...
# 3

Your don't need multiple classes for your balls. You create a single class and then you pass parameters to make each ball distinct if you wish.

So, for example you would pass in the size and color of the ball. Then your painting code would use that information to paint the ball.

Also, the size of the panel should be equal to the size of the ball. Right now the size of every panel is (300, 200).

Also you should extend JComponent and not JPanel. This way each ball will be non-opaque. Right now both balls are positioned at location (0, 0) so only one ball will ever be visible since they are painted on top of one another. So by making the ball non-opaque and by making the size of the component equal to the size of the ball you will be able to display multiple balls at one time.

camickra at 2007-7-12 0:57:05 > top of Java-index,Desktop,Core GUI APIs...
# 4

OK say i want a

class to draw a ball

and another

class to draw a square

I can add them to the panel ok but they are side by side like a flow layout

what i want to know (i think) is how to absolute position but cant seem to get it too work....setBounds..?

ATM it looks like thisO[]circle followed by a square

What i want do to is this [o]have the the square painted first then the circle on top..

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class example

{

public static void main(String args[])

{

Runnable r;

r = new Runnable()

{

public void run()

{

start();

}

};

SwingUtilities.invokeLater(r);

}

public static void start()

{

JFrame frame;

JLabel label;

JPanel panel;

JPanel ballPanel;

JButton button;

TheBall ball;

ball = new TheBall();

TheSquare square;

square = new TheSquare();

frame = new JFrame();

frame.setTitle("Ball");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLayout(new BorderLayout());

button = new JButton("Click me");

button.addActionListener(new ActionListener()

{public void actionPerformed(ActionEvent e)

{}

}

);

panel = new JPanel();

panel.add(button);

frame.add(panel, BorderLayout.NORTH);

ballPanel = new JPanel();

//ballPanel.setLayout(null);

ballPanel.add(ball);

//ballPanel.setBounds(80,80,80,80);

ballPanel.add(square);

frame.add(ballPanel, BorderLayout.CENTER);

frame.setSize(300, 200);

frame.setVisible(true);

}

}

class TheBall extends JComponent

{

public void paintComponent(Graphics g)

{

super.paintComponent(g);

Graphics2D g2;

g2 =(Graphics2D) g;

g.setColor(Color.RED);

g.fillOval(0,0,40,40);

}

public Dimension getPreferredSize()

{

return new Dimension(40, 40);

}

}

class TheSquare extends JComponent

{

public void paintComponent(Graphics g)

{

super.paintComponent(g);

Graphics2D g2;

g2 =(Graphics2D) g;

g.setColor(Color.BLACK);

g.fillRect(0,0,40,40);

}

public Dimension getPreferredSize()

{

return new Dimension(40, 40);

}

}

Chillia at 2007-7-12 0:57:05 > top of Java-index,Desktop,Core GUI APIs...
# 5

Quit trying to do two things at once.

I gave you a link to a tutorial that shows you how to use absolute positioning. Read the tutorial. Understand the example and create your own simple test program. Start by using existing components. Add two JLabels to a panel and use a null layout manager. Once you understand how that works replace one of the labels with your circle. Once that works replace the other label with your square.

Your demo code is trying to do two things at once:

a) create a custom component

b) use a null layout manager

When it doesn't work your don't know if the problem is because of the custom component or the null layout manager.

So learn to simplify problem solving by learning one new concept at a time.

camickra at 2007-7-12 0:57:05 > top of Java-index,Desktop,Core GUI APIs...
# 6

Need advise on getting the keylistener to work...

Is it implemented in the right place...?

public class AbsoluteLayoutDemo{

public static void addComponentsToPane(Container pane) {

pane.setLayout(null);

//DrawBall ball = new DrawBall();

TheSquare square = new TheSquare();

//DrawGrid grid = new DrawGrid();

// pane.add(ball);

pane.add(square);

// pane.add(grid);

// ball.setBounds(50,50,900,900);

square.setBounds(100,80,40,40);

//grid.setBounds(50,50,1000,1000);

}

private static void createAndShowGUI() {

//Create and set up the window.

JFrame frame = new JFrame("AbsoluteLayoutDemo");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set up the content pane.

addComponentsToPane(frame.getContentPane());

//Size and display the window.

Insets insets = frame.getInsets();

frame.setSize(900,900);

frame.setVisible(true);

}

public static void main(String[] args) {

//Schedule a job for the event-dispatching thread:

//creating and showing this application's GUI.

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

}

class TheSquare extends JComponent implements KeyListener

{

public void paintComponent(Graphics g)

{

super.paintComponent(g);

Graphics2D g2;

g2 =(Graphics2D) g;

g.setColor(Color.BLACK);

g.fillRect(0,0,40,40);

}

public void keyPressed( KeyEvent event )

{

System.out.println("keyPressed" + event.getKeyCode());

}

public void keyReleased( KeyEvent event )

{

System.out.println("keyReleased" + event.getKeyCode());

}

public void keyTyped( KeyEvent event )

{

System.out.println("keyTyped" + event.getKeyCode());

}

public Dimension getPreferredSize()

{

return new Dimension(40, 40);

}

}

Chillia at 2007-7-12 0:57:05 > top of Java-index,Desktop,Core GUI APIs...
# 7
[url http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html]How to Write a Key Listener[/url]
camickra at 2007-7-12 0:57:05 > top of Java-index,Desktop,Core GUI APIs...