JAVA做一个可以鼠标绘图键盘写字的画板
Posted huchangqing
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA做一个可以鼠标绘图键盘写字的画板相关的知识,希望对你有一定的参考价值。
工具: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做一个可以鼠标绘图键盘写字的画板的主要内容,如果未能解决你的问题,请参考以下文章