运行后可以效果如下: 功能:
1.可进行三角形,圆形的相应图形进行绘制
2.可设置绘制图形的颜色
3.可显示当前绘制图形的周长,面积等信息
4.将绘制好的图片保存到本地
进行相应画图截图: 用到的知识:
1.反射解耦
2.接口多态
3.继承封装
4.其他Java基础知识
部分代码:
public class PaintApp {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
JFrame frame = new JFrame("画图板");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
MyCanvas canvas = new MyCanvas();
canvas.setBackground(Color.white);
frame.add(canvas, BorderLayout.CENTER);
JMenuBar menuBar = new JMenuBar();
JButton rectBtn = new JButton(new ImageIcon("D:\\java\\Exp_PaintBord_chenhao\\resource\\rect.png"));
rectBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
canvas.setFigType("rectangle");
}
});
menuBar.add(rectBtn);
JButton lineBtn = new JButton(new ImageIcon("D:\\java\\Exp_PaintBord_chenhao\\resource\\line.png"));
lineBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
canvas.setFigType("line");
}
});
menuBar.add(lineBtn);
JButton ellipseBtn = new JButton(new ImageIcon("D:\\java\\Exp_PaintBord_chenhao\\resource\\ellipse.png"));
ellipseBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
canvas.setFigType("ellipse");
}
});
menuBar.add(ellipseBtn);
JButton eqTriangleBtn = new JButton(new ImageIcon("D:\\java\\Exp_PaintBord_chenhao\\resource\\eqTriangle.png"));
eqTriangleBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
canvas.setFigType("eqTriangle");
}
});
menuBar.add(eqTriangleBtn);
JButton rightTriangleBtn = new JButton(
new ImageIcon("D:\\java\\Exp_PaintBord_chenhao\\resource\\rightTriangle.png"));
rightTriangleBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
canvas.setFigType("rightTriangle");
}
});
menuBar.add(rightTriangleBtn);
JButton colorBtn = new JButton("■");
colorBtn.setForeground(Color.black);
colorBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Color color = JColorChooser.showDialog(frame, "绘制颜色", colorBtn.getForeground());
colorBtn.setForeground(color);
canvas.setColor(color);
}
});
menuBar.add(colorBtn);
JButton saveBtn = new JButton("保存");
saveBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
if(chooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
try {
canvas.savePic(file);
} catch (IOException e1) {
e1.printStackTrace();
}
}
;
}
});
menuBar.add(saveBtn);
frame.add(menuBar, BorderLayout.NORTH);
frame.setVisible(true);
}
}
```java
public class MyCanvas extends Canvas {
static Properties config ;
static {
try {
config = new Properties();
config.load(MyCanvas.class.getResourceAsStream("config.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}
private Figure figure;
private String figType = "line";
private Color color = Color.black;
List<Figure> figures = new ArrayList<>();
public void setColor(Color color) {
this.color = color;
}
public void setFigType(String figType) {
this.figType = figType;
}
MouseAdapter adapter = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
try {
Class clz = Class.forName(config.getProperty(figType));
Constructor constructor = clz.getConstructor();
figure = (Figure)constructor.newInstance();
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException
| IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) {
e1.printStackTrace();
}
figure.setColor(color);
figure.setStart(e.getPoint());
}
@Override
public void mouseReleased(MouseEvent e) {
figures.add(figure);
}
@Override
public void mouseDragged(MouseEvent e) {
figure.setEnd(e.getPoint());
MyCanvas.this.repaint();
}
};
public MyCanvas() {
this.addMouseListener(adapter);
this.addMouseMotionListener(adapter);
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.white);
g.fillRect(0, 0, getWidth(), getHeight());
for (Figure figure : figures) {
figure.draw(g);
}
if (figure != null) {
figure.draw(g);
Point strat = figure.getStart();
Point end = figure.getEnd();
StringBuilder sb = new StringBuilder();
sb.append("起点:("+strat.x+","+strat.y+")");
sb.append("|终点:("+end.x+","+end.y+")");
if(figure instanceof Lengthable) {
Lengthable lengthable = (Lengthable)figure;
sb.append("|长度:"+lengthable.length());
}
if(figure instanceof Areable) {
Areable area = (Areable)figure;
sb.append("|面积:"+area.area());
}
if(figure instanceof Perimeterable) {
Perimeterable pe = (Perimeterable)figure;
sb.append("|周长:"+pe.perimeter());
}
g.drawString(sb.toString(), 20, getHeight()-20);
}
}
public void savePic(File file) throws IOException {
BufferedImage image = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_BGR);
Graphics g = image.createGraphics();
this.paint(g);
g.dispose();
if(!file.exists()) {
file.createNewFile();
}
ImageIO.write(image, "jpg", file);
}
}
想要全部代码可以关注我发你哦!!
|