Buffering optimization of Java Program performance optimization

Source: Internet
Author: User

Pre-optimization code:

 Packagecom;Importjavax.swing.*;Importjava.awt.*;/*** Using Eclipse, right-click Run As,java applet running * Pre-Optimization effect: screen jitter and white light effects *@authorHu Jinshui **/ Public classNobuffermovingcircleextendsJAppletImplementsRunnable {Image screenimage=NULL;    Thread thread; intx = 5; intmove = 1;  Public voidinit () {screenimage= CreateImage (230,160); }     Public voidstart () {if(Thread = =NULL) {Thread=NewThread ( This);        Thread.Start (); }} @Override Public voidrun () {Try{             while(true) {x+=move; if((x > 105) | | (X < 5) {Move*=-1;                } repaint (); Thread.Sleep (10); }        }Catch(Exception e) {}} Public voiddrawcircle (Graphics GC) {graphics2d g=(GRAPHICS2D) GC;        G.setcolor (Color.green); G.fillrect (0,0,200,100);        G.setcolor (color.red); G.filloval (x,5,90,90); }     Public voidPaint (Graphics g) {G.setcolor (color.white); G.fillrect (0,0,200,100);    Drawcircle (g); }}

Optimized code:

 Packagecom;ImportJava.awt.Color;ImportJava.awt.Graphics;/*** No screen jitter or white light effect when running with Java applet * Optimized@authorHu Jinshui **/ Public classBuffermovingcircleextendsnobuffermovingcircle{Graphics DoubleBuffer=NULL;//buffers         Public voidinit () {Super. Init (); DoubleBuffer=Screenimage.getgraphics (); }         Public voidPaint (Graphics g) {//use buffer to optimize the original paint () methodDoublebuffer.setcolor (Color.White);//draw in memory firstDoublebuffer.fillrect (0, 0, 200, 100);        Drawcircle (DoubleBuffer); G.drawimage (Screenimage,0, 0, This);//display the buffer once.    }}

Buffering optimization of Java Program performance optimization

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.