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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:01:07+00:00 2026-06-11T15:01:07+00:00

I wrote a code in Java (using swing) which draws few polygons on a

  • 0

I wrote a code in Java (using swing) which draws few polygons on a panel.

public MyClass extends JPanel

The code is very simple (but long) and basically adds few Polygons, then adds few points to each polygon and then draw them on the screen (with drawPolygon).

My problem is when I run the program, I can’t see the drawings on the panel.
After a while, I figure out that when I re-size my frame, I can suddenly see the drawing but it duplicates itself many times (depends how much I re-size). If I play enough time with the resizing, I get:

 java.lang.OutOfMemoryError: Java heap space

Also, myPolygon.invalidate() doesn’t help.

When using setResizable(false) I can’t see my drawing at all.

Does anyone have a solution?

Duplicate Image Screenshot:1

  • 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-11T15:01:08+00:00Added an answer on June 11, 2026 at 3:01 pm

    To start with, in your paintComponent method, don’t call

    setPreferredSize(new Dimension(500,500));
    setVisible(true);
    validate();
    

    This will request a repaint, cause paintComponent to be recalled and you’ll end up in a nasty loop, consuming your CPU and (as you have found out), your memory.

    IF you can get away with it, you’re better off to draw the polygon to a buffer and draw the buffer to the screen on each iteration of the paintComponent. This will be faster in the long run…

    // Create a field
    private BufferedImage buffer;
    
    // Call this when you need to change the polygon some how...
    protected void createBuffer() {
    
        // You need to determine the width and height values ;)
        buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = image.createGraphics();
    
        int xoffset=5;//Multiply in order to "zoom" the picture
        int offset=0;//moves shape to the right
    
        p.addPoint(40*xoffset-offset, 30*xoffset-offset);
        p.addPoint(50*xoffset-offset,30*xoffset-offset);
        p.addPoint(57*xoffset-offset,37*xoffset-offset);
        p.addPoint(57*xoffset-offset,47*xoffset-offset);
        p.addPoint(50*xoffset-offset,54*xoffset-offset);
        p.addPoint(40*xoffset-offset,54*xoffset-offset);
        p.addPoint(33*xoffset-offset,47*xoffset-offset);
        p.addPoint(33*xoffset-offset, 37*xoffset-offset);
    
        g.drawPolygon(p);
        g.dispose();
    
    }
    
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (buffer != null) {
            Graphics2D g2d = (Graphics2D)g;
            g2d.drawImage(buffer, translateX, translateY, this);
        }
    }
    

    UPDATE

    So, anyway, the other fun things you’re doing are…

    1. Creating a static reference to your Polygon. Hope you weren’t intending to have more the one on the screen at a time 😉
    2. Add new points to an already existing polygon (each time paintComponent was called)
    3. Translating the polygon each time paintComponent was called

    Try something like this instead

    public class RoundTop extends JPanel {
    
        //Polygons declarations
        private Polygon p = new Polygon();
        //Translate variables;
        private int translateX = 10;
        private int translateY = 10;
    
        public RoundTop() {
    
            int xoffset = 5;//Multiply in order to "zoom" the picture
            int offset = 0;//moves shape to the right
    
            p.addPoint(40 * xoffset - offset, 30 * xoffset - offset);
            p.addPoint(50 * xoffset - offset, 30 * xoffset - offset);
            p.addPoint(57 * xoffset - offset, 37 * xoffset - offset);
            p.addPoint(57 * xoffset - offset, 47 * xoffset - offset);
            p.addPoint(50 * xoffset - offset, 54 * xoffset - offset);
            p.addPoint(40 * xoffset - offset, 54 * xoffset - offset);
            p.addPoint(33 * xoffset - offset, 47 * xoffset - offset);
            p.addPoint(33 * xoffset - offset, 37 * xoffset - offset);
    
            p.translate(translateX, translateY);
    
        }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
    
            Graphics2D g2d = (Graphics2D) g.create();
    
            g2d.drawPolygon(p);
    
            g2d.dispose();
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have wrote the code below which was taken from Java How to program
I wrote a piece of java code using threads, JDBC, Java Mail API etc.
I wrote such code today: import javax.swing.BorderFactory; import javax.swing.JApplet; import javax.swing.JPanel; import javax.swing.SwingUtilities; import
I'm new to java swing. I wrote a simple text editor using java swing,
I made few checkboxes using swing in Java. I want to write a superscript
I wrote some code (Java and LDAP) to create a user in the Active
I wrote the following code: import java.lang.*; import DB.*; private Boolean validateInvoice(String i) {
I wrote some code to read a file in my Java Servlet class. (I'm
i am a newbie for java server pages and wrote a code for a
I wrote a same(..similar?) piece of code for Java 7 and C#(.net 3.5) and

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.