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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:31:23+00:00 2026-05-30T08:31:23+00:00

So basically I’m writing a program where the user clicks and drags the mouse

  • 0

So basically I’m writing a program where the user clicks and drags the mouse to a size he/she wants, and lets go, filling in a rectangle based on the choice from the JComboBox.

basic layout

What I have implemented is MouseListener and MouseMotionListener to track the location of the mouse, and draw a rectangle based on where the user first clicked, to where it was let go.

When the user clicks and drags (but does not let go), there is a drawRect() but not a fillRect() (as in, the rectangle does not fill – only when the user releases the mouse does the rectangle fill with the color).

This class creates a Rect object that has another part in the constructor, which is the color that is selected (which is determined in the ColorListener class below).

This is where I included my instance variables, and everything is instantiated in the constructor below:

private ArrayList<Rect> rectList;
private Color currentColor;
private Canvas canvas;
private JPanel controlPanel;
private JButton undo, erase;
private JComboBox comboBox;
private int xStart, yStart, xEnd, yEnd;
private Graphics page;
private Point pt = null;
private PointListener pointListener;
private ColorListener colorListener;

public WholePanel()
{
    // here we use black to draw a rectangle
    currentColor = Color.black;
    pointListener = new PointListener();
    addMouseListener(pointListener);
    addMouseMotionListener(pointListener);

    String[] listOfColors = {"black", "red", "blue", "green", "orange"};

    comboBox = new JComboBox(listOfColors);
    comboBox.setSelectedIndex(0);

    rectList = new ArrayList<Rect>();
    controlPanel = new JPanel(new GridLayout(1,3));

    undo = new JButton("Undo");
    erase = new JButton("Erase");
    controlPanel.add(comboBox);
    controlPanel.add(undo);
    controlPanel.add(erase);
    undo.addActionListener(new ButtonListener());
    erase.addActionListener(new ButtonListener());

    canvas = new Canvas();

    JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, controlPanel, canvas);

    setLayout(new BorderLayout());
    add(sp);
}

The Rect class can create a Rect object used later.

public class Rect
{
private int x1, y1, width1, height1;
private Color color1;

public Rect(int x, int y, int width, int height, Color color)
{
    x1 = x;
    y1 = y;
    width1 = width;
    height1 = height;
    color1 = color;
}

public void draw(Graphics page)
{
    page.setColor(color1);
    page.drawRect(x1,y1,width1,height1);
}
}

The Canvas class creates the space that allows for drawing of an object.

private class Canvas extends JPanel
{

    public void paintComponent(Graphics page)
    {
        super.paintComponent(page);
        setBackground(Color.white);
        if (pt != null)
        {
            Rect rect = new Rect(xStart, yStart, xEnd-xStart, yEnd-yStart, currentColor);
            rect.draw(page);
        }
    }
}

The PointListener class finds all the points that are there, as in where the user clicks, to where the user drags, and also to where the user releases.

private class PointListener implements MouseListener, MouseMotionListener
{
    public void mousePressed(MouseEvent event)
    {
        pt = event.getPoint();
        xStart = pt.x;
        yStart = pt.y;
    }
    public void mouseReleased(MouseEvent event)
    {
        pt = event.getPoint();
        if (pt != null)
        {
            xEnd = pt.x;
            yEnd = pt.y;
            page.fillRect(xStart, yStart, xEnd-xStart, yEnd-yStart);
        }
    }
    public void mouseClicked(MouseEvent event) {}
    public void mouseEntered(MouseEvent event) {}
    public void mouseExited(MouseEvent event) {}
    public void mouseDragged(MouseEvent event)
    {
        pt = event.getPoint();
        if (pt != null)
        {
            xEnd = pt.x;
            yEnd = pt.y;
            Rect rect = new Rect(xStart, yStart, xEnd-xStart, yEnd-yStart, currentColor);
            rect.draw(page);
        }
        repaint();
    }
    public void mouseMoved(MouseEvent event) {}

}

ColorListener finds the type of object that is selected in the JComboBox determined in the main() method, and sets the currentColor to it (it is put back as the color in the Rect constructor above).

private class ColorListener implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        if (event.getSource().equals("black"))
        {
            currentColor = Color.black;
            comboBox.setSelectedIndex(0);
        }
        else if (event.getSource().equals("red"))
        {
            currentColor = Color.red;
            comboBox.setSelectedIndex(1);
        }
        else if (event.getSource().equals("blue"))
        {
            currentColor = Color.blue;
            comboBox.setSelectedIndex(2);
        }
        else if (event.getSource().equals("green"))
        {
            currentColor = Color.green;
            comboBox.setSelectedIndex(3);
        }
        else if (event.getSource().equals("orange"))
        {
            currentColor = Color.orange;
            comboBox.setSelectedIndex(4);
        }
    }
}

So what I am having trouble with is being able to draw it. I don’t see any flaws in logic in the program so far above, and nothing is being able to be drawn onto the Canvas (don’t worry about the JButtons Undo and Erase, as they are easy to work with with an ArrayList and remove() and clear() and things like that).

  • 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-30T08:31:24+00:00Added an answer on May 30, 2026 at 8:31 am

    Your program should have no Graphics field that is a class field (your page class field). I’m surprised your not seeing a NullPointException with the way you’re attempting to use it above in say your Mouse Listener class:

    public void mouseReleased(MouseEvent event)
    {
        pt = event.getPoint();
        if (pt != null)
        {
            xEnd = pt.x;
            yEnd = pt.y;
    
    
            // !!!! don't do this !!!!
            page.fillRect(xStart, yStart, xEnd-xStart, yEnd-yStart);
        }
    }
    

    Instead the Graphics object should only be obtained from the JVM, should only exist within the paintComponent method and should only be used within the same paintComponent method (exceptions being any methods called from with paintComponent that are passed this object and of course a Graphics object obtained from a BufferedImage). The MouseListener/MouseMotionListener should fill in your x-start, y-start, x-end, and y-end variables during mouseDragged, and then on mouseRelease these variables should be used to create a new Rect object that is placed in the rectList, and then repaint called.

    Then paintComponent should iterate through rectList, filling each Rect object it finds there.

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

Sidebar

Related Questions

Basically, something better than this: <input type=file name=myfile size=50> First of all, the browse
Basically what I am doing is giving a user a list of terms via
Basically I am writing a simple bit of code to increment every one second
Basically I have a fixed size IFRAME with overflow: auto . The IFRAME displays
Basically i'm trying to build a comment system. The user looks at a photo
Basically I have two models: User and Godfather. The godfather table has three columns:
Basically I want to create a user control in code behind, DataBind() it and
Basically what I want to do it this: a pdb file contains a location
Basically I have some code to check a specific directory to see if an
Basically I'm going to go a bit broad here and ask a few questions

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.