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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:48:34+00:00 2026-06-13T05:48:34+00:00

I have a simple program (most of it is going to be used for

  • 0

I have a simple program (most of it is going to be used for something else) that just draws ovals when a user clicks the drawPanel JPanel and displays ovals. The problem is that the repaint() method isn’t calling paintComponent(). Why is this?

Here is the code:

// Imports Used:
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import java.awt.geom.*;
// Class DrawPolygon
public class DrawPolygon extends JPanel implements MouseListener
{
// Variables used in GUI
static JFrame frame;
static JPanel drawPanel;
static JPanel labelPanel;
static JPanel primaryPanel;
static JButton loadButton;
static JButton saveButton;
static JButton clearButton;
static JButton addButton;
static JButton moveButton;
static JButton exitButton;
// Variables used for GUI interaction
static final int SIZE = 6;
static int numVertices;
ArrayList<Point> vertices;
static Color outlineColor = Color.RED;
static int lineWidth = 10;
static Color fillColor = Color.BLACK;
// Constructor for new Polygon Drawing Application
public DrawPolygon()
{
    // Create Frame
    frame = new JFrame("Vector Painter");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // Set Location of GUI on screen
    frame.setLocation(225,100);
    // Primary Panel to store Draw Panel and Lable/Button Panel
    primaryPanel = new JPanel();
    // Create Label Panel
    createLabelPanel();
    // Create Draw Panel
    createDrawPanel();
    // Add panels to Primary
    primaryPanel.add(labelPanel);
    primaryPanel.add(drawPanel);
    // Add to frame
    frame.getContentPane().add(primaryPanel);
    frame.pack();
    frame.setVisible(true);
}
// Add Buttons to left side
public void createLabelPanel()
{
    // Label Panel to add Buttons
    labelPanel = new JPanel();
    labelPanel.setBackground(Color.BLACK);
    labelPanel.setPreferredSize(new Dimension(200,600));
    // Create JButtons
    loadButton = new JButton("LOAD");
    loadButton.setPreferredSize(new Dimension(180,75));
    loadButton.setBackground(Color.BLACK);
    loadButton.setForeground(Color.WHITE);
    loadButton.addMouseListener(this);
    saveButton = new JButton("SAVE");
    saveButton.setPreferredSize(new Dimension(180,75));
    saveButton.setBackground(Color.BLACK);
    saveButton.setForeground(Color.WHITE);
    saveButton.addMouseListener(this);
    clearButton = new JButton("CLEAR");
    clearButton.setPreferredSize(new Dimension(180,75));
    clearButton.setBackground(Color.BLACK);
    clearButton.setForeground(Color.WHITE);
    clearButton.addMouseListener(this);
    addButton = new JButton("ADD");
    addButton.setPreferredSize(new Dimension(180,75));
    addButton.setBackground(Color.BLACK);
    addButton.setForeground(Color.WHITE);
    addButton.addMouseListener(this);
    moveButton = new JButton("MOVE");
    moveButton.setPreferredSize(new Dimension(180,75));
    moveButton.setBackground(Color.BLACK);
    moveButton.setForeground(Color.WHITE);
    moveButton.addMouseListener(this);
    exitButton = new JButton("EXIT");
    exitButton.setPreferredSize(new Dimension(180,75));
    exitButton.setBackground(Color.BLACK);
    exitButton.setForeground(Color.WHITE);
    exitButton.addMouseListener(this);
    // Add Buttons to Label Panel
    labelPanel.add(loadButton);
    labelPanel.add(saveButton);
    labelPanel.add(clearButton);
    labelPanel.add(addButton);
    labelPanel.add(moveButton);
    labelPanel.add(exitButton);
}
// Creates Draw Panel
public void createDrawPanel()
{
    // Draw Panel to Draw Polygons
    drawPanel = new JPanel();
    drawPanel.setBackground(Color.BLACK);
    drawPanel.setPreferredSize(new Dimension(600,600));
    drawPanel.addMouseListener(this);
    vertices = new ArrayList<>();
}

@Override
public void paintComponent(Graphics g)
{
    super.paintComponent(g);

    g.setColor(Color.ORANGE);

    for (Point spot : vertices)
    {
        g.fillOval(spot.x-SIZE, spot.y-SIZE, SIZE*2, SIZE*2);
    }

    g.drawString("Count: " + vertices.size(), 5, 15);

    System.out.println("repaint is working?");
}

// Execute when Load button is clicked
public void loadButton()
{
    System.out.println("Load Button CLICKED!!");
}
// Execute when Save button is clicked
public void saveButton()
{
    System.out.println("Save Button CLICKED!!");
}
// Execute when Clear button is clicked
public void clearButton()
{
    System.out.println("Clear Button CLICKED!!");
}
// Execute when Add button is clicked
public void addButton()
{
    System.out.println("Add Button CLICKED!!");
}
// Execute when Move button is clicked
public void moveButton()
{
    System.out.println("Move Button CLICKED!!");
}
public void mouseClicked(MouseEvent e)
{
    if (e.getSource() == loadButton)
    {
        loadButton();
    }
    else if (e.getSource() == saveButton)
    {
        saveButton();
    }
    else if (e.getSource() == clearButton)
    {
        clearButton();
    }
    else if (e.getSource() == addButton)
    {
        addButton();
    }
    else if (e.getSource() == moveButton)
    {
        moveButton();
    }
    else if (e.getSource() == exitButton)
    {
        System.exit(0);
    }
    else if (e.getSource() == drawPanel)
    {
        System.out.println("TEST");
        vertices.add(e.getPoint());
        repaint();
    }
}
// These are here because program wouldn't compile without them
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
// Main Function
public static void main(String[] args) 
{
    // Create Frame
    new DrawPolygon();        
}
}
  • 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-13T05:48:35+00:00Added an answer on June 13, 2026 at 5:48 am

    Actually repaint is being called, but on the current DrawPolygon object, but since you don’t seem to be displaying one of these paintComponent(...) won’t ever be called. To clarify, your current class, DrawPolygon, extends JPanel, but it doesn’t seem to have been added to any container that is part of your GUI hierarchy. Perhaps you want to add your current object, your this, to the GUI somewhere? In fact you probably should consider using your current object in place of the your drawPanel JPanel object. I would consider just deleting that variable entirely.

    Also unrelated to your problem, you almost never want to use MouseListeners on JButtons where ActionListeners should instead be used.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

below i have a code that runs in most of my simple programs ..
Say I have simple program that emulates a board game with a number of
I have a simple program that checks webpages for strings, example: Private Sub Button1_Click(ByVal
I have this simple program that computes salaries for four different worker types. It's
I have a simple program that creates a thread, loops twenty times and then
I have a simple Java program that reads in a text file, splits it
I have a very simple SDL program that uses only 1MB of memory with
I have a simple program that looks up details of an IP you give
I have Simple java program named MainController.java. Wehn I try to compile it from
I have a simple program. I want to set the path and some properties

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.