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 ...
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Custom drawing  RSS feed

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) {
this.posX = posX;
this.posY = posY;
this.aWidth = width;
this.aHeight = height;

public SubProcess() {

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

If I add multiple subProcesses with

JFrame frame = new JFrame("VOPS");

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


//Display the window.

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?

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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!