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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:35:20+00:00 2026-05-12T00:35:20+00:00

I have my program that can draw rectangles. I have two problems I can’t

  • 0

I have my program that can draw rectangles. I have two problems I can’t solve. After I draw the rectangle it won’t stay. The only code I have that clears the canvas in under paint, repaint is only called on mouse drag. Why when I mouse release or mouse move does my canvas clear. The second thing isn’t as much a problem, but something I can’t figure out, when either the height or width of my rectangle is negative the rectangle is filled in black.

package pracpapp2;


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MouseTracker4July extends JFrame
   implements MouseListener, MouseMotionListener {


  private static final long serialVersionUID = 1L;
  private JLabel mousePosition;
  int x, y;
  int x1, x2, y1, y2;
  int w, h;
  private JLabel recStart;
  private JLabel recStop;
  private JLabel cords;
   // set up GUI and register mouse event handlers
   public MouseTracker4July()
   {
      super( "Rectangle Drawer" );

      mousePosition = new JLabel();
      mousePosition.setHorizontalAlignment(SwingConstants.CENTER);
      getContentPane().add( mousePosition, BorderLayout.CENTER );


      JLabel text1 = new JLabel();
      text1.setText( "At the center the mouse pointer's coordinates will be displayed." );
      getContentPane().add( text1, BorderLayout.SOUTH );

      recStart = new JLabel();
      getContentPane().add(recStart, BorderLayout.WEST);

      recStop = new JLabel();
      getContentPane().add(recStop, BorderLayout.EAST);

      cords = new JLabel();
      getContentPane().add(cords, BorderLayout.NORTH);


      addMouseListener( this );        // listens for own mouse and
      addMouseMotionListener( this );  // mouse-motion events

      setSize( 800, 600 );
      setVisible( true );
   }

   // MouseListener event handlers
   // handle event when mouse released immediately after press
   public void mouseClicked( MouseEvent event )
   {
      mousePosition.setText( "Clicked at [" + event.getX() +
         ", " + event.getY() + "]" );
   }

   // handle event when mouse pressed
   public void mousePressed( MouseEvent event )
   {

      mousePosition.setText( "Pressed at [" +(x1 = event.getX()) +
         ", " + (y1 = event.getY()) + "]" );

      recStart.setText( "Start:  [" + x1 +
         ", " + y1 + "]" );
   }

   // handle event when mouse released after dragging
   public void mouseReleased( MouseEvent event )
   {
     mousePosition.setText( "Released at [" +(x2 = event.getX()) +
         ", " + (y2 = event.getY()) + "]" );

     recStop.setText( "End:  [" + x2 +
         ", " + y2 + "]" );

   }

   // handle event when mouse enters area
   public void mouseEntered( MouseEvent event )
   {
      mousePosition.setText( "Mouse entered at [" + event.getX() +
         ", " + event.getY() + "]" );
   }

   // handle event when mouse exits area
   public void mouseExited( MouseEvent event )
   {
      mousePosition.setText( "Mouse outside window" );
   }

   // MouseMotionListener event handlers
   // handle event when user drags mouse with button pressed
   public void mouseDragged( MouseEvent event )
   {
      mousePosition.setText( "Dragged at [" + (x = event.getX()) + 
         ", " + (y = event.getY()) + "]" );
      // call repaint which calls paint
      repaint();

   }

   // handle event when user moves mouse
   public void mouseMoved( MouseEvent event )
   {
      mousePosition.setText( "Moved at [" + event.getX() +
         ", " + event.getY() + "]" );
   }

   public void paint(Graphics g)
   {
      super.paint(g); // clear the frame surface
      g.drawString("Start Rec Here", x1, y1);
      g.drawString("End Rec Here", x, y);

      w = x1 - x;
      h = y1 - y;
      w = w * -1;
      h = h * -1;

      g.drawRect(x1, y1, w, h);

      cords.setText( "w = " + w + ", h = " + h);
   }

   public static void main( String args[] )
   { 
      MouseTracker4July application = new MouseTracker4July();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   }

} // end class MouseTracker
  • 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-12T00:35:20+00:00Added an answer on May 12, 2026 at 12:35 am

    Ok, after re-reading your question it seems you could care less to have multiple rectangles 🙂

    Here is a solution with only one at a time (which is close to what you had to begin with):

    import java.awt.BorderLayout;
    import java.awt.Graphics;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingConstants;
    
    public class MouseTracker4July extends JFrame implements MouseListener, MouseMotionListener {
    
        private static final long serialVersionUID = 1L;
        private final JLabel mousePosition;
        int x1, x2, y1, y2;
        int x, y, w, h;
        private final JLabel recStart;
        private final JLabel recStop;
        private final JLabel cords; // set up GUI and register mouse event handlers
        boolean isNewRect = true;
    
        public MouseTracker4July() {
            super( "Rectangle Drawer" );
    
            this.mousePosition = new JLabel();
            this.mousePosition.setHorizontalAlignment( SwingConstants.CENTER );
            getContentPane().add( this.mousePosition, BorderLayout.CENTER );
    
            JLabel text1 = new JLabel();
            text1.setText( "At the center the mouse pointer's coordinates will be displayed." );
            getContentPane().add( text1, BorderLayout.SOUTH );
    
            this.recStart = new JLabel();
            getContentPane().add( this.recStart, BorderLayout.WEST );
    
            this.recStop = new JLabel();
            getContentPane().add( this.recStop, BorderLayout.EAST );
    
            this.cords = new JLabel();
            getContentPane().add( this.cords, BorderLayout.NORTH );
    
            addMouseListener( this ); // listens for own mouse and
            addMouseMotionListener( this ); // mouse-motion events
    
            setSize( 800, 600 );
            setVisible( true );
    
        }
    
    // MouseListener event handlers // handle event when mouse released immediately after press 
        public void mouseClicked( final MouseEvent event ) {
            this.mousePosition.setText( "Clicked at [" + event.getX() + ", " + event.getY() + "]" );
    
            repaint();
        }
    
    // handle event when mouse pressed 
        public void mousePressed( final MouseEvent event ) {
    
            this.mousePosition.setText( "Pressed at [" + ( this.x1 = event.getX() ) + ", " + ( this.y1 = event.getY() ) + "]" );
    
            this.recStart.setText( "Start:  [" + this.x1 + ", " + this.y1 + "]" );
    
            this.isNewRect = true;
    
            repaint();
        }
    
    // handle event when mouse released after dragging 
        public void mouseReleased( final MouseEvent event ) {
            this.mousePosition.setText( "Released at [" + ( this.x2 = event.getX() ) + ", " + ( this.y2 = event.getY() ) + "]" );
    
            this.recStop.setText( "End:  [" + this.x2 + ", " + this.y2 + "]" );
    
            repaint();
        }
    
    // handle event when mouse enters area 
        public void mouseEntered( final MouseEvent event ) {
            this.mousePosition.setText( "Mouse entered at [" + event.getX() + ", " + event.getY() + "]" );
            repaint();
        }
    
    // handle event when mouse exits area 
        public void mouseExited( final MouseEvent event ) {
            this.mousePosition.setText( "Mouse outside window" );
            repaint();
        }
    
    // MouseMotionListener event handlers // handle event when user drags mouse with button pressed 
        public void mouseDragged( final MouseEvent event ) {
            this.mousePosition.setText( "Dragged at [" + ( this.x2 = event.getX() ) + ", " + ( this.y2 = event.getY() ) + "]" ); // call repaint which calls paint repaint();
    
            this.isNewRect = false;
    
            repaint();
        }
    
    // handle event when user moves mouse 
        public void mouseMoved( final MouseEvent event ) {
            this.mousePosition.setText( "Moved at [" + event.getX() + ", " + event.getY() + "]" );
            repaint();
        }
    
        @Override
        public void paint( final Graphics g ) {
            super.paint( g ); // clear the frame surface 
            g.drawString( "Start Rec Here", this.x1, this.y1 );
            g.drawString( "End Rec Here", this.x2, this.y2 );
    
            int width = this.x1 - this.x2;
            int height = this.y1 - this.y2;
    
            this.w = Math.abs( width );
            this.h = Math.abs( height );
            this.x = width < 0 ? this.x1
                : this.x2;
            this.y = height < 0 ? this.y1
                : this.y2;
    
            if ( !this.isNewRect ) {
                g.drawRect( this.x, this.y, this.w, this.h );
            }
    
            this.cords.setText( "w = " + this.w + ", h = " + this.h );
    
        }
    
        public static void main( final String args[] ) {
            MouseTracker4July application = new MouseTracker4July();
            application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        }
    
    } // end class MouseTracker
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 209k
  • Answers 209k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Your best bet is probably to put a placeholder in… May 12, 2026 at 9:41 pm
  • Editorial Team
    Editorial Team added an answer class Program { static void Main(string[] args) { string path… May 12, 2026 at 9:41 pm
  • Editorial Team
    Editorial Team added an answer Problem is resolved, interestingly shutdown(1) was causing the problem, the… May 12, 2026 at 9:41 pm

Related Questions

I have my program that can draw rectangles. I have two problems I can't
I'm new to graphics programming. I'm trying to create a program that allows you
I am brand new in the world of Wpf, and am thinking it's time
I need to draw 3d projections and i am using opengl wrapper for JAVA.
I'm having a problem designing part of my program (not writing it, for once!).

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.