📄 yhjpainter.java
字号:
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
//import javax.swing.Component;
public class YhjPainter extends JFrame implements ActionListener, MouseListener, MouseMotionListener {
int x, y, x1, y1, x2, y2, width, height;
boolean isFirstPoint = true;
// 初始化开始画的是线
int drawType = PaintingGround.LINE;
// 添加控件
ButtonGroup btg = new ButtonGroup();
Button btLine = new Button("线");
Button btRectangle = new Button("矩形");
Button btRound = new Button("圆");
Button btEllipse = new Button("椭圆");
Panel buttonPanel = new Panel();
PaintingGround paintingGround = new PaintingGround();
// Main Method
public static void main(String[] args) {
// 设置显示外观
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
new YhjPainter();
}
// 构造函数
public YhjPainter() {
// 控件添加到控件组中
// btg.add(btLine);
// btg.add(btRectangle);
// btg.add(btRound);
// btg.add(btEllipse);
buttonPanel.add(btLine);
buttonPanel.add(btRectangle);
buttonPanel.add(btRound);
buttonPanel.add(btEllipse);
// 设置容器及容器的整体布局
Container cp = this;
cp.setLayout(new BorderLayout());
cp.add(BorderLayout.NORTH, buttonPanel);
cp.add(BorderLayout.CENTER, paintingGround);
// cp.add(BorderLayout.SOUTH,jf);
// jf.setJMenuBar(mb);
setLocation(300, 150);
setSize(600, 480);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// 添加鼠标触发事件
paintingGround.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent evn) {
isFirstPoint = true;
switch (drawType) {
case PaintingGround.LINE:
// 画线
Line line = new Line(x1, y1, x2, y2);
paintingGround.add(line);
break;
case PaintingGround.RECTANGLE:
// 画矩形
Rect rect = new Rect(x1, y1, x2 - x1, y2 - y1);
paintingGround.add(rect);
break;
case PaintingGround.ROUND:
// 画圆
// 两点距离公式
int size = Math.abs((int) Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)));
Round round = new Round(x1, y1, size);
paintingGround.add(round);
break;
case PaintingGround.ELLIPSE:
// 画椭圆
Ellipse ellipse = new Ellipse(x1, y1, x2 - x1, y2 - y1);
paintingGround.add(ellipse);
break;
default:
break;
}
}
});
// 对鼠标的输入进行判断并调用画图程序
paintingGround.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent evn) {
if (isFirstPoint) {
x1 = evn.getX();
y1 = evn.getY();
isFirstPoint = false;
} else {
x2 = evn.getX();
y2 = evn.getY();
switch (drawType) {
case PaintingGround.LINE:
// 画线
Line line = new Line(x1, y1, x2, y2);
paintingGround.setNowObj(line);
break;
case PaintingGround.RECTANGLE:
// 画矩形
Rect rect = new Rect(x1, y1, x2 - x1, y2 - y1);
paintingGround.setNowObj(rect);
break;
case PaintingGround.ROUND:
// 画圆
// 两点距离公式
int size = Math.abs((int) Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)));
Round round = new Round(x1, y1, size);
paintingGround.setNowObj(round);
break;
case PaintingGround.ELLIPSE:
// 画椭圆
Ellipse ellipse = new Ellipse(x1, y1, x2 - x1, y2 - y1);
paintingGround.setNowObj(ellipse);
break;
default:
break;
}
repaint();
}
}
});
// 各个控件的触发事件
btLine.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evn) {
drawType = PaintingGround.LINE;
}
});
btRectangle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evn) {
drawType = PaintingGround.RECTANGLE;
}
});
btRound.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evn) {
drawType = PaintingGround.ROUND;
}
});
btEllipse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evn) {
drawType = PaintingGround.ELLIPSE;
}
});
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
}
class PaintingGround extends JPanel {
public static final int LINE = 1;
public static final int RECTANGLE = 2;
public static final int ROUND = 3;
public static final int ELLIPSE = 4;
private int x, y;
private int x1, y1, x2, y2;
private int width, height, size;
private int drawType = 0;
private boolean isFill = false;
private List list = new ArrayList();
private Object nowObj=null;
// 构造函数
public PaintingGround() {
setBackground(Color.white);
}
// 判断是用实心还是空心的,
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.black);
System.out.println("size="+list.size());
for (int i = 0; i < list.size(); i++) {
Object obj = list.get(i);
drawPic(obj,g);
}
drawPic(nowObj,g);
}
private void drawPic(Object obj,Graphics g){
if(obj==null)
return ;
if (obj instanceof Line) {
Line line = (Line) obj;
g.drawLine(line.x1, line.y1, line.x2, line.y2);
} else if (obj instanceof Rect) {
Rect rect = (Rect) obj;
g.drawRect(rect.x1, rect.y1, rect.width, rect.height);
} else if (obj instanceof Round) {
Round round = (Round) obj;
g.drawOval(round.x, round.y, round.size, round.size);
} else if (obj instanceof Ellipse) {
Ellipse ellipse = (Ellipse) obj;
g.drawOval(ellipse.x, ellipse.y, ellipse.width, ellipse.height);
}
}
public void setFillState(boolean isFill) {
this.isFill = isFill;
}
public void add(Object obj) {
list.add(obj);
}
public void setNowObj(Object obj)
{
this.nowObj=obj;
}
}
class Line {
public int x1;
public int x2;
public int y1;
public int y2;
Line(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
}
class Rect {
public int x1;
public int y1;
public int width;
public int height;
Rect(int x1, int y1, int width, int height) {
this.x1 = x1;
this.y1 = y1;
this.width = width;
this.height = height;
}
}
class Round {
public int x;
public int y;
public int size;
Round(int x, int y, int size) {
this.x = x;
this.y = y;
this.size = size;
}
}
class Ellipse {
public int x;
public int y;
public int width;
public int height;
Ellipse(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -