Home Answers Viewqa Java-Beginners Program to draw rectangle on each mouse click and erase priviouse rectangles

 
 


vaishali govind sali
Program to draw rectangle on each mouse click and erase priviouse rectangles
1 Answer(s)      2 years ago
Posted in : Java Beginners

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 Pages:
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...) { JFrame f = new JFrame("Draw Rectangle On Mouse Click
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
PHP GD Draw Rectangle
; After running the program you will get the following output
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
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
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... mouse pointer then select Direct selection tool (A key) and adjust the points
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
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.  
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     
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 : image.jsp <%@ page contentType="image
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.  By using draw() method of class
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
The Mouse
(and shouldn't care) whether this was from a mouse click on the button, or from... Java NotesThe Mouse The mouse is handled automatically by most components..., in a JPanel) and need to know where the user clicks, then you need to know about mouse
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
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
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
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
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
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
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
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 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... How to Calculate Area of Rectangle   
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
How to draw a television
Shape: First draw a Rectangle shape with black color by using Rectangle tool (U...; color and rounded rectangle tool (U key) to draw a rounded rectangle shape... How to draw a television Try to draw a television
Mouse Buttons, Modifier Keys
for each of the mouse buttons as well as modifier keys... Java NotesMouse Buttons, Modifier Keys Mouse Buttons. Java supports up to three mouse buttons. Even if your mouse doesn't have three separate
Rectangle Image in Java
for filling the color into the rectangle image. Here is the code of this program... Rectangle Image in Java       Introduction In this section, you will learn how to create rectangle
How to make a Rectangle type pdf
rectangle. The code of the program is given below... How to make a Rectangle type pdf       In this program we are going to tell you how we can
How to draw a house, draw a house, a house
; color and Rectangle tool (U key) to draw a rectangle shape like a window. Go... How to draw a house       Use this example to draw a house in the photoshop, it has been
Dynamic Line draw in JSp
Dynamic Line draw in JSp  In my application. I have one source selected from listbox and multiple targets selected from checkboxes. After submitting... multiple target. And each line should have dynamically link of other jsp page
Auto select cell on mouse hover using 'hover' mouse event
Auto select cell on mouse hover using 'hover' mouse event In this tutorial, we will discuss about how to highlight Table's cell on mouse hover using jQuery 'hover' mouse event. In this example, a table is given
MouseListener
Java NotesMouseListener This type of mouse listener is for events which typically don't happen very often -- a mouse button is pressed, released, or the mouse enters or leaves the area of the component with a listener. Here
How to draw pie chart in J2ME
How to draw pie chart in J2ME  Hi, I'm developing a program using J2ME and I want to draw a pie chart but it seems J2ME language hasn't available chart component like JFreeChart in Java! could any one help me how can I draw a pie
Count instances of each word
Count instances of each word  I am working on a Java Project... of the words preceded by the occurrence count. My program compiles and runs... number 1 of 1 program 2 test 2 that 1 the 1 this 1 to 1 total 1 using 1 verify 1
Change the user input to integer
of Rectangle class.  Here is the code of the program... each time before the compilation in the method itself. Now if we want to enter an integer value after the compilation of a program and force the JVM to ask
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
jQuery '.live' & '.die' Mouse events
; mouse event of jQuery. In this Example, we removes the 'click' event...jQuery '.live'  & '.die' Mouse events...;click not working</button> <button id="bind">Enable Click<
Draw a Flowchart
and set the size using Rectangle class.   To connect the boxes, we draw... Draw a Flowchart       This section illustrates you how to draw a Flowchart to compute
how to draw network with nodes - Java Beginners
how to draw network with nodes  how can i draw a network graph (game theory) with 3 nodes and each node display some information
'append' & 'fade out/in' on mouse hover
'append' & 'fade out/in' on mouse hover In this tutorial...; on mouse hover using jQuery. In this Example, a list is given , if we hover on first two element, "<<<" is appended on it, to show mouse
program
program  how to draw lines using JSP
PHP GD draw text
the program you will get the following output
Mouse Listener
Mouse Listener  how to move any object with help of mouse but not drag it in an applet
MouseTest
: mousedeme/MouseTest.java // Description: Main program/applet to demo mouse listeners..._lastClickedX = 0; // x coord of mouse click private int m_lastClickedY = 0; // y coord of mouse click private int m_lastMovedX = 0; // x coord of mouse
Draw Line in J2me
Draw Line in J2me       In this example we are going to show you how to draw a line using J2ME. Please... and class to draw a line.  Basically in J2ME, Canvas class is used to draw
SplitButton of dropdownbutton invisible when mouse clicking - Swing AWT
SplitButton of dropdownbutton invisible when mouse clicking  click the SplitButton of dropdownbutton, when dropdownmenu willbe displayed... the dropdownmenu when we click the dropdown button? //the code is below
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
PHP GD draw line
); ?> After running the program you will get the following output
program
program  Write a JSP Program, which displays a web page containing... enrolled in each course, new courses to be offered, eligibility criteria for taking admission in each course
program
program   An old-style movie theater has a simple profit program. Each customer pays $5 per ticket. Every performance costs the theater $20, plus $.50 per attendee. Develop the program calculateTotalProfit that consumes

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.