Java Applet drawing Board

Source: Internet
Author: User
Tags exit gety
Import java.awt.*;
Import javax.swing.*;
Import java.awt.event.*;
public class Huatu implements mousemotionlistener,actionlistener{
static final int r=3;
static int ox, OY;
static int flag=1;
public static void Main (String arg[]) {
JFrame f=new JFrame ("Paint");
Container P=f.getcontentpane ();
P.setlayout (New FlowLayout ());
JButton jButton1 = new JButton ("Red");
JButton jButton2 = new JButton ("Black");
JButton JButton3 = new JButton ("Exit");
Graphics G.setcolor (New Color (255,0,0));
Buttonevent arg=new buttonevent (ARG);
Jbutton1.addactionlistener (New Huatu ());
Jbutton2.addactionlistener (New Huatu ());
Jbutton3.addactionlistener (New Huatu ());
P.addmousemotionlistener (New Huatu ());
P.add (JButton1);
P.add (JButton2);
P.add (JButton3);
F.setsize (200,200);
F.show ();
}
public void actionperformed (ActionEvent e) {
if (E.getactioncommand (). Equals ("Red")) {
flag=1;
}
else if (E.getactioncommand (). Equals ("Black")) {
flag=2;
}
else if (E.getactioncommand (). Equals ("Exit"))
System.exit (0);
}
public void mousedragged (MouseEvent e) {
Container c= (Container) E.getsource ();
Graphics G=c.getgraphics ();
if (ox>=0) {
if (flag==1) {
G.setcolor (New Color (168,0,255));
G.drawline (Ox,oy,e.getx (), e.gety ());}
Else
{G.setcolor (new Color (0,0,0));
G.drawline (Ox,oy,e.getx (), e.gety ());}
}
Ox=e.getx (); Oy=e.gety ();
}
public void mousemoved (MouseEvent e) {
Ox=-1;oy=-1;
}
}




Related Article

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.