aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes graphics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "graphics "components"" Watch "graphics "components"" New topic
Author

graphics "components"

rob byrd
Greenhorn

Joined: Aug 06, 2004
Posts: 1
I'm trying to draw a red square and a blue square but treat them as though they are individual components. Code below shows only the red square, but when the line frame.getContentPane().add(redAy) is commented out the blue square appears. Can anyone tell me what is going on and how to make both squares appear?
Thanks!

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

//-------------------------- blue -----------------------
class BlueAyComp extends JPanel{
int x = 0;
int y = 0;
int w = 0;
int h = 0;

public BlueAyComp(int ulx, int uly, int width, int hite){
x = ulx;
y = uly;
w = width;
h = hite;
setForeground(Color.blue);
}

public void paintComponent(Graphics g) {
g.drawRect(x, y, w, h);
}
}
//--------------------------- red ----------------------
class RedAyComp extends JPanel {
int x = 0;
int y = 0;
int w = 0;
int h = 0;

public RedAyComp(int ulx, int uly, int width, int hite){
x = ulx;
y = uly;
w = width;
h = hite;
setForeground(Color.red);
}
public void paintComponent(Graphics g){
g.drawRect(x, y, w, h);
}
}
//-----------------------------------------------------------
public class classtest{

public static void main(String arggs[]){
JFrame frame = new JFrame("Class Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,400);
frame.setBackground(Color.white);
JPanel blueAy = new BlueAyComp(40, 40, 30, 30);
JPanel redAy = new RedAyComp(200,200,30,30);

frame.getContentPane().add(blueAy);
frame.getContentPane().add(redAy);
frame.show();
}
}
Liam Tiarnach
Ranch Hand

Joined: Aug 06, 2004
Posts: 51


If you read the API specs for java.awt.GridLayout, using it with no arguements will cause the components to be laid out side to side by default (single row and same size).

But if you want to uses the default layout, then you need to specify where you want your components, the component sizes and so on...



Check java.awt.BorderLayout class in the API specs for more information and usage...

Have fun...

[ August 06, 2004: Message edited by: Liam Tiarnach ]
[ August 06, 2004: Message edited by: Liam Tiarnach ]

- Liam...<br />- ' He who never sleeps... '
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
BorderLayout is the default layout manager for JFrame. When you add a component to a BorderLayout without specifying a constraint, eg, BorderLayout.NORTH which translates to "North" (follow the link in the Field Detail section of the JFrame api to the Constant Field Values page), the component is added to the center section (see BorderLayout api). When you add a component to a section in the layout and come along later and add another component into the same section the layout manager replaces the first component with the new component and discards the first component. This explains the behavior you cited in your post: commenting out one line adding the second component reveals the first–added component. The solution is to add the components to different sections (as shown above) or use a different layout manager.
Liam Tiarnach
Ranch Hand

Joined: Aug 06, 2004
Posts: 51
Thanks Criag...

I gots to be more careful, I had an explaination also with a link to the Java Tutorial regarding Swing and Layout and one to the API specs...
I guess upon editing my comments on the code, it was lost...

Any way, here are those links...

Lesson: Laying Out Components Within a Container
http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html
J2SE SDK 1.4.2 API specs
http://java.sun.com/j2se/1.4.2/docs/api/index.html
 
Consider Paul's rocket mass heater.
 
subject: graphics "components"