• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

graphics "components"

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic