Entry
Java: How to write a simple mouse controlled line drawing program in Java?
Aug 24th, 2004 19:30
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 11 May 2002 - 04:40 pm ---------------------
Java: How to write a simple mouse controlled line drawing program in
Java?
---
Use the mouse events (e.g. drag, click, ...) to do something.
E.g. store a point.
---
By definition the screen is cleared all the time.
You repaint then your figure all the time, using the already stored
points.
The paint() method is by definition never called directly
by your methods, but via the repaint() method.
So most mouse event methods here call the repaint() method, which
calls in turn your paint() method.
In this paint() method you redraw the figure by drawing lines
using the already stored points in the array.
---
Undo is here only removing the last point and then repainting.
That shows the figure again without the last point.
---
You by definition also need to add some listener methods,
which 'listen' e.g. for keyboard and mouse events.
---
---
The next step would be to create areas (e.g. draw a rectangle with the
text
'change color') on your screen, which when you click
on this area
Like this:
if (
( xareamin[1] <= xmouse ) AND ( xmouse <= xareamax[1] )
AND
( yareamin[1] <= ymouse ) AND ( ymouse <= yareamax[1] )
)
// do something
(e.g. changing color, storing the figure, starting again, ...).
You have then started to create your own graphical editor.
---
---
Steps: Overview:
1. save and compile the below Java program
(e.g. javac SimpleGraphics.java)
2. this will create the file
SimpleGraphics.class
3. save the below .htm file, e.g. as
SimpleGraphics.htm
in the same directory
4. load that .htm file in your browser.
That will load the applet
5. you can then draw polygons.
-Dragging the mouse drags the line
-Right clicking the mouse to undo the last point
-Clicking saves the last point
---
---
Steps: Worked out:
--- cut here ---------------------------------------------------------
// save as 'SimpleGraphics.java'
//
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
//
public class SimpleGraphics extends Applet implements MouseListener,
MouseMotionListener {
//
int xmouseI = 0;
int ymouseI = 0;
int pointminT = 1;
int pointT = pointminT - 1;
int xI[] = new int [500];
int yI[] = new int [500];
String s = "";
//
public void init() {
xI[ 1 ] = xmouseI;
yI[ 1 ] = ymouseI;
addMouseListener( this );
addMouseMotionListener( this );
}
//
public void mouseClicked( MouseEvent m ) {
if ( ( m.getModifiers() & InputEvent.BUTTON3_MASK ) ==
InputEvent.BUTTON3_MASK ) {
s = "mouse right clicked";
pointT = pointT - 1;
if ( pointT <= pointminT ) {
pointT = pointminT;
xI[ 1 ] = xmouseI;
yI[ 1 ] = ymouseI;
}
}
else {
s = "mouse clicked";
}
repaint();
}
//
public void mouseEntered( MouseEvent m ) {
s = "mouse entered";
}
//
public void mouseExited( MouseEvent m ) {
s = "mouse exited";
}
//
public void mousePressed( MouseEvent m ) {
if ( ( m.getModifiers() & InputEvent.BUTTON3_MASK ) ==
InputEvent.BUTTON3_MASK ) {
s = "mouse right clicked";
pointT = pointT - 1;
if ( pointT <= pointminT ) {
pointT = pointminT;
xI[1] = xmouseI;
yI[1] = ymouseI;
}
}
else {
s = "mouse pressed";
}
xmouseI = m.getX();
ymouseI = m.getY();
repaint();
}
//
public void mouseReleased( MouseEvent m ) {
pointT = pointT + 1;
xI[ pointT ] = xmouseI;
yI[ pointT ] = ymouseI;
s = "mouse released";
repaint();
}
//
public void mouseDragged( MouseEvent m ) {
xmouseI = m.getX();
ymouseI = m.getY();
s = "mouse dragged";
repaint();
}
//
public void mouseMoved( MouseEvent m ) {
showStatus( "test" );
}
//
public void paint( Graphics g ) {
g.setColor( Color.blue );
g.drawString( s, xmouseI, ymouseI );
for ( int I = pointminT; I <= pointT - 1; I++ ) {
g.drawLine( xI[ I ], yI[ I ], xI[ I + 1 ], yI[ I + 1 ] );
}
g.drawLine( xI[ pointT ], yI[ pointT ], xmouseI, ymouseI );
}
}
--- cut here ---------------------------------------------------------
---
---
To run this applet, create an HTML file, and save it as
'SimpleGraphics.htm'
--- cut here ---------------------------------------------------------
<HTML>
<TITLE>
Simple Graphics Applet
</TITLE>
<APPLET
CODE="SimpleGraphics.class"
WIDTH="700"
HEIGHT="500"
>
</APPLET>
</HTML>
--- cut here ---------------------------------------------------------
---
---
Internet: see also:
---
[Internet: see also: http://www.joegrip.com: Java GUI]
---
---
Book: see also:
[book: see also: Schildt, Herbert - Java 2 / The complete reference
(3th edition) - ISBN 0-07-211976-4 - p. 655 'Handling mouse events'
[book: see also: Jamsa, Kris - Java Now! - ISBN 1-884133-30-4 - p.
144 'Understanding mouse operations']
----------------------------------------------------------------------