Import javax. Swing .*;
Import javax. Swing. filechooser. filenameextensionfilter;
Import java. AWT .*;
Import java. AWT. event .*;
Import java. AWT. image. bufferedimage;
Import javax. ImageIO. ImageIO; // This class enables and saves images.
Public class huatu extends jpanel
Implements mouselistener, mousemotionlistener, actionlistener
{
Private jframe F, F1;
Private int xbegin = 0, ybegin = 0, xend = 0, yend = 0; // coordinates of the start and end points
Private jbutton butline, butrect, butoval, butpen, buteraser; // line, square, circle
Private jtoolbar TB;
Private int I = 0; // 0, line; 1, square; 2, circle; 3, pen;
// Save the final image and use the buffered image.
Private bufferedimage Bi;
Private graphics GG; // image-specific paint brush
Private jfilechooser jfc;
Private jbutton butsave, butopen, butabout;
Private jbutton butcolor; // select the color
Private color mycolor;
Private Label label1;
Public huatu ()
{
// Bufferedimage. type_int_rgb standard three-color image type
Bi = new bufferedimage
(800,600, bufferedimage. type_int_rgb );
F = new jframe ("yizero graphic Board ");
Butline = new jbutton ("\");
Butrect = new jbutton ("□ ");
Butoval = new jbutton ("○ ");
Butabout = new jbutton ("about ");
Butsave = new jbutton ("save ");
Butopen = new jbutton ("open ");
TB = new jtoolbar ();
Jfc = new jfilechooser ();
Filenameextensionfilter filter = new filenameextensionfilter ("jpg", "jpg ");
Jfc. setfilefilter (filter );
Butcolor = new jbutton ("");
Mycolor = color. Black; // The default color is black.
Butcolor. setbackground (color. Red );
// Because the cached image is just created, the default color is black.
// Obtain the image paint brush
Gg = Bi. getgraphics ();
GG. setcolor (color. White); // adjust the paint brush to white.
GG. fillrect (800,600,); // whitelist the image
GG. setcolor (color. Black); // change the paint brush to black.
TB. Add (butline );
TB. Add (butrect );
TB. Add (butoval );
TB. Add (butopen );
TB. Add (butsave );
TB. Add (butcolor );
TB. Add (butabout );
Butline. addactionlistener (this );
Butrect. addactionlistener (this );
Butoval. addactionlistener (this );
Butabout. addmouselistener (New mouseadapter (){
Public void mouseclicked (mouseevent e) {about_me ();}
});
Butopen. addactionlistener (this );
Butsave. addactionlistener (this );
Butcolor. addactionlistener (this );
This. setlayout (New borderlayout ());
This. Add (TB, borderlayout. North );
This. addmouselistener (this );
This. addmousemotionlistener (this );
F. Add (this );
F. setbounds (100,100,800,600 );
F. setvisible (true );
F1 = new jframe ("about Author ");
Setlayout (New flowlayout (flowlayout. Center, 20, 20 ));
F1.add (new label ("this program is written by yizero. If you have any questions, contact yizero via: yizero@qq.com.com "));
F1.setlayout (New gridlayout (1, 1 ));
F1.setfont (new font ("", 15, 15 ));
F1.pack ();
F1.setbounds (170,250,670,100 );
F1.setresizable (false );
F1.show ();
F1.setvisible (false );
F1.addwindowlistener (New java. AWT. event. windowadapter (){
Public void windowclosing (Java. AWT. event. javaswevent E)
{
F1.setvisible (false );
}
});
}
Public void about_me ()
{
F1.setvisible (true );
}
Public void paintcomponent (Graphics g)
{
// Set the paint brush color
G. setcolor (mycolor );
// Manually force clear the Panel
Super. paintcomponent (g );
// In order to see the final effect, the buffered image is also painted on the panel.
G. drawimage (Bi, 0, 0, null );
If (I = 0) // line
G. drawline (xbegin, ybegin, xend, yend );
Else // other figures need to determine the start point and width and height
{
Int X, Y, width, height;
If (xbegin> xend) x = xend;
Else x = xbegin;
If (ybegin> yend) y = yend;
Else y = ybegin;
Width = math. Abs (xbegin-xend); // ABS returns the absolute value.
Height = math. Abs (ybegin-yend );
If (I = 1) // rect
G. drawrect (X, Y, width, height );
Else if (I = 2) // oval
G. drawoval (X, Y, width, height );
}
}
Public static void main (string ARGs [])
{
New huatu ();
}
Public void mouseclicked (mouseevent e ){
}
Public void mousepressed (mouseevent E)
{// When you press the mouse, record the coordinates of the start point
This. xbegin = E. getx ();
This. ybegin = E. Gety ();
}
Public void mousereleased (mouseevent E)
{// Release the mouse to confirm that the final image needs to be saved on the buffered Image
If (I = 0)
GG. drawline (xbegin, ybegin, xend, yend );
Else // other figures need to determine the start point and width and height
{
Int X, Y, width, height;
If (xbegin> xend) x = xend;
Else x = xbegin;
If (ybegin> yend) y = yend;
Else y = ybegin;
Width = math. Abs (xbegin-xend); // ABS returns the absolute value.
Height = math. Abs (ybegin-yend );
If (I = 1) // rect
GG. drawrect (X, Y, width, height );
Else if (I = 2) // oval
GG. drawoval (X, Y, width, height );
}
}
Public void mousedragged (mouseevent E)
{// Record the end point when dragging
This. xend = E. getx ();
This. yend = E. Gety ();
// You need to re-paint the Panel each time you drag to display the most image.
This. repaint ();
}
Public void mouseentered (mouseevent e ){
}
Public void mouseexited (mouseevent e ){
}
Public void mousemoved (mouseevent e ){
}
Public void actionreceivmed (actionevent e ){
If (E. getsource () = This. butline) // line
{
I = 0;
}
Else if (E. getsource () = This. butrect) //
{
I = 1;
}
Else if (E. getsource () = This. butoval) // circle
{
I = 2;
}
Else if (E. getsource () = This. butopen) // open
{
Int I = jfc. showopendialog (this. F );
If (I = jfilechooser. approve_option)
{
Try
{// Load the file image to the memory
Bi = ImageIO. Read (jfc. getselectedfile ());
// After the image is changed, the paint brush must be replaced with a new one.
Gg = Bi. getgraphics ();
// The paint brush is white by default.
GG. setcolor (mycolor );
// To prevent a messy image from being opened, process it.
This. xbegin = 100;
This. ybegin = 0;
This. xend = 0;
This. yend = 0;
This. repaint ();
}
Catch (exception ex)
{
Ex. printstacktrace ();
}
}
}
Else if (E. getsource () = This. butsave) // save
{
Int I = jfc. showsavedialog (this. F );
If (I = jfilechooser. approve_option)
{
Try
{// Save the image
// Parameter Buffer location of the image type file
ImageIO. Write (Bi, "jpg", jfc. getselectedfile ());
}
Catch (exception ex)
{
Ex. printstacktrace ();
}
}
}
Else if (E. getsource () = butcolor) // select the color
{
Mycolor = jcolorchooser. showdialog (F, "select your preferred color", mycolor );
GG. setcolor (mycolor );
}
}
}
Note: This Java applet is provided by the "music-Chariot" Source code. After my modifications, please contact me if you have any questions. Email: yizero@qq.com
See yizero by yizero.com