Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JFrames and animation

 
Reed Spool
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi and thanks for reading,
my question involves JFrames and drawing stuff
i made a class that extends JPanel and overrided <is that the right word?> the
drawing method just like Head First Java says in the GUI chapter, but when i use the fillrect method just doesn't work. i've coppied the code from the book exactly and result is a frame coming up with nothing on it. it compiles fine and everything but there is no orange rectangle
what's more, when i try to add a button, it doesn't look like it's there
but here's somethin cool, when i expand the frame, the button is in the middle of the screen.
seriously, am i missing something big?
thanks for reading-Reed
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
possibly indicates you are setting the frame's visibility to true,
before the components etc have been added to the frame.

post your code, so we can see how you have put it all together.
 
Campbell Ritchie
Marshal
Posts: 52630
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . or try looking up the validate() methods. A validate() as the last statement of the constructor often sorts out that sort of problem.
 
Reed Spool
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i looked up the validate method but i dont understand it at all.
anyways for michael here is my code:

code:


import javax.swing.*;
import java.awt.*;

class RobinHood
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
JPanel drawing = new DrawingPanel();
JButton button = new JButton("Click me");


frame.setSize(300,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(drawing);
frame.getContentPane().add(button);

}
}
class DrawingPanel extends JPanel
{
public void pantComponent(Graphics g)
{
g.setColor(Color.red);
g.fillRect(0,0,300,300);
}
}



again this results in a small frame of the correct size displaying nothing, but then when i expand the box the Click Me button is in the direct center of the screen. thanks for reading once again
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2 problems (possibly 3)

1)
frame.setVisible(true);
needs to be after you add the components

2)
frame.getContentPane().add(drawing);
frame.getContentPane().add(button);

the default layout manager for jframe is borderLayout, and when you do not
specify 'where' (in the borderlayout) the default of center is used.
You can only add one component to borderlayout.center, so when you add 'button'
you effectively kick out 'drawing'.

you can add more than one component to center if you add them to a JPanel
(or other container), then add the panel/container to center.

3)
'drawing' probably will not show, without components its preferredSize may be (0,0).
if it doesn't show, add a constructor to DrawingPanel, to include
setPreferredSize(new Dimension(100,100));
where 100,100 is anything you want
 
Reed Spool
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok this is my new code, i've taken out the button and only left the rectangle,
i also made all other requested changes and it is still displaying nothing but an empty frame

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)
g.fillRect(100,100,300,300);

look up the Graphics api docs for the above method,
in particular the first 2 arguments

2)
when you correct that, it still won't work.
when overriding methods, they have to match, otherwise
a new method is created which is not called

public void pantComponent(Graphics g)
public void paintComponent(Graphics g)
 
Reed Spool
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much, it was indeed sloppy of me
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!