Charles Bell
Posts: 519
Nickname: charles
Registered: Feb, 2002
|
|
Re: How to use the functions in class Graphics to draw in JTextArea
|
Posted: Jun 18, 2002 8:13 PM
|
|
Hey, a JTextArea is not the normal graphics context for drawing. You should use a Canvas or a Panel or JPanel.
Applets and JApplets are subclasses of Panel. Here is some simple demo code I wrote for you doing line sketching on a Canvas.
/* SimpleLineSketch.java
* @author: Charles Bell
* @version: June 18, 2002
*/
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class SimpleLineSketch extends JApplet{
private boolean inAnApplet = true;
private boolean debug = false;
public static void main(String[] args){
SimpleLineSketch sketch = new SimpleLineSketch();
sketch.inAnApplet = false;
sketch.init();
}
public void init(){
getContentPane().add(new SketchPad());
if (!inAnApplet){
JFrame frame = new JFrame("SimpleLineSketch");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(this);
frame.pack();
frame.show();
}
}
class SketchPad extends Canvas implements MouseListener, MouseMotionListener{
private boolean isOnPad = false;
private Point startPoint = new Point(0,0);
private Point endPoint = new Point(0,0);
private Vector strokes = new Vector();
public SketchPad(){
setSize(Toolkit.getDefaultToolkit().getScreenSize());
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g){
ListIterator iterator = strokes.listIterator();
while (iterator.hasNext()){
BrushStroke nextBrushStroke = (BrushStroke)iterator.next();
g.drawLine(nextBrushStroke.start.x, nextBrushStroke.start.y,
nextBrushStroke.end.x, nextBrushStroke.end.y);
}
g.drawLine(startPoint.x, startPoint.y,
endPoint.x, endPoint.y);
}
/** Invoked when the mouse button has been clicked (pressed and released) on the Sketch Pad.
*/
public void mouseClicked(MouseEvent me){
if (debug) System.out.println("mouseClicked");
}
/** Invoked when the mouse enters the Sketch Pad.
*/
public void mouseEntered(MouseEvent me){
if (debug) System.out.println("mouseEntered");
isOnPad = true;
}
/** Invoked when the mouse exits a componentthe Sketch Pad.
*/
public void mouseExited(MouseEvent me){
if (debug) System.out.println("mouseExited");
isOnPad = false;
}
/** Invoked when a mouse button has been pressed on the Sketch Pad.
*/
public void mousePressed(MouseEvent me){
if (debug) System.out.println("mousePressed at " + me.getPoint().toString());
startPoint = me.getPoint();
}
/** Invoked when a mouse button has been released on the Sketch Pad.
*/
public void mouseReleased(MouseEvent me){
if (debug) System.out.println("mouseReleased at " + me.getPoint().toString());
endPoint = me.getPoint();
strokes.add(new BrushStroke(startPoint, endPoint));
update(getGraphics());
}
/** Invoked when a mouse button is pressed on on the Sketch Pad and then dragged
*/
public void mouseDragged(MouseEvent me){
if (debug) System.out.println("mouseDragged");
endPoint = me.getPoint();
update(getGraphics());
}
/** Invoked when the mouse button has been moved onthe Sketch Pad (with no buttons down).
*/
public void mouseMoved(MouseEvent me){
if (debug) System.out.println("mouseMoved");
}
}
class BrushStroke{
Point start;
Point end;
public BrushStroke(Point start, Point end){
this.start = start;
this.end = end;
}
}
}
|
|