After the repaint method is rewritten in swing, the painting will flash or fail to be updated in time.

Source: Internet
Author: User
Tags gety

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 ();


}

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.