Graphics2D and Panels

W

Wong Chee Wee

Dear all,

Recently i wrote some code involving the Graphics2D class in an application.
I used the following code:

public class Result extends JPanel{
public void paintComponent(Graphics comp){
super.paintComponent(comp);
Graphics2D comp2D = (Graphics2D) comp;

In another class, I called a new object of the Result class:

Result aObject = new Result();

This aObject is put into a panel. But on it just won't display correctly
(it'll have a very small area to display the graphics, e.g. 319 by 10
pixels)!
If i use a layout manager with the panel, no graphics is shown.

What is wrong? I would appreciate any help greatly. Thanks.

Regards,
Chee Wee
 
N

Nigel Wade

Wong said:
Dear all,

Recently i wrote some code involving the Graphics2D class in an
application. I used the following code:

public class Result extends JPanel{
public void paintComponent(Graphics comp){
super.paintComponent(comp);
Graphics2D comp2D = (Graphics2D) comp;

In another class, I called a new object of the Result class:

Result aObject = new Result();

This aObject is put into a panel. But on it just won't display correctly
(it'll have a very small area to display the graphics, e.g. 319 by 10
pixels)!
If i use a layout manager with the panel, no graphics is shown.

What is wrong? I would appreciate any help greatly. Thanks.

Regards,
Chee Wee


If you don't put any swing objects inside it then it doesn't have any
implicit size at all. If you only want the JPanel for drawing in then you
have to size it yourself.

If you know how big you want it to be, override getPreferredSize() so it
returns the size you want it to be to the layout manager.
 
W

Wong Chee Wee

Thanks a lot!

U're a great help.


Nigel Wade said:
If you don't put any swing objects inside it then it doesn't have any
implicit size at all. If you only want the JPanel for drawing in then you
have to size it yourself.

If you know how big you want it to be, override getPreferredSize() so it
returns the size you want it to be to the layout manager.

--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : (e-mail address removed)
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top