码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA做一个可以鼠标绘图、键盘写字的画板

时间:2018-05-05 23:07:56      阅读:402      评论:0      收藏:0      [点我收藏+]

标签:gety   value   vax   ola   char   awt   png   代码   struct   

工具:eclipse

语言:java

成果:

技术分享图片

源代码:

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("画板");

    JPanel mPanel=new JPanel();

    int LastX=0;

    int LastY=0;

public HuiTu() {

        // TODO Auto-generated constructor stub

    mFrame.setSize(800, 800);

    mFrame.setVisible(true);

    mFrame.setForeground(Color.BLUE);

    mFrame.add(mPanel);

    mPanel.setBackground(Color.WHITE);

      

      

    mFrame.addMouseListener(new MouseAdapter() {

        public void mousePress(MouseEvent e) {

            LastX = e.getX();

            LastY = e.getY();

                

            }

        });

    mFrame.addMouseMotionListener(new MouseMotionAdapter() {

        public void mouseDragged(MouseEvent e) {

            int x=e.getX();

            int y=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 += 30;

          

                

            }

        });

    }

 

}

public class C5T8 {

 

    public static void main(String[] args) {

        // TODO Auto-generated method stub

new HuiTu();

    }

 

}

JAVA做一个可以鼠标绘图、键盘写字的画板

标签:gety   value   vax   ola   char   awt   png   代码   struct   

原文地址:https://www.cnblogs.com/huchangqing/p/8996410.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!