Hi everybody, I'm a spanish Java student, and I have a problem to solve: I have to add code to a programm that draws on a frame using the mouse. The thing is, the drawing disappears when you minimize or resize the window. My teacher has told me to create an array of lines where the drawing can be saved, and repaint it each time the Frame is maximized or resized. Could anyone help me? Thanks in advance These are the classes from the project:
public class gestorMovimientoRaton extends MouseMotionAdapter { ventGarabatos m_ventana; public gestorMovimientoRaton(ventGarabatos ventana) { m_ventana= ventana;
} public void mouseDragged(MouseEvent e) { /**@todo: Override this java.awt.event.MouseMotionAdapter method*/ //super.mouseDragged( e); m_ventana.getGraphics().setColor(Color.black); m_ventana.getGraphics().drawLine(m_ventana.xAnt,m_ventana.yAnt, e.getX(),e.getY()); m_ventana.xAnt=e.getX(); m_ventana.yAnt=e.getY(); } } //Another class package garabatos;
The ComponentAdapter class implements ComponentListener. You could create your own by extending it, and then overriding the componentResized method to cause an update of your frame with a call to update(Graphics g).
in your program:
frame.addComponentListener(new FrameSizeListener());
class FrameSizeListener extends ComponentAdapter {
componentResized(ComponentEvent e){
Component c = e.getComponent();
c.update(c.getGraphics());
}
}
Thanks for your information, Charles, I guess this could solve the problem, but I'm asked to create an array of lines(made up by points)where I can save the drawing, then, using a 'for' cycle, one can go trough it. Can you figure out a way to do it? I?d be vey grateful indeed. Thanks again for your friendly help
Creat a class called Line which consists of two points called start and end. Its job is to hold the data for a Line. Create new Line objects and add them to your array of lines.
import java.awt.*;
class Line{
private Point startPoint;
private Point endPoint;
public Line(Point startPoint, Point endPoint){
this.startPoint = startPoint;
this.endPoint = endPoint;
}
public Point getStartPoint(){
return startPoint;
}
public Point getEndPoint(){
return endPoint;
}
public String toString(){
return startPoint.toString() + endPoint.toString();
}
}
Here is some demo code I made a while back for someone else:
/* SimpleSketch.java
* @author: Charles Bell
* @version: June 23, 2002
*/
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
publicclass SimpleSketch extends JApplet{
privateboolean inAnApplet = true;
privateboolean debug = false;
publicstaticvoid main(String[] args){
SimpleSketch sketch = new SimpleSketch();
sketch.inAnApplet = false;
sketch.init();
}
publicvoid init(){
getContentPane().add(new SketchPad());
if (!inAnApplet){
JFrame frame = new JFrame("SimpleSketch");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(this);
frame.pack();
frame.show();
}
}
class SketchPad extends Canvas implements MouseListener, MouseMotionListener{
privateboolean isOnPad = false;
private Point startPoint = new Point(0,0);
private Point endPoint = new Point(0,0);
//private Vector strokes = new Vector();
private Vector shapes = new Vector();
public SketchPad(){
setSize(Toolkit.getDefaultToolkit().getScreenSize());
addMouseListener(this);
addMouseMotionListener(this);
}
publicvoid paint(Graphics g){
ListIterator shapes = shapes.listIterator();
while (iterator.hasNext()){
Object nextShape = iterator.next();
if (nextShape instance of BrushStroke){
nextBrushStroke = (BrushStroke) nextShape;
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.
*/
publicvoid mouseClicked(MouseEvent me){
if (debug) System.out.println("mouseClicked");
}
/** Invoked when the mouse enters the Sketch Pad.
*/
publicvoid mouseEntered(MouseEvent me){
if (debug) System.out.println("mouseEntered");
isOnPad = true;
}
/** Invoked when the mouse exits a componentthe Sketch Pad.
*/
publicvoid mouseExited(MouseEvent me){
if (debug) System.out.println("mouseExited");
isOnPad = false;
}
/** Invoked when a mouse button has been pressed on the Sketch Pad.
*/
publicvoid 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.
*/
publicvoid mouseReleased(MouseEvent me){
if (debug) System.out.println("mouseReleased at " + me.getPoint().toString());
endPoint = me.getPoint();
shapes.add(new BrushStroke(startPoint, endPoint));
update(getGraphics());
}
/** Invoked when a mouse button is pressed on on the Sketch Pad and then dragged
*/
publicvoid 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).
*/
publicvoid 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;
}
}
}