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 6473253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:26:39+00:00 2026-05-25T06:26:39+00:00

As a beginner, whenever I want to add graphical shapes inside the frame I

  • 0

As a beginner, whenever I want to add graphical shapes inside the frame I do something like this:

public void paintComponent(Graphics g) {
    super.paintComponent(g); 
    g.fillRect(0, 0, 10, 10);
    g.fillOval(10,10,10,10);
}

How do I draw an unlimited number of shapes automatically inside the frame? If I follow the way I did above I only have a limited number of shapes(Rect, Oval and nothing else).

I’m looking for something different, for example, whenever a method addStuff(x, y) has called, it draws “Stuff” automatically at the coordinate x and y without having to edit anything inside paintComponent manually again.

I used to do this with the acm package and it was easy. Just like the code below.

for (int i = 0; i < NCIRCLES; i++) {
    double r = rgen.nextDouble(MIN_RADIUS, MAX_RADIUS);
    double x = rgen.nextDouble(0, getWidth() - 2 * r);
    double y = rgen.nextDouble(0, getHeight() - 2 * r);
    GOval circle = new GOval(x, y, 2 * r, 2 * r);
    circle.setFilled(true);
    circle.setColor(rgen.nextColor());
    add(circle);
}

As you can see above, I can add as many circles as I want, I know it can take pages to explain this but I just want a brief instruction on how to create something similar to the code above without relying on acm package.

  • 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-05-25T06:26:40+00:00Added an answer on May 25, 2026 at 6:26 am

    I would use an abstract class to define this behavior.

    abstract class OpalDraw {
        protected int x1, y1, x2, y2;
        protected OpalDraw(int x1, int y1, int x2, int y2) {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
        }
        abstract public void draw(Graphics g);
    }
    
    class OpalOval {
        public OpalOval(int x1, int y1, int x2, int y2) { super(x1,y1,x2,y1); }
        public void draw(Graphics g) {
            g.drawOval(x1,y1,x2,y1);
        }
    }
    
    class OpalRect {
        public OpalRect(int x1, int y1, int x2, int y2) { super(x1,y1,x2,y1); }
        public void draw(Graphics g) {
            g.drawRect(x1,y1,x2,y1);
        }
    }
    

    Then in your painting container you can have

    addOval(int x1, int y1, int x2, int y2) {
        myDraws.add(new OpalOval(x1,x2,y1,y2);
    }
    
    addRect(int x1, int y1, int x2, int y2) {
        myDraws.add(new OpalRect(x1,x2,y1,y2);
    }
    

    Then later inside paintComponent(Graphics) you can

    for(OpalDraw draw : myDraws) draw.draw(g);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Absolute beginner question: I have a template file index.html that looks like this: ...
This question may be simple for experts but for a beginner like me it
Beginner question. How come I can do this: Public Class Form1 Private StudentsInMyRoom As
I'm a LaTeX beginner. Up until now whenever I've created a table (using the
Beginner here. Why is this an endless loop ? for (p = 0; p
Beginner here: In this answer to my question of how to insert data into
I'm a beginner on Gmaps programming. I want to create an InfoWindow containing two
(Beginner to HTML) I have made a Photoshop mock-up of the website I want
Beginner question. My goal is to add documents to my CouchDB database on a
Beginner question here! look at picture, I want the butten-edittext-button to fill the width

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.