"Jege image generator" made by Java"

Source: Internet
Author: User


Import java. AWT .*;
Import java. util .*;
Import java. AWT. Geom .*;
Import java. AWT. image .*;
Import java. AWT. event .*;
Import java. Io .*;
Import javax. Swing .*;
Import com.sun.image.codec.jpeg .*;

Class Point
{
Int X, Y;
Point (int x, int y)
{
This. x = X;
This. Y = y;
}
}

Public class makejpeg extends canvas implements mousemotionlistener, mouselistener, actionlistener
{
Int x =-1, y =-1, eraser notification = 0, clear notification = 0;
Vector v = NULL; int n = 1;
Graphics2d ggg; // specifies the paint brush object that draws the image.
Bufferedimage image; // image object
Frame window;
Button save, color palette, eraser, clear, paint brush, screen acquisition, drawing graphics;
Color: Specifies the paint brush color;
Panel pcenter, psouth, pnorth;

Public makejpeg ()
{
Save = new button ("Save the drawn image or screen as a JPG file ");
Get screen = new button ("Get screen ");
Drawing Graphics = new button ("Drawing Graphics ");
Palette = new button ("Open the palette ");
Paint Brush = new button ("paint brush ");
Rubber = new button ("rubber ");
Clear = new button ("clear ");
Palette. addactionlistener (this );
Draw the image. addactionlistener (this );
Save. addactionlistener (this );
Paint Brush. addactionlistener (this );
Rubber. addactionlistener (this );
Clear. addactionlistener (this );
Get screen. addactionlistener (this );
Paint Color = new color (0, 0, 0 );
Addmousemotionlistener (this );
Addmouselistener (this );
V = new vector ();
Setbackground (color. White); // you can specify the canvas color.

// Create an image object with 210 pixels in width and height
Image = new bufferedimage (210,210, bufferedimage. type_int_rgb );
Ggg = image. creategraphics (); // create a paint brush object to be drawn in the image object
Rectangle2d rect = new rectangle2d. Double (210,210 );
Ggg. setcolor (getbackground ());
Ggg. Fill (rect); // set the background color of the image to the same as that of the canvas.
Window = new frame ("JPEG image generator ");
Pcenter = new Panel ();
Pcenter. setlayout (null );
Pcenter. Add (this );
Pcenter. setbackground (color. Gray );
This. setbounds (210,210,); // you can specify the size of the canvas to be the same as that of the image.
Window. Add (pcenter, borderlayout. center );
Pnorth = new Panel ();
Pnorth. Add (SAVE );
Pnorth. Add (Drawing Graphics );
Pnorth. Add (get screen );
Window. Add (pnorth, borderlayout. North );
Psouth = new Panel ();
Psouth. Add (palette );
Psouth. Add (rubber );
Psouth. Add (clear );
Psouth. Add (paint brush );
Window. Add (psouth, borderlayout. South );
Window. setvisible (true );
Window. addwindowlistener (New windowadapter ()
{
Public void windowclosing (windowevent E)
{
System. Exit (0 );
}
});
Window. setbounds (390,380, 80 );
Window. Validate (); // make sure that the components in the window are correctly displayed.
}
 
Public void paint (Graphics g) // specifies the stroke object for drawing the image on the canvas.
{
If (X! =-1 & Y! =-1 & eraser notification = 0 & clear notification = 0)
{
G. setcolor (paint brush color );
N = V. Size ();
For (INT I = 0; I <n-1; I ++)
{
Point P1 = (point) v. elementat (I );
Point P2 = (point) v. elementat (I + 1 );
G. drawline (p1.x, p1.y, p2.x, p2.y); // paint g in the canvas
Ggg. setcolor (G. getcolor ());
Ggg. drawline (p1.x, p1.y, p2.x, p2.y); // meanwhile, the paint brush ggg also draws images in the image.
}
}
Else if (eraser notification = 1 & clear notification = 0) // erase the image in the 4x4 size area
{
G. setcolor (getbackground ());
G. fillrect (X-2, y-2, 4, 4 );
Ggg. setcolor (getbackground ());
Ggg. fillrect (X-2, y-2, 4, 4 );
}
Else if (clear notification = 1 & eraser notification = 0) // clear the image in the canvas and Image
{
G. setcolor (getbackground ());
G. fillrect (0, 0, 200,200 );
Ggg. setcolor (getbackground ());
Ggg. fillrect (0, 0, 200,200 );
}

G. drawimage (image, 200,200, this); // draw the image object to the canvas.
}
Public void mousedragged (mouseevent E)
{
X = (INT) E. getx ();
Y = (INT) E. Gety ();
Point P = new point (x, y );
V. addelement (P );
Repaint ();
}
Public void mousemoved (mouseevent E)
{}
Public void mousepressed (mouseevent E)
{}
Public void mousereleased (mouseevent e) // clear the Image Vector when you open the mouse.
{
V. removeallelements ();
}
Public void mouseentered (mouseevent e ){}
Public void mouseexited (mouseevent e ){}
Public void mouseclicked (mouseevent e ){}

Public void Update (Graphics g)
{
{
Paint (g );
}
}
Public void actionreceivmed (actionevent E)
{
If (E. getsource () = rubber)
{
Eraser notification = 1;
Clear notification = 0;
}
Else if (E. getsource () = clear)
{
Clear notification = 1;
Eraser notification = 0;
Repaint ();
}
Else if (E. getsource () = paint brush)
{
Eraser notification = 0;
Clear notification = 0;
}

Else if (E. getsource () = save)
{
// The "save file" dialog box is displayed.
Filedialog savedialog = new filedialog (window, "Save image to JPG format", filedialog. Save );

// This dialog box is a mode dialog box. The program can continue to run only when a response is received.
Savedialog. setvisible (true );

// Select the "yes" or "cancel" button in the dialog box. The dialog box is automatically closed and the program continues to run.
If (savedialog. GetFile ()! = NULL)
// If yes is selected, GetFile () will return the file name
{
Try {
// The dialog box is responsible for naming JPEG files.
String filename = savedialog. GetFile ();
Fileoutputstream out = new fileoutputstream (filename );

// Obtain a jpeg Encoder
Required imageencoder encoder = required codec. createjpegencoder (out );

// Obtain the decoder encoding Parameters
Export encodeparam Param = encoder. getdefaultjpegencodeparam (image );

// Set the image quality to 100%
Param. setquality (1.0f, false );
Encoder. setincluencodeparam (PARAM );

// The encoder writes the image to the output stream out.
Encoder. encode (image );
Out. Close ();
}
Catch (exception ee)
{
}
}
}
Else if (E. getsource () = get screen)
{
Robot robot = NULL; // The robot object used to obtain the screen image
Try {
Robot = new robot ();
}
Catch (exception ER)
{
}
Rectangle screenrect = NULL; // The rectangle object used to determine the image size
Int width = gettoolkit (). getscreensize (). width; // obtain the screen width.
Int Height = gettoolkit (). getscreensize (). height; // get the screen height
Screenrect = new rectangle (0, 0, width, height );

Window. setvisible (false); // hide the current Java program window to obtain the screen image
This. Window. setvisible (false );
Image = robot. createscreencapture (screenrect); // Save the screen image to the image
Window. setvisible (true); // re-display the current program window
Repaint ();
}
Else if (E. getsource () = color palette) // select the painting color
{
Color tempcolor = jcolorchooser. showdialog (window, "color palette", paint brush color );
{
If (tempcolor! = NULL)
{
Paint Color = tempcolor;
Paint Brush. setforeground (paint color );
}
}
}
Else if (E. getsource () = drawing a graph)
{
Window. Dispose (); // release the window
This. Window. Dispose ();
Makejpeg canvas = new makejpeg (); // create a canvas object
}
}
Public static void main (string ARGs [])
{
New makejpeg ();
}
}

 

Then solve the eclipse Setting Problem
This issue occurs in eclipse settings. By default, these access-restricted APIs are set to error.
Windows-preferences-Java-complicer-errors/warnings
In the deprecated and restricted API, select forbidden references (access rules) as warning to compile and pass.

It's amazing to pass the test. Haha

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.