Program to draw rectangle on each mouse click and erase priviouse rectangles

Program to draw rectangle on each mouse click and erase priviouse rectangles

Program to draw rectangle on each mouse click and erase previous rectangle on next mouse click

View Answers

May 24, 2011 at 3:26 PM

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

public class DrawRectangleOnMouseClick extends JPanel{

     MouseHandler mouseHandler = new MouseHandler();
     Point p1 = new Point(0, 0);
     Point p2 = new Point(0, 0);
     boolean drawing;

    public DrawRectangleOnMouseClick(){
        this.setPreferredSize(new Dimension(500, 400));
        this.addMouseListener(mouseHandler);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawRect(p1.x, p1.y, p2.x, p2.y);
    }

    private class MouseHandler extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            drawing = true;
            p1 = e.getPoint();
            p2 = p1;
            repaint();
        }

        public void mouseReleased(MouseEvent e) {
            drawing = false;
            p2 = e.getPoint();
            repaint();
        }
    }

    public static void main(String[] args) {
        JFrame f = new JFrame("Draw Rectangle On Mouse Click");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new DrawRectangleOnMouseClick());
        f.pack();
        f.setVisible(true);
    }
}









Related Tutorials/Questions & Answers:
Program to draw rectangle on each mouse click and erase priviouse rectangles
Program to draw rectangle on each mouse click and erase priviouse rectangles  Program to draw rectangle on each mouse click and erase previous rectangle on next mouse click
Java Swing: Draw rectangle on mouse click
Java Swing: Draw rectangle on mouse click In this tutorial, you will learn how to draw a rectangle on mouse click. Sometimes, there is a need of mouse clicks...(String[] args) { JFrame f = new JFrame("Draw Rectangle On Mouse Click
Advertisements
how to draw lines,circles, rectangles on JSP.
how to draw lines,circles, rectangles on JSP.  how to draw lines,circles, rectangles on JSP. Kindly provide solutions asap
PHP GD Draw Rectangle
ImageDestroy($im); ?> After running the program you will get the following
Draw Ellipse in Rectangle
Draw Ellipse in Rectangle     ... the rectangle. To draw an Ellipse inside the rectangle, we have defined two classes... a rectangle and a circle respectively. ADS_TO_REPLACE_1 By using draw() method
how to draw lines,circles, rectangles on JSP (using Java Graphics)
how to draw lines,circles, rectangles on JSP (using Java Graphics)  how to draw lines,circles, rectangles on JSP (using Java Graphics)   Hello Anuj Try the Following Code :ADS_TO_REPLACE_1 image.jsp <%@ page
drawing rectangle in java/Creating a draw rectangle filled with black color function in Java for a grid
drawing rectangle in java/Creating a draw rectangle filled with black color function in Java for a grid  drawing rectangle in java/Creating a draw rectangle (filled with black color) function in Java for a grid
Draw a line in a JPanel with button click in Java
Draw a line in a JPanel with button click in Java  Draw a line in a JPanel with button click in Java
Java gui program for drawing rectangle and circle
Java gui program for drawing rectangle and circle   how to write java gui program for drawing rectangle and circle? there shoud be circle.... and the program must also show message dialog "this is a red circle" when click
four rectangle/image of the shape of the button.then Draw these on some x and y co-ordinated in j2me then how to draw ?
four rectangle/image of the shape of the button.then Draw these on some x and y co-ordinated in j2me then how to draw ?  hi, i have button use in j2me. then use a imaga for lick a button. then process is four rectangle/image
J2ME Draw Triangle, Rectangle, Arc, Line Round Rectangle Example
J2ME Draw Triangle, Rectangle, Arc, Line Round Rectangle Example... tutorial, we are going to describe how to draw a triangle, rectangle, arc, line or a round rectangle in one small application. Although we have already explained
how to make multiple rectangles
of the array list in rectangles. as in each element should be in different rectangles... Output() { //Set JFrame title setTitle("Draw A Rectangle In JFrame...=x+25; int z=y+25; String thisCity= classlist.get(i); //draw
Rectangle
Rectangle  Could anybody help me on this problem, Write two Rectangle objects with the following properties: Rectangle1: Height 15 width 53 Y 25 X 15 Rectangle2: height 47 Width 60 Y 12 X 0 It's to be used four-argument
Mouse Drag and Drop
Mouse Drag and Drop       This section illustrates you how to drag and move mouse to draw... the rectangle and move, a number of figures will be drawn connected to each other
ModuleNotFoundError: No module named 'erase_exif'
ModuleNotFoundError: No module named 'erase_exif'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'erase_exif' How to remove the ModuleNotFoundError: No module named '
Rectangle Canvas MIDlet Example
that extends to the Canvas class to draw the given types of rectangle. In this figure first two rectangle draw from the solid and dotted line and the other two rectangle draw as round Rectangle shape. To draw these types of rectangle we are useing
Handling Mouse Clicks in Java
the mouse click event in the awt application. This program simply implements the left click event of the mouse. When you click "Click Me" button... Handling Mouse Clicks in Java     
for each
for each  Sir, is it possible to write bubble sort in java using for each loop ? or any two related nested loops using for each? Thanks in advance
A Program to find area and perimeter of Rectangle
A Program to find area and perimeter of Rectangle In this section we...; a rectangle. To find area and perimeter of a rectangle is a simple program... result. Program using function to find the area and perimeter of rectangle
Adapters Example
click of the mouse from top left corner, we get a rectangle on the release...; In the following program code, the adapter class has been used. This class has been used as an anonymous inner class to draw a rectangle within an applet
Draw Rectangle in J2ME
Draw Rectangle in J2ME      ... are used to draw a rectangle using J2ME language: g.setColor (255, ... it to create rectangle and to set the color of canvas and draw line or box.ADS
How to design a mouse
How to design a mouse Follow this example to learn how to design a mouse. New File: First take a new file (Ctrl + N). Use Pen Tool (P..., point your mouse pointer then select Direct selection tool (A key) and adjust
mouse events
mouse events  When i click a button i want to retrieve image and audio from database and play it. how can i do dis. I m using Netbeans IDE and mysql for database. pls do hel[p me
plotting of points inside a rectangle
inside a rectangle such that any two points are at a distance of at least 3 units from each other. I want all possible configurations that are possible.For example ,if I have a Rectangle of 20m by 14m then one possible placement of points
ModuleNotFoundError: No module named 'rectangle'
ModuleNotFoundError: No module named 'rectangle'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'rectangle' How to remove the ModuleNotFoundError: No module named '
Mouse Listener
Mouse Listener  how to move any object with help of mouse but not drag it in an applet
ModuleNotFoundError: No module named 'mouse'
ModuleNotFoundError: No module named 'mouse'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'mouse' How to remove the ModuleNotFoundError: No module named 'mouse'
Mouse Motion
Mouse Motion  Could you please send me the code of finding the (x,y) co-ordinates in a JFrame Compoenent..?? I need it urgently for my project
Write a java program that encrypt and decrypt some string by adding or removing 2 on each character.
Write a java program that encrypt and decrypt some string by adding or removing 2 on each character.  Write a java program that encrypt and decrypt some string by adding or removing 2 on each character. Example 1 : Please Enter
Java draw triangle draw method?
a rectangle and this works for a rectangle: public void draw(Graphics g...Java draw triangle draw method?  hi how would i construct the draw method for an triangle using the 'public void draw (graphics g ) method? im
How to draw a television
Shape: First draw a Rectangle shape with black color by using Rectangle tool (U... and rounded rectangle tool (U key) to draw a rounded rectangle shape.ADS_TO_REPLACE_5... How to draw a television Try to draw a television
Swap images oon mouse over and mouse out
Swap images oon mouse over and mouse out  I have multiple images...i want to swap each image on mouse out and mouse over..   <html> <script language="javascript" type="text/javascript"> if(document.images
Rectangle Image in Java
: In this program, you can also see how to used Rectangle2D for rectangle component... Rectangle Image in Java       Introduction In this section, you will learn how to create rectangle
ModuleNotFoundError: No module named 'rectangle-overlap'
ModuleNotFoundError: No module named 'rectangle-overlap'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'rectangle-overlap' How to remove the ModuleNotFoundError: No module
ModuleNotFoundError: No module named 'rectangle-packer'
ModuleNotFoundError: No module named 'rectangle-packer'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'rectangle-packer' How to remove the ModuleNotFoundError: No module
program to display frequency count of each word in a file using Hashmap, Hashset and streamtokenizer.plz help me out ..
program to display frequency count of each word in a file using Hashmap...; Program : by HashSet and StringTokenizer import java.io.BufferedReader... each word in fileContext Iterator<String> iterator
Updating rows who has same id with different values for each row from java program
Updating rows who has same id with different values for each row from java program  Here is my requirement, Following is a table which contains 5...) with "lalitha" as a value for sname column from java program. Same for records with sid
how to use mouse pressed
how to use mouse pressed   to action mouse clicked
ModuleNotFoundError: No module named 'mouse_droid'
ModuleNotFoundError: No module named 'mouse_droid'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'mouse_droid' How to remove the ModuleNotFoundError: No module named '
ModuleNotFoundError: No module named 'mouse_record'
ModuleNotFoundError: No module named 'mouse_record'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'mouse_record' How to remove the ModuleNotFoundError: No module named '
ModuleNotFoundError: No module named 'remarkable-mouse'
ModuleNotFoundError: No module named 'remarkable-mouse'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'remarkable-mouse' How to remove the ModuleNotFoundError: No module
Image swap on mouse over and mouse move
Image swap on mouse over and mouse move  i tried the code , sent form u...But if there are 50 images and..i want swap images on mousse over and mouse out, then the code becomes very lengthy...so please give the code for multiple
Image swap on mouse over and mouse move
Image swap on mouse over and mouse move  i tried the code , sent form u...But if there are 50 images and..i want swap images on mousse over and mouse out, then the code becomes very lengthy...so please give the code for multiple
program
in rectangles or circles wat ever it may be. the theme of program is to show algorithm working through graphical representations like circles,rectangles,colored...program  no i want a program that shows how merge sort algorithm
How to calculate area of rectangle
that is enclosed by the rectangle object. In the program coordinate of space its.... This program also provide the implementation of rectangle values and illustration... In this program explain the static method how to display the rectangle values. First
print a rectangle - Java Beginners
simple while loop?Assuming that the length n width of the rectangle is given...("Give me the size of each triangle : "); int size = Integer.parseInt(n.readLine()); System.out.print("The number of triangles you want to output in each row
4D Mouse
the whole mouse and has to click on the desired point or user can also use... 4D Mouse       Here... mouse is an advanced species of traditional 2D mouse that can move only horizontal
mouse over effects in css
mouse over effects in css  I have written a mouse over effect in CSS. But it is not working in HTML. Can any one suggest me? Thanks
ModuleNotFoundError: No module named 'draw'
ModuleNotFoundError: No module named 'draw'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'draw' How to remove the ModuleNotFoundError: No module named 'draw' error
ModuleNotFoundError: No module named 'draw'
ModuleNotFoundError: No module named 'draw'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'draw' How to remove the ModuleNotFoundError: No module named 'draw' error

Ads