首先建包,在其中引入swing和awt工具包,通过使用其中的类来构建界面应用程序和完成画图板内容;
package yyy1110;
import javax.swing.*;
import java.awt.*;
创建画板实现图形的正常绘制
1、创建窗口对象,创建面板。
public class DrawPad {
DrawListener dl=new DrawListener();
public void showUI(){
JFrame jf=new JFrame();
jf.setTitle("画图板pro");
jf.setSize(600,600);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setLayout(new FlowLayout());
2、利用数组添加图形和颜色按钮(方便后续修改)。
String[] btnstrs={"直线", "圆形","矩形","实心圆形","实心矩形","三角形","多边形"};
for (int i = 0; i < btnstrs.length; i++) {
JButton btn=new JButton(btnstrs[i]);
jf.add(btn);
btn.addActionListener(dl);
}
Color[] colors={Color.BLACK,Color.BLUE,Color.CYAN,Color.GRAY,Color.GREEN,Color.magenta,Color.WHITE};
for (int i = 0; i < colors.length; i++) {
JButton btn=new JButton("color");
btn.setBackground(colors[i]);
btn.setPreferredSize(new Dimension(30,30));
jf.add(btn);
btn.addActionListener(dl);
}
jf.addMouseListener(dl);
上列代码中的
DrawListener dl=new DrawListener();
btn.addActionListener(dl);
btn.addActionListener(dl);
jf.addMouseListener(dl);
为创建监听器和给按钮添加监听器对象,根据监听事件执行相应功能(画“直线”、“曲线”等)。
3、为画板添加画板和画笔,画笔是java中有的,直接获得就可以使用(先让窗体可视化)。
jf.setVisible(true);
Graphics g1= jf.getGraphics();
dl.g1=g1;
}
4、为主界面添加主函数
public static void main(String[] args){
DrawPad dp=new DrawPad();
dp.showUI();
}
}
<2> 创建接口,监听事件
1、重新引入类名,创建接口,定义未知数(在创建接口时要写出所有包含的方法)
package yyy1110;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class DrawListener implements MouseListener, ActionListener {
Graphics g1;
int x1,y1,x2,y2;
String shapeType = "";
2、在接口监听事件中,完善代码(图形绘制)—通过判断按钮上的文字(“直线”,“曲线”)来执行相应代码
@Override
public void actionPerformed(ActionEvent e) {
String ac=e.getActionCommand();
if(ac.equals("color")){
JButton btn=(JButton)e.getSource();
Color color=btn.getBackground();
g1.setColor(color);
}
else{
shapeType=ac;
}
System.out.println(ac);
}
int x3, y3, x4, y4, x5, y5;
int count = 0;
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("点击");
int x = e.getX();
int y = e.getY();
if(shapeType.equals("三角形")){
if (count == 0) {
x3 = x;
y3 = y;
count++;
} else if (count == 1) {
x4 = x;
y4 = y;
g1.drawLine(x3, y3, x4, y4);
count++;
} else if (count == 2) {
x5 = x;
y5 = y;
g1.drawLine(x3, y3, x5, y5);
g1.drawLine(x5, y5, x4, y4);
count = 0;
}
}
//多边形: 多次点击 不确定边数 也就不确定需要的坐标数量
if(shapeType.equals("多边形")){
if (count == 0) {
x3=x;
y3=y;
count++;
} else if (count==1) {
x4 = x;
y4 = y;
g1.drawLine(x3, y3, x4, y4);
count++;
} else if (count == 2) {
x5 = x;
y5 = y;
if (abs(x3-x5)<15&&abs(y3-y5)<15){
g1.drawLine(x3,y3,x4,y4);
count=0;
return;
}
g1.drawLine( x4, y4,x5, y5);
x4=x5;y4=y5;
}
}
}
@Override
public void mousePressed(MouseEvent e) {
System.out.println("按下");
x1=e.getX();
y1=e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("松开");
x2 = e.getX();
y2 = e.getY();
if (shapeType.equals("直线")) {
g1.drawLine(x1, y1, x2, y2);
}
if (shapeType.equals("矩形")) {
g1.drawRect(min(x1, x2), min(y1, y2), abs(x2 - x1), abs(y2 - y1));
}
if (shapeType.equals("圆形")) {
g1.drawOval(min(x1, x2), min(y1, y2), abs(x2 - x1), abs(y2 - y1));
}
if (shapeType.equals("实心矩形")) {
g1.fillRect(min(x1, x2), min(y1, y2), abs(x2 - x1), abs(y2 - y1));
}
if (shapeType.equals("实心圆形")) {
g1.fillOval(min(x1, x2), min(y1, y2), abs(x2 - x1), abs(y2 - y1));
}
}
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("进入");
}
@Override
public void mouseExited(MouseEvent e) {
System.out.println("离开");
}
3 、定义一些简单方法,协助上列图形的绘制。
public int abs(int n) {
if (n < 0) {
return -n;
}
return n;
}
public int min(int a, int b) {
return a < b ? a : b;
}
}