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

  • Home
  • SEARCH
  • 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 8932405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:18:55+00:00 2026-06-15T09:18:55+00:00

How do I get paintComponent to work within my drawArea JPanel? Additionally, my attempts

  • 0

How do I get paintComponent to work within my drawArea JPanel? Additionally, my attempts to set the drawArea’s dimensions have failed. No drawing occurs once running, and the JPanel is minimal size. DOes this have to do with MigLayout?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import net.miginfocom.layout.*;
import net.miginfocom.swing.MigLayout;
import java.awt.geom.*;

public class OvalDrawer extends JApplet
{
private JLabel numberL;
private JTextField numberTF;

private NumHandler numHandler;

public static final int WIDTH = 500;
public static final int HEIGHT = 500;

public int number;

//Create Layout
public void init()
{
    setLayout(new MigLayout("wrap 2"));
    numberL = new JLabel("Enter number of ovals to draw:");
    numberTF = new JTextField(7);

    add(numberL);
    add(numberTF);

    numHandler = new NumHandler();
    numberTF.addActionListener(numHandler);

    JPanel drawArea = new JPanel();
    drawArea.setSize(400, 400);
    drawArea.setBorder(BorderFactory.createTitledBorder("Draw Area"));
    add(drawArea, "span 2");

    setSize(WIDTH, HEIGHT);
}

//Event Handler
public class NumHandler implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        number = Integer.parseInt(numberTF.getText());
        repaint();
    }

}

//Draw Ovals
public void paintComponent (Graphics g)
{
    super.paintComponents(g);
    int x = 10;
    int y = 10;
    int width = 200;
    int height = 100;

    for (int i = 0; i < number; i++)
    {
        g.drawOval(x, y, width, height);

        x += 5;
        y += 5;
        width += 5;
        height += 5;
    }
}
}
  • 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-15T09:18:56+00:00Added an answer on June 15, 2026 at 9:18 am

    There is no paintComponent method in JApplet.

    You should avoid painting directly onto a top level container like JApplet, its much more the just a single layer container.

    Instead, create a second class, extending from something JPanel and perform you custom painting there.

    UPDATED

    You should also avoid using setPreferred/Minimum/MaximumSize, it’s to easy for some one else to circumvent.

    public class OvalDrawer extends JApplet {
    
        private JLabel numberL;
        private JTextField numberTF;
        private NumHandler numHandler;
        public static final int WIDTH = 500;
        public static final int HEIGHT = 500;
        public int number;
    
        //Create Layout
        public void init() {
    //        setLayout(new MigLayout("wrap 2"));
            setLayout(new BorderLayout());
            numberL = new JLabel("Enter number of ovals to draw:");
            numberTF = new JTextField(7);
    
            JPanel header = new JPanel();
            header.add(numberL);
            header.add(numberTF);
    
            add(header, BorderLayout.NORTH);
            add(new PaintPane());
    
            numHandler = new NumHandler();
            numberTF.addActionListener(numHandler);
    
    //        JPanel drawArea = new JPanel();
    //        drawArea.setSize(400, 400);
    //        drawArea.setBorder(BorderFactory.createTitledBorder("Draw Area"));
    //        add(drawArea, "span 2");
    
            setSize(WIDTH, HEIGHT);
        }
    
        //Event Handler
        public class NumHandler implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                number = Integer.parseInt(numberTF.getText());
                repaint();
            }
        }
    
        //Draw Ovals
        public class PaintPane extends JPanel {
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 400);
            }
    
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                int x = 10;
                int y = 10;
                int width = 200;
                int height = 100;
    
                for (int i = 0; i < number; i++) {
                    g.drawOval(x, y, width, height);
    
                    x += 5;
                    y += 5;
                    width += 5;
                    height += 5;
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying for hours to get my drawing method to work by drawing
I'm drawing a couple of shapes on a JPanel using the paintComponent() method. The
The issue I'm having is issue with is I'm trying to get the paintComponent
I get the following error even when my JAVA_HOME is set correctly. C:\workspace-sts-2.8.0.RELEASE\JBClient\target>echo %JAVA_HOME%
I've created a JPanel canvas that holds all graphics; namely JLabel. To get animated
I was a problem with drawing rectangle using mouse. I have solve my problem
I have A Jpanel with A GridLayout inside. Now I added another Jpanel inside
Get the following error periodically in an IIS application: Failed to load resources from
public class FrameDemo extends JPanel { public void paintComponent(Graphics g) { Graphics2D g2 =
I'm overriding a JPanel's paintComponent() method to draw some things on the screen. This

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.