The Artima Developer Community
Sponsored Link

Java Answers Forum
How to use the functions in class Graphics to draw in JTextArea

2 replies on 1 page. Most recent reply: Jun 19, 2002 1:23 PM by trung vo

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
trung vo

Posts: 2
Nickname: trung
Registered: Jun, 2002

How to use the functions in class Graphics to draw in JTextArea Posted: Jun 18, 2002 6:34 PM
Reply to this message Reply
Advertisement
I want to use the functions such as drawrect(), drawString(),.. etc in class Graphics to draw in JTextArea. But I didn't know how to declare the variable (Graphics g) in my main program class Drawpaint () extends JFrame. I tried to declare
JTextArea output;
Graphics g = output.getGraphics();
g.drawString("Test",25,30);
but it didn't not work. Thanks in advance for helping


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
Reply to this message Reply
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;
		}
 
	}
}

trung vo

Posts: 2
Nickname: trung
Registered: Jun, 2002

Re: How to use the functions in class Graphics to draw in JTextArea Posted: Jun 19, 2002 1:23 PM
Reply to this message Reply
Thank you very much. Your code really help me to understand the problem.
Trung vo

Flat View: This topic has 2 replies on 1 page
Topic: jswing & a io rw dat file application ..... again Previous Topic   Next Topic Topic: PC and phone connectivity

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use