This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Custom drawing  RSS feed

 
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
 
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
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!