Simple Drawing Program (implement the rubber band function)

Source: Internet
Author: User

/**
* Author Zhao Xing
*/
Package homewoek;

Import java. AWT. borderlayout;
Import java. AWT. color;
Import java. AWT. container;
Import java. AWT. flowlayout;
Import java. AWT. Font;
Import java. AWT. graphics;
Import java. AWT. gridlayout;
Import java. AWT. event. actionevent;
Import java. AWT. event. actionlistener;
Import java. AWT. event. itemevent;
Import java. AWT. event. itemlistener;
Import java. AWT. event. mouseadapter;
Import java. AWT. event. mouseevent;
Import java. AWT. event. mousemotionadapter;

Import javax. Swing. jbutton;
Import javax. Swing. jcheckbox;
Import javax. Swing. jcolorchooser;
Import javax. Swing. jcombobox;
Import javax. Swing. jframe;
Import javax. Swing. jlabel;
Import javax. Swing. jpanel;
Import javax. Swing. jtextfield;

/**
* @ Author Administrator
*
*/
Public class drawpad extends jframe {
Private Static final long serialversionuid = 1l;

Private jbutton colorbutton, clearbutton;
Private jlabel statuslabel, stylejlabel, strokejlabel;
Private jcombobox shapebox, stylecombobox;
Private jtextfield inputfield, strokefield; // drop-down box
Private jcheckbox fillcheckbox; // whether to fill
Private jpanel toolpanel, drawpanel, wordjpanel, and northjpanel;
Private string namses [] = {"line", "Oval", "rect"}, fontnames [] = {
"Serif", "sansserif", "monospaced "};
Private Boolean fill = false; // fill flag
Private Boolean clear = false, word = false; // clear and text mark
Private Final int line = 0, oval = 1, rect = 2;
Private int shape = 0; // default line
Private color = color. Black; // The current color is black.
// Private int R, G, B; // Save the current color
Private int topx, topy, X, Y, height, width;
Private int size = 3; // font size
Private string = "", font = "serif ";

Public drawpad (){
Super ("Drawing ");

Shapebox = new jcombobox (namses );
Shapebox. setmaximumrowcount (3 );
Shapebox. additemlistener (New itemlistener (){
Public void itemstatechanged (itemevent event ){
Shape = shapebox. getselectedindex ();
}
});

Fillcheckbox = new jcheckbox ("fill ");
Fillcheckbox. additemlistener (New itemlistener (){
Public void itemstatechanged (itemevent event ){
If (event. getstatechange () = itemevent. Selected) // If selected
Fill = true; // fill
Else
Fill = false;
}
});

Colorbutton = new jbutton ("color ");
Colorbutton. addactionlistener (New actionlistener (){
Public void actionreceivmed (actionevent event ){
Choosecolor ();
}
});

Clearbutton = new jbutton ("clear ");
Clearbutton. addactionlistener (New actionlistener (){
Public void actionreceivmed (actionevent event ){
Clear ();
}
});

Toolpanel = new jpanel ();
Toolpanel. setlayout (New gridlayout (1, 4 ));
Toolpanel. Add (shapebox );
Toolpanel. Add (fillcheckbox );
Toolpanel. Add (colorbutton );
Toolpanel. Add (clearbutton );

Drawpanel = new jpanel ();
Drawpanel. setbackground (color. White );

Statuslabel = new jlabel ();

Inputfield = new jtextfield (10 );
Inputfield. addactionlistener (New actionlistener (){

@ Override
Public void actionreceivmed (actionevent event ){
// Todo auto-generated method stub
WORD = true; // input text
String = event. getactioncommand ();
}
});

Stylejlabel = new jlabel ("Word Style ");

Stylecombobox = new jcombobox (fontnames );
Stylecombobox. additemlistener (New itemlistener (){

@ Override
Public void itemstatechanged (itemevent event ){
// Todo auto-generated method stub
Font = fontnames [stylecombobox. getselectedindex ()]; // obtain the name of the selected font.
}
});

Strokejlabel = new jlabel ("stroke ");

Strokefield = new jtextfield (3 );
Strokefield. addactionlistener (New actionlistener (){

@ Override
Public void actionreceivmed (actionevent event ){
// Todo auto-generated method stub
Size = integer. parseint (event. getactioncommand ());
}
});

Wordjpanel = new jpanel ();
Wordjpanel. setlayout (New flowlayout ());
Wordjpanel. Add (inputfield );
Wordjpanel. Add (stylejlabel );
Wordjpanel. Add (stylecombobox );
Wordjpanel. Add (strokejlabel );
Wordjpanel. Add (strokefield );

Northjpanel = new jpanel ();
Northjpanel. setlayout (New gridlayout (2, 1 ));
Northjpanel. Add (toolpanel );
Northjpanel. Add (wordjpanel );

Container = getcontentpane ();
Container. Add (northjpanel, borderlayout. North );
Container. Add (drawpanel, borderlayout. center );
Container. Add (statuslabel, borderlayout. South );

Addmouselistener (New mousehandler (); // registers mouse and keyboard events
Addmousemotionlistener (New mousemotionhandler ());

Setsize (400,400 );
Setvisible (true );
}

Public void paint (Graphics g ){
Super. Paint (g); // key step. If not, a pile of lines will be generated.

If (Word ){
G. setfont (new font (font, Font. Plain, size); // set the font
G. drawstring (string, topx, topy );
WORD = false;
Return;
}

Width = math. Abs (topx-x );
Height = math. Abs (topy-y );

If (shape! = Line) {// determine the start point
Topx = math. Min (topx, X );
Topy = math. Min (topy, y );
}

If (shape! = Line & fill ){
Switch (SHAPE ){
Case oval:
G. setcolor (color );
G. filloval (topx, topy, width, height );
Break;
Case rect:
G. setcolor (color );
G. fillrect (topx, topy, width, height );
Break;
Default:
Break;
}
} Else {
Switch (SHAPE ){
Case line:
G. setcolor (color );
G. drawline (topx, topy, x, y );
Break;
Case oval:
G. setcolor (color );
G. drawoval (topx, topy, width, height );
Break;
Case rect:
G. setcolor (color );
G. drawrect (topx, topy, width, height );
Break;
Default:
Break;
}
}

If (clear ){
G. setcolor (color. White );
G. fillrect (0, 0, getsize (). Width, getsize (). Height );
}
}

Private class mousehandler extends mouseadapter {// listens to mouse clicks and releases
Public void mousepressed (mouseevent event ){
Topx = event. getx ();
Topy = event. Gety ();
}

Public void mousereleased (mouseevent event ){
X = event. getx ();
Y = event. Gety ();

Repaint (); // refresh
}
}

Private class mousemotionhandler extends mousemotionadapter {// listens for mouse movement
Public void mousemoved (mouseevent event ){
Statuslabel. settext ("(" + event. getx () + "," + event. Gety () + ")"
+ Fill );
}

Public void mousedragged (mouseevent event) {// produce the rubber band Effect
X = event. getx ();
Y = event. Gety ();

Repaint (); // refresh
}
}
 
Public void choosecolor (){
Color = jcolorchooser. showdialog (this, "Choose a color", color );
// R = color. getred (); // obtain the selected color.
// G = color. getgreen ();
// B = color. getblue ();
}

Public void clear (){
Color = color. Black;
Fill = false;
Clear = true;

Repaint ();
}

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Drawpad application = new drawpad ();
Application. setdefaclocloseoperation (jframe. exit_on_close );
}

}

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.