实验要求:
涂鸦板是一个具有趣味性的小程序,它的实现原理是通过鼠标事件,控制鼠标移动结合画图功能实现。鼠标拖拽才开始绘制,松开鼠标则停止绘制。
代码:
--MyPaint画板类:
package com.junlin.exer6;
import javax.swing.JLabel;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
public class MyPaint extends JLabel {
private boolean is_painting = false; //判断是否正在绘制
private List<Curve> curves = new ArrayList<>(); //记录绘制过程中所有的曲线
//记录使用过的线条粗细,主要是为了保留之前绘制的效果,不会让改变线条粗细影响之前的绘制
private List<Float> lineSizes = new ArrayList<>();
//同上记录使用过的颜色
private List<Color> lineColors = new ArrayList<>();
private Float currentLineSize = 2.0f; //当前线条的粗细
private Color currentLineColor = Color.RED;
public MyPaint(){
//绑定事件
MyMouseListener handler = new MyMouseListener();
this.addMouseListener(handler);
this.addMouseMotionListener(handler);
}
public void setCurrentLineSize(float currentLineSize) {
this.currentLineSize = currentLineSize;
}
public void setCurrentLineColor(Color color) {
this.currentLineColor = color;
}
//下面这个方法在程序运行的时候被一直调用
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
//this.setOpaque(true);
//this.setBackground(Color.WHITE); //为什么这行代码不起作用就是因为没有写上面设置透明度的代码
// System.out.printf("%d %d\n", width, height);
g.setColor(Color.WHITE);
g.fillRect(0,0,width,height);
//g.setFont(new Font("宋体", Font.BOLD, 200));
Graphics2D g2 = (Graphics2D)g; //g是Graphics对象
g2.setStroke(new BasicStroke(currentLineSize));
g2.setPaint(currentLineColor);
//从曲线集合中进行绘制
for(int i = 0;i < curves.size();i++) {
Curve curve = curves.get(i);
g2.setStroke(new BasicStroke(lineSizes.get(i)));
g2.setPaint(lineColors.get(i));
if(curve.points.size() >= 2) {
Point p1 = curve.points.get(0);
for(int j = 1;j < curve.points.size();j++){
Point p2 = curve.points.get(j);
g.drawLine(p1.x,p1.y,p2.x,p2.y);
p1 = p2;
}
}
}
}
//清空画板
public void clearPaint() {
this.curves.clear();
this.lineSizes.clear();
this.lineColors.clear();
repaint();
}
//监听内部类
public class MyMouseListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
is_painting = true;
lineSizes.add(currentLineSize);
lineColors.add(currentLineColor);
Curve curve = new Curve();
curve.points = new ArrayList<>();
curves.add(curve);
}
@Override
public void mouseReleased(MouseEvent e) {
is_painting = false;
}
@Override
public void mouseDragged(MouseEvent e) {
//如果表示正在绘制
if(is_painting) {
//取出曲线集合中的最后一条曲线(就是当前正在绘制的曲线)
Curve curve = curves.get(curves.size() - 1);
curve.points.add(e.getPoint()); //将当前的点加入曲线中
repaint(); //重新绘制
}
}
}
//曲线内部类,其属性就是曲线上的点的集合
public class Curve {
List<Point> points;
}
}
MyFrame窗体类:
package com.junlin.exer6;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class MyFrame extends JFrame {
public MyFrame(){
super("涂鸦板");
JPanel root = new JPanel();
root.setLayout(null);
this.add(root);
//自定义画板控件
MyPaint paint = new MyPaint();
paint.setBounds(250,10,600,600);
root.add(paint);
//清除画板按钮
JButton clearBtn = new JButton("清空画板");
clearBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
paint.clearPaint();
}
});
clearBtn.setBounds(70,40,95,40);
root.add(clearBtn);
//文本标签
JLabel lineSizeLabel = new JLabel("选择线条粗细:");
lineSizeLabel.setBounds(10,300,100,20);
lineSizeLabel.setFont(new Font("宋体", Font.BOLD, 14));
root.add(lineSizeLabel);
//线条粗细列表
JComboBox<String> LineTypeField = new JComboBox<>();
for(int i = 2;i <= 12;i += 2) {
LineTypeField.addItem(i+"");
}
LineTypeField.setBounds(120,300,100,30);
LineTypeField.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
// 选择的下拉框选项
System.out.println(e.getItem() instanceof String);
float size = Float.parseFloat(String.valueOf(e.getItem()));
paint.setCurrentLineSize(size);
}
}
});
root.add(LineTypeField);
//文本控件
JLabel lineColorLabel = new JLabel("选择线条颜色:");
lineColorLabel.setBounds(10,200,100,20);
lineColorLabel.setFont(new Font("宋体", Font.BOLD, 14));
root.add(lineColorLabel);
//颜色选择列表
JComboBox<String> LineColorField = new JComboBox<>();
LineColorField.addItem("红色");
LineColorField.addItem("蓝色");
LineColorField.addItem("粉红色");
LineColorField.addItem("橙色");
LineColorField.addItem("绿色");
LineColorField.setBounds(120,200,100,30);
LineColorField.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
// 选择的下拉框选项
Color color;
switch (String.valueOf(e.getItem())) {
case "红色":
color = Color.RED;
break;
case "蓝色":
color = Color.BLUE;
break;
case "粉红色":
color = Color.PINK;
break;
case "橙色":
color = Color.ORANGE;
break;
case "绿色":
color = Color.GREEN;
break;
default:
color = Color.BLACK;
break;
}
paint.setCurrentLineColor(color);
}
}
});
root.add(LineColorField);
this.setSize(1000,700);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
--JavaMain主类:
package com.junlin.exer6;
public class JavaMain {
public static void main(String[] args) {
new MyFrame();
}
}
标签:awt,Java,java,Color,Swing,new,import,涂鸦板,public
From: https://www.cnblogs.com/junlin623/p/17171466.html