Java to do a mouse drawing, keyboard to write the artboard

Source: Internet
Author: User
Tags gety stub

Tools: Eclipse

Language: Java

Results:

Source:

Import Java.awt.Color;

Import java.awt.Frame;

Import Java.awt.Graphics;

Import Java.awt.event.KeyAdapter;

Import java.awt.event.KeyEvent;

Import Java.awt.event.MouseAdapter;

Import java.awt.event.MouseEvent;

Import Java.awt.event.MouseListener;

Import Java.awt.event.MouseMotionAdapter;

Import Javax.swing.JFrame;

Import Javax.swing.JPanel;

class huitu{

JFrame mframe=new JFrame (" artboard ");

JPanel Mpanel=new JPanel ();

intlastx=0;

intlasty=0;

Public Huitu () {

// TODO auto-generated constructor stub

mframe. SetSize (+);

mframe. setvisible (true);

mframe. Setforeground (Color. BLUE );

mframe. Add (mpanel);

Mpanel. SetBackground (Color. White );

mframe. Addmouselistener (new mouseadapter () {

Public voidmousepress (MouseEvent e) {

lastx = e. GetX ();

lasty = e. GetY ();

}

});

mframe. Addmousemotionlistener (new mousemotionadapter () {

Public void mousedragged (MouseEvent e) {

intx=e. GetX ();

inty=e. GetY ();

Graphics g =mframe. getgraphics ();

G.drawLine (lastx, lasty, x, y);

lastx=e. GetX ();

lasty=e. GetY ();

}

});

mframe. Addkeylistener (new keyadapter () {

Public void keytyped (keyevent e) {

String string=string. ValueOf(e. Getkeychar ());

mframe. getgraphics (). DrawString (string, lastx, lasty);

lastx + = +;

}

});

}

}

Public class c5t8 {

Public staticvoid main (string[] args) {

// TODO auto-generated method stub

New Huitu ();

}

}

Java to do a mouse drawing, keyboard to write the artboard

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.