-
Re: Mutiple classfile, JPanel nothing shows up
Quote:
I got rid of my myFrame class
it gives me
myFrame bounds = java.awt.Rectangle[x=0,y=0,width=0,height=0]
Not sure where the printout is coming from if you have gotten rid of the myFrame class.
I'm assuming that the label for the printed message has the name of the class it is in. The idea when debugging with printlns is to have ALL of the printlns use a unique label so you can tell which println it came from.
-
Re: Mutiple classfile, JPanel nothing shows up
Quote:
Originally Posted by
Norm
Not sure where the printout is coming from if you have gotten rid of the class?
in the addComponentsToPane method
-
Re: Mutiple classfile, JPanel nothing shows up
Is the program working now?
-
Re: Mutiple classfile, JPanel nothing shows up
Quote:
Originally Posted by
Norm
Is the program working now?
No but I got a button showing up
Code :
controls.add(startButton);
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.validate();
panel.repaint();
System.out.println("Layout bounds = " + getBounds());
}
});
pane.add(controls,BorderLayout.SOUTH);
With that I got a button and every time I press it tells me the bounds
Layout bounds = java.awt.Rectangle[x=0,y=0,width=800,height=600]
But still no ball or pad
-
Re: Mutiple classfile, JPanel nothing shows up
Quote:
still no ball or pad
Do you mean that the paintComponent() method of those two classes is not being called?
Does the button's bounds take up the whole screen?
How are you using a layout manager to control where the components are positioned on the screen?
-
Re: Mutiple classfile, JPanel nothing shows up
Quote:
Originally Posted by
Norm
Do you mean that the paintComponent() method of those two classes is not being called?
Right none of those two classes are being called
Quote:
Originally Posted by
Norm
Does the button's bounds take up the whole screen?
No the button is a nice size and it's at the bottom of the screen
Quote:
Originally Posted by
Norm
How are you using a layout manager to control where the components are positioned on the screen?
pane.add(controls,BorderLayout.SOUTH);
-
Re: Mutiple classfile, JPanel nothing shows up
Please post the current code.
-
Re: Mutiple classfile, JPanel nothing shows up
arkanoidGame.java
Code :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class arkanoidGame extends JFrame {
FlowLayout layout = new FlowLayout();
JButton startButton = new JButton("Start Game");
private static final long serialVersionUID = 1L;
public arkanoidGame(String name) {
super(name);
}
public void addComponentsToPane(final Container pane) {
final JPanel panel = new JPanel();
JPanel controls = new JPanel();
controls.setLayout(new FlowLayout());
panel.setLayout(layout);
panel.add(new ball());
panel.add(new pad());
controls.add(startButton);
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.validate();
panel.repaint();
System.out.println("pad bounds = " + getBounds());
}
});
pane.add(controls,BorderLayout.SOUTH);
pane.add(new ball(),BorderLayout.NORTH);
pane.add(new pad(),BorderLayout.CENTER);
}
private static void createAndShowGui() {
arkanoidGame frame = new arkanoidGame("Arkanoid Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setSize(800,600);
frame.setBounds(0,0,800,600);
frame.addComponentsToPane(frame.getContentPane());
frame.setFocusable(true);
frame.setFocusTraversalKeysEnabled(false);
frame.setVisible(true);
}
public static void main(String[] args) {
try {
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
UIManager.put("swing.boldMetal", Boolean.FALSE);
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run() {
createAndShowGui();
}
});
}
}
-
Re: Mutiple classfile, JPanel nothing shows up
When I execute the code I get this:
pad paint bnds=java.awt.Rectangle[x=0,y=10,width=784,height=516]
Ball paint bnds=java.awt.Rectangle[x=0,y=0,width=784,height=10]
showing that the paintComponent methods are called and I get two lines (blue and red) drawn on the screen.
-
Re: Mutiple classfile, JPanel nothing shows up
Quote:
Originally Posted by
Norm
When I execute the code I get this:
pad paint bnds=java.awt.Rectangle[x=0,y=10,width=784,height=516]
Ball paint bnds=java.awt.Rectangle[x=0,y=0,width=784,height=10]
I get the same thing
-
Re: Mutiple classfile, JPanel nothing shows up
I'm confused. In post #29 you said: But still no ball or pad
If those messages are being printed then the ball and pad class's methods are being executed.
-
Re: Mutiple classfile, JPanel nothing shows up
Quote:
Originally Posted by
Norm
I'm confused. In post #29 you said: But still no ball or pad
If those messages are being printed then the ball and pad class's methods are being executed.
I added this since :
pane.add(new ball(),BorderLayout.NORTH);
pane.add(new pad(),BorderLayout.CENTER);
I think that's why we get some random bounds...
-
Re: Mutiple classfile, JPanel nothing shows up
The objects for the ball and pad classes are being added and their paintComponent methods are being called.
What is the problem now?
-
Re: Mutiple classfile, JPanel nothing shows up
Quote:
Originally Posted by
Norm
The objects for the ball and pad classes are being added and their paintComponent methods are being called.
What is the problem now?
They are not showing up
-
Re: Mutiple classfile, JPanel nothing shows up
Do you have the call to drawLine() I mentioned in post#4 in the paintComponent() method to show where the component is located?
What do you expect to show up? How does the drawing x,y values compare to the bounds of the component? Are they outside or inside?
-
Re: Mutiple classfile, JPanel nothing shows up
Quote:
Originally Posted by
Norm
Do you have the call to drawLine() I mentioned in post#4 in the paintComponent() method to show where the component is located?
What do you expect to show up? How does the drawing x,y values compare to the bounds of the component? Are they outside or inside?
I added the drawLine() and the lines show up.
The problem is the bound of each they are not right... I tryed with the method setBounds but it doesn't seem to work
-
Re: Mutiple classfile, JPanel nothing shows up
Quote:
the bound of each they are not right
How are you trying to control the bounds of the components?
The code uses two layout managers. They will position the components. You cant use the setBounds() method in the same container that there is a layout manager.
-
Re: Mutiple classfile, JPanel nothing shows up
Quote:
Originally Posted by
Norm
How are you trying to control the bounds of the components?
The code uses two layout managers. They will position the components. You cant use the setBounds() method in the same container that there is a layout manager.
How can I do it then? Can I put them where ever I want them to be?
-
Re: Mutiple classfile, JPanel nothing shows up
Quote:
Can I put them where ever I want them
Set the layout manager to null and use setBounds()
-
Re: Mutiple classfile, JPanel nothing shows up
Quote:
Originally Posted by
Norm
Set the layout manager to null and use setBounds()
Can i have overlapping bounds?
-
Re: Mutiple classfile, JPanel nothing shows up
Yes. The results would be like laying one piece of paper on another one. The top one will be visible, the one underneath will be hidden.
Perhaps if you made the top ones transparent you could see what is underneath.
-
Re: Mutiple classfile, JPanel nothing shows up
Quote:
Originally Posted by
Norm
Set the layout manager to null and use setBounds()
I'm trying to use setBounds it doesn't work I tryed to use it that way :
pane.add(new ball().setBounds(60,60,60,60));
-
Re: Mutiple classfile, JPanel nothing shows up
What does "doesn't work" mean? Please explain in more detail.
-
Re: Mutiple classfile, JPanel nothing shows up
Quote:
Originally Posted by
Norm
What does "doesn't work" mean? Please explain in more detail.
It's an error :
The method add(Component) in the type Container is not applicable for the arguments (void)
-
Re: Mutiple classfile, JPanel nothing shows up
Break the statement up into single steps.
setBounds() is a void method. You can not add(void) to a container.