Application of Double Buffering in drawing board Program (2)

Source: Internet
Author: User
Tags gety

/File 14e. T.

2. Use double buffering to draw various images

In an artboard program, users should be able to draw various graphics with a paint brush. In addition to freehand, they should also be able to draw straight lines, cubes, and ovans. taking a straight line as an example, we all know that a straight line is displayed on the canvas only after the mouse button is released. while dragging the mouse, the display of a straight line in the canvas is constantly updated as the orientation of the mouse arrow changes. it is embodied in the program. This is a process of continuous erasure, display, re-erasure, and re-display. erased is a straight line between the previous arrow point and the start point. It shows a straight line between the current and the start points of the arrow. the display process is the responsibility of update_buffer, And the erased operation is the same as that of the previous section, which is completed by copy_from_offscreen_buf. in fact, the so-called erasure refers to restoring the canvas to an original moment.

This process is completed in the following modified drag-and-drop operation handler:

Public void mousedragged (mouseevent e ){
Graphics G = getgraphics ();
Copy_from_offscreen_buf (g );
X1 = E. getx ();
Y1 = E. Gety ();
Update_buffer (G, new drawitem (x0, y0, X1, Y1 ));
G. Dispose ();
}

Note: In this method, we didn't update the backend buffer. This is because when you drag the mouse, the line is displayed on the drawing board, but the line is not actually painted. so when should I buffer updates in the background? Obviously, when the mouse is released, we need to do this in the mousereleased method.

Public void mousereleased (mouseevent e ){
Graphics G = getgraphics ();
Copy_from_offscreen_buf (g );
X1 = E. getx ();
Y1 = E. Gety ();
Update_buffer (G, new drawitem (x0, y0, X1, Y1 ));
Update_buffer (off_screen_gc, new drawitem (x0, y0, X1, Y1 ));
G. Dispose ();
}

We can see that the line drawn to the drawing board is final only when the mouse is released, so that we can back up this line to the buffer zone.

The following is the complete whiteboard. Java program after the upgrade.

//: Whiteboard. Java

Import java. AWT .*;
Import java. AWT. event .*;

Public class whiteboard extends canvas implements mousemotionlistener, mouselistener {

Final Static int default_boardwidth = 700;
Final Static int default_boardheight = 400;
Int x0, y0, X1, Y1;

Whiteboard (wbapplet wbapplet1 ){
Parent = wbapplet1;
Off_screen_buf = parent. createimage (default_boardwidth, default_boardheight );
Off_screen_gc = off_screen_buf.getgraphics ();
Addmousemotionlistener (this );
Addmouselistener (this );
Draw_mode = 2;
}

Synchronized public void update_buffer (Graphics g, drawitem data ){
G. drawline (data. x0, Data. y0, Data. X1, Data. Y1 );
}

Public void mousemoved (mouseevent e ){}
Public void mousereleased (mouseevent e ){
Switch (draw_mode ){
Case 2:
Graphics G = getgraphics ();
Copy_from_offscreen_buf (g );
X1 = E. getx ();
Y1 = E. Gety ();
Update_buffer (G, new drawitem (x0, y0, X1, Y1 ));
Update_buffer (off_screen_gc, new drawitem (x0, y0, X1, Y1 ));
G. Dispose ();
}

}
Public void mouseentered (mouseevent e ){}
Public void mouseexited (mouseevent e ){}
Public void mouseclicked (mouseevent e ){}

Public void mousedragged (mouseevent e ){
Switch (draw_mode ){
Case 1:
X1 = E. getx ();
Y1 = E. Gety ();
Graphics G = getgraphics ();
Update_buffer (G, new drawitem (x0, y0, X1, Y1 ));
Update_buffer (off_screen_gc, new drawitem (x0, y0, X1, Y1 ));
G. Dispose ();
X0 = x1;
Y0 = Y1;
Break;
Case 2:
Graphics G1 = getgraphics ();
Copy_from_offscreen_buf (G1 );
X1 = E. getx ();
Y1 = E. Gety ();
Update_buffer (G1, new drawitem (x0, y0, X1, Y1 ));
G1.dispose ();
}
}

Public void mousepressed (mouseevent e ){
X0 = E. getx ();
Y0 = E. Gety ();
}

Public void paint (Graphics g ){
Copy_from_offscreen_buf (g );
}

Void copy_from_offscreen_buf (Graphics g ){
If (G! = NULL)
G. drawimage (off_screen_buf, 0, 0, null );
}

Private int draw_mode;
Private image off_screen_buf;
Private graphics off_screen_gc;
Wbapplet parent;
 
}

Class drawitem {
Drawitem (INT x0, int y0, int X1, int Y1 ){
This. X0 = x0;
This. Y0 = y0;
This. X1 = x1;
This. Y1 = Y1;
}
Int x0;
Int y0;
Int x1;
Int Y1;
}

///:~

Note that a new private variable draw_mode is created in this program to store the code of the drawing mode. here, we use 1 to represent free painting, and 2 to represent a straight line. defining the initial value for draw_mode In the constructor makes it easy to draw different types of images. In the above program, we define 2. If the value is 1, then return to the free painting mode. in fact, we should constantly expand and improve the program on such a framework.

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.