资源描述
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class huahua extends JFrame {
DrawPanel drawingArea;
int index = 0;
drewings[] itemList = new drewings[5000];
Container c = getContentPane();
public huahua()
{
super("简单画板");
drawingArea = new DrawPanel();
c.add(drawingArea, BorderLayout.CENTER);
itemList[index] = new Penci();
setSize(500 , 500) ;
setVisible(true);
show();
}
public static void main(String[] Strgs)
{
new huahua();
}
class mousea extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
itemList[index].x1 = itemList[index].x2 = e.getX();
itemList[index].y1 = itemList[index].y2 = e.getY();
index++;
itemList[index] = new Penci();
}
public void mouseReleased(MouseEvent e)
{
itemList[index].x1 = e.getX();
itemList[index].y1 = e.getY();
//重新获取x1y1的值 虽然移动的时候x1y1在变但那时移动监听的不是释放监听的
//如果不重新获取坐标将变成默认的0,0
itemList[index].x2 = e.getX();
itemList[index].y2 = e.getY();
repaint();
index++;
itemList[index] = new Penci();
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
}
class mouseb extends MouseMotionAdapter
{
public void mouseDragged(MouseEvent e)
{
itemList[index - 1].x1 = itemList[index].x2 = itemList[index].x1 = e.getX();
itemList[index - 1].y1 = itemList[index].y2 = itemList[index].y1 = e.getY();
//按住移动是获取了x1y1的坐标但这坐标不会赋值给释放 动作不同不会相互赋值
//index++;
itemList[index] = new Penci();
repaint();
}
public void mouseMoved(MouseEvent e)
{
}
}
class DrawPanel extends JPanel {
public DrawPanel() {
setBackground(Color.white);
addMouseListener(new mousea());
addMouseMotionListener(new mouseb());
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g; //定义画笔
int j = 0;
while (j <= index) {
drew(g2d, itemList[j]);
j++;
}
}
void drew(Graphics2D g2d, drewings i) {
i.drew(g2d);//将画笔传入到各个子类中,用来完成各自的绘图
}
}
}
class drewings implements Serializable
{
int x1 , y1 , x2, y2;
void drew(Graphics2D g2d) {
}
}
class Penci extends drewings{
void drew(Graphics2D g2d) {
g2d.drawLine(x1, y1, x2, y2);
//System.out.println(x1);
}
}
展开阅读全文