Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8203843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:42:01+00:00 2026-06-07T07:42:01+00:00

I am trying to build a an application where I intend to create some

  • 0

I am trying to build a an application where I intend to create some basic animations. My goal is to have a panel on top where the graphics will be painted and a panel below with regular buttons and labels.

I have done the code presented below but I am failing to draw rectangles inside previously drawn rectangles. I believe my problem is that I am calling a class (Rectangle) to draw the rectangles which extends a JPanel, meaning that everytime I try to draw a new rectangle a new panel will be creating not overlapping the previous one!

EDIT

I’ve looked up at some more examples, and finally could come up with a code to generate shapes (rectangles this case) as I need them on the same panel. I’ve made some changes to the code, below is the new code.

L.java

import java.awt.Color;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

class L extends JFrame {

public static final int ww = 1000;
public static final int wh = 600;
public static final String wt = "Teste";
Container pane = getContentPane();
DrawRectangle rectangle = new DrawRectangle();

public L() {
    setSize(ww,wh);
    this.setTitle(wt);
    Sim();
    pane.add(rectangle);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}

public void addRectangle(int px, int py, int pwh, Color pc) {
    this.rectangle.addRectangle( px, py, pwh, pc);
}

public void Sim() {
    addRectangle(100,100,250,Color.red);
    addRectangle(200,200,50,Color.green);
}

public static void main(String[] args) {

    L l = new L();

    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (UnsupportedLookAndFeelException ex) {
        ex.printStackTrace();
    } catch (IllegalAccessException ex) {
        ex.printStackTrace();
    } catch (InstantiationException ex) {
        ex.printStackTrace();
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();
    }

    //} 

}

}  

DrawRectangle.java

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.JPanel;

class DrawRectangle extends JPanel {
private java.util.List<Rectangle2D> squares;
private java.util.List<Color> colors;
//private int a, startX, startY;
public DrawRectangle(){
    squares = new ArrayList<Rectangle2D>();
    colors = new ArrayList<Color>();
}

public void addRectangle(int px, int py, int pwh, Color pc)  { // square
    squares.add( new Rectangle2D.Double(px, py, pwh, pwh) ) ;
    colors.add(pc);
    //this.a = a;
    //this.startX = startX;
    //this.startY = startY;
}
public void paintComponent(Graphics g) {
    Graphics2D g1 = (Graphics2D) g;
    for( Rectangle2D rect : squares ) {
        System.out.println(colors);
        g1.setColor(colors);
        g1.draw(rect);
    }
}
}

A new problem has now arose! What am I doing wrong that I can’t use the Color I am passing to the method to paint the shapes with different colors?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T07:42:02+00:00Added an answer on June 7, 2026 at 7:42 am

    Hmm well maybe try creating a JPanel which accepts an array of Rectangles and then draw those too the panel like so:

    import java.awt.*;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class RectangleDrawingTest extends JFrame {
    
        public RectangleDrawingTest() {
            createAndShowUI();
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    RectangleDrawingTest drawingTest = new RectangleDrawingTest();
                }
            });
        }
    
        private void createAndShowUI() {
            setTitle("Rectangle Drawing Test");
            setSize(300, 300);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setResizable(false);
            setLocationRelativeTo(null);
            addComponentsToContentPane(this.getContentPane());
            setVisible(true);
        }
    
        private void addComponentsToContentPane(Container contentPane) {
    
            Rectangle[] recs = new Rectangle[3];
            recs[0] = new Rectangle(100, 100, 150, 100);
            recs[1] = new Rectangle(100, 100, 125, 75);
            recs[2] = new Rectangle(100, 100, 100, 50);
    
            MyPanel mr = new MyPanel(recs);
    
            contentPane.add(mr);
        }
    }
    
    class MyPanel extends JPanel {
    
        private final Rectangle[] recs;
    
        public MyPanel(Rectangle[] recs) {
            this.recs = recs;
        }
    
        @Override
        protected void paintComponent(Graphics grphcs) {
            super.paintComponent(grphcs);
            grphcs.setColor(Color.BLACK);
            for (int i = 0; i < recs.length; i++) {
                grphcs.drawRect(recs[i].x, recs[i].y, recs[i].width, recs[i].height);
            }
        }
    }
    

    Addendum:

    Because you have both squares and color ArrayList transverse them using an for loop like this with a incrementing variable (squares size and color size are the same of course):

     for( int i=0;i<squares.size();i++) {
           // System.out.println(colors);
            g1.setColor(colors.get(i));
            g1.draw(squares.get(i));
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build an application, but it gives some error. My JDK
I'm trying to build an application from source in windows that requires some Unix
i'm trying to build an application that every given time, will connect to a
I am trying to build a web application, where I have abstracted logic in
I'm trying to build an application that will allow the user to setup N
I'm trying to build an application in my ARM9 (Freindly ARM) board that will
I am trying to build an application in php and I have an encrypt/decrypt
We're trying to build an application that will describe our price list. The price
I'm trying to build an application in Android using some codes from Objective-C (IPhone
I'm trying build my application using REST and Spring MVC. For some entities I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.