• Post Reply Bookmark Topic Watch Topic
  • New Topic

Custom drawing

 
Piet Snot
Greenhorn
Posts: 18
Firefox Browser IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to create an application that renders custom components like rectangles, ovals, ...

I'm now trying to subclass Jcomponent and overriding paintComponent. The code is

public class SubProcess extends JPanel {

private int posX;
private int posY;
private int aWidth;
private int aHeight;

/**
* @param posX
* @param posY
* @param width
* @param height
*/
public SubProcess(int posX, int posY, int width, int height) {
super();
this.posX = posX;
this.posY = posY;
this.aWidth = width;
this.aHeight = height;
}

public SubProcess() {
super();
}

public void paintComponent(Graphics g) {
//super.paint(g);
g.drawRect(this.posX, this.posY, this.aWidth, this.aHeight);
}
}

If I add multiple subProcesses with

JFrame frame = new JFrame("VOPS");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

SubProcess subpro1 = new SubProcess(32,4,191,128);
SubProcess subpro2 = new SubProcess(12,202,182,134);

contentPane.add(subpro1);
contentPane.add(subpro2);

//Display the window.
frame.setSize(400,400);
//frame.pack();
frame.setVisible(true);

then only the latest subprocess is painted on the panel.

Does anybody have any idea how I can resolve this or is there a better way than subclassing JComponent?

Piet
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The default layout manager for JFrame is BorderLayout. If you add more than one component to the same section in the layout, only the last component is displayed. You can try something like

or a different layout manager
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!