1. Paint (Graphics g)
In Java drawing, the most common use is paint (Graphics g) {... Content... } method to get the brush, and then use containers such as jpanel as the canvas, the content is rendered within the jframe, in many cases this approach is still very useful, the following example:
ImportJava.awt.Color; ImportJava.awt.Graphics; ImportJavax.swing.JFrame; ImportJavax.swing.JPanel; Public classTest//test is a custom main class name, typically uppercase{ Public Static voidMain (String args[]) {JFrame newframe=NewJFrame ("FunBox"); Newframe.setdefaultcloseoperation (Jframe.exit_on_close); //defines the action (required) when the JFrame is closed, effectively avoiding the problem of closing the current box body process in the backgroundNewframe.setsize (400, 400);//defining the related properties of JFrameNewframe.setlocation (200, 200); Newframe.setvisible (true); Newframe.add (NewFangkuai ());//add images that need to be rendered into the JFrame} } classFangkuaiextendsJPanel//Fangkuai New class name for custom{ Public voidPaint (Graphics g)//overriding the implementation Panit () method{g.setcolor (color.green); //(0,0) position to draw a 20*20 Green squareG.fillrect (0, 0, 20, 20); } }
2. Graphics g=getgraphics ()
Sometimes it takes a graphics object to do more (for example, you need to call the Graphics object in run () below) instead of using the paint (Graphics g) method, and then you have to get your own defined Graphics object to complete the requirements, with an example attached
ImportJava.applet.Applet; ImportJava.awt.Color; ImportJava.awt.Graphics; ImportJavax.swing.JFrame; ImportJavax.swing.JPanel; //The following is a box body applet Public class_001//_001 the main class name as a custom { Public Static voidMain (string[] args) {JFrame newframe=NewJFrame ("FunBox"); Newframe.setdefaultcloseoperation (Jframe.exit_on_close); //defines the action (required) when the JFrame is closed, effectively avoiding the problem of closing the current box body process in the backgroundNewframe.setsize (400, 400);//defining the related properties of JFrameNewframe.setlocation (200, 200); Newframe.setvisible (true); Newthread N1=NewNewthread ();//The thread runs, adding the image that needs to be rendered into the JFrameNewframe.add (N1); Thread T1=NewThread (N1); T1.start (); } } classNewthreadextendsJPanelImplementsRunnable//only one class can be inherited from a Java class, but multiple interfaces may be implemented, where Newthread is a custom new class name{Graphics g; //The Graphics object g is defined here; Private Static Final LongSerialversionuid = 1L; Public voidRun ()//process Run () method overrides{g=getgraphics ();//acquisition of the Graphics object G for(inti=0;i<100;) { Try{Thread.Sleep (2000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } This. Update (g);//Update () method to refresh the image so that the image does not overlapG.setcolor (Color.green);//Draw (0,0) Start moving the 20*20 Green Small BlockG.fillrect (i, I, 20, 20); I+=20; } } }
3. Summary
A simple call to the repaint () method can do a lot of Java drawing needs, but when it comes to getting a graphics object flexibly, you have to use Getgraphics () to get a little bit of your own Java drawing experience and hopefully get more reviews to improve.
<java>paint (Graphics g) and the two brushes of the graphics g=getgraphics () get