1: The dual-buffer solution is circulated above, but this will waste the memory. If this method is used in distributed applications, the consequences will be very serious.
2: The reason is that the current drawing is not displayed when repaint is triggered.
Import java. AWT. borderlayout;
Import java. AWT. color;
Import java. AWT. graphics;
Import java. AWT. graphics2d;
Import java. AWT. Point;
Import java. AWT. event. mouseadapter;
Import java. AWT. event. mouseevent;
Import java. AWT. event. mousemotionadapter;
Import java. AWT. Geom. line2d;
Import java. util. vector;
Import javax. Swing. jframe;
Import javax. Swing. jpanel;
Class linepanel extends jpanel {
Private Static final long serialversionuid = 1l;
Private vector <line2d> lines = new vector <line2d> (); // stores all rows on the canvas.
Private line2d line = new line2d. Double ();
Private line2d cur = new line2d. Double (); // a straight line from the start point to the current cursor, temporary
Public linepanel (){
Super ();
This. setbackground (new color (255,255,255 ));
}
Public void setlines (vector <line2d> lines ){
This. Lines = lines;
}
Public void setcur (line2d cur ){
This. cur = cur;
}
@ Override
Public void paint (Graphics g ){
Super. Paint (g );
Graphics2d g2d = (graphics2d) g;
For (line2d l2d: lines) {// draw the finished graph that shows the storage
Line = l2d;
G2d. Draw (line );
}
I
F (C
Ur! = NULL) {// draw a temporary drawing. If this step is not performed, the drawing will not be smooth or flickering.
G2d. Draw (cur );
}
}
}
Public class drawline extends jframe {
Private Static final long serialversionuid = 1l;
Private linepanel panel;
Private vector <line2d> lines;
Private point start;
Public drawline (){
Panel = new linepanel ();
Lines = new vector <line2d> ();
This. setbounds (100,200,400,400 );
This. setlayout (New borderlayout ());
This. Add (panel, borderlayout. center );
This. setpanellistener ();
This. setdefaclocloseoperation (jframe. exit_on_close );
This. setvisible (true );
}
Private void setpanellistener (){
Panel. addmouselistener (New mouseadapter (){
@ Override
Public void mousepressed (mouseevent e ){
Start = new point (E. getx (), E. Gety (); // stores the starting coordinate points when you press the mouse.
}
@ Override
Public void mousereleased (mouseevent e ){
Line2d line = new line2d. Double (start. X, start. Y, E. getx (), E. Gety ());
Lines. Add (line); // stores the finished drawing because the drawing is definitely over when the mouse is released.
Panel. setlines (lines );
Line = NULL;
}
});
Panel. addmousemotionlistener (New mousemotionadapter (){
@ Override
Public void mousedragged (mouseevent e ){
Panel. setcur (New line2d. Double (start. X, start. Y, E. getx (), E. Gety ()));
// During the Drag and Drop Process, the image is excessive and cannot be stored in the vector. Otherwise, it will be a great waste of resources, so it should be processed separately.
Panel. repaint ();
}
});
}
Public static void main (string [] ARGs ){
New drawline ();
}
}