按钮特别多,一个按钮至少需要 3行代码,所以要用到数组
数组: 一次性存储多个相同类型的数据 按照下标编号去索引
图形类型按钮: 只有文字不同
颜色按钮: 只有颜色不同
数组格式: 数据类型[ ] 数组名 ={元素1,2,3,4,5,6};
如 : String[] strs = {"直线","矩形","圆形","实心矩形","实心圆形","等腰三角形","三角形","多边 形","签字笔","橡皮擦"};
操作数组中的数据: 下标 从0 开始 依次递加1 String text = strs[0];// text 就是 “直线”
三角形: 三个点: 第一个点: 点击 获取 x3 y3 第二个点: 点击 获取 x4 y4 与第一个点连线 第三个点: 点击 获取 x5 y5 与前两个点都连线
package com.ldy0720;
import javax.swing.*;
import java.awt.*;
import java.util.concurrent.Flow;
public class DrawPad{
DrawListener dl = new DrawListener ();
public void initUI(){
// 创建一个窗体
JFrame jf = new JFrame ();
jf.setTitle ("画板");
jf.setSize (720, 600);
jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
FlowLayout fl = new FlowLayout ();
jf.setLayout (fl);
String[] strs = {"直线", "矩形", "圆形", "实心矩形", "实心圆形",
"等腰三角形", "三角形", "多边形", "橡皮擦"};
for(int i = 0; i < strs.length; i++){
// 创建按钮
JButton btn = new JButton ();
// 获取数组中对应位置的数据
String text = strs[i];
// 设置文本
btn.setText (text);
jf.add (btn);
btn.addActionListener (dl);
}
// 颜色按钮
Color[] colors = {Color.BLACK, Color.WHITE, Color.GRAY, Color.LIGHT_GRAY, Color.DARK_GRAY,
Color.RED, Color.GREEN, Color.BLUE, Color.CYAN, Color.PINK, Color.MAGENTA, Color.ORANGE};
Dimension dim=new Dimension (30,30);
for(int i = 0; i < colors.length; i++){
// 创建按钮
JButton btn = new JButton ();
// 取出数组中对应位置的数据
Color color = colors[i];
// 设置背景颜色
btn.setBackground (color);
btn.setPreferredSize (dim);
jf.add (btn);
btn.addActionListener (dl);
}
jf.setVisible (true);
// 添加鼠标监听器
jf.addMouseListener (dl);
// 初始化监听器中的画笔
dl.g = jf.getGraphics ();
}
public static void main(String[] args){
DrawPad drawPad = new DrawPad ();
drawPad.initUI ();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DrawListener implements ActionListener, MouseListener{
Graphics g;
int x1, y1, x2, y2;
String shapeType = "直线";
Color shapeColor = Color.BLACK;
public void actionPerformed(ActionEvent e){
String ac = e.getActionCommand ();
System.out.println ("点击了" + ac);
if(ac.equals ("")){// 表示颜色按钮
// 获取按钮对象 用于获取颜色
JButton btn = (JButton) e.getSource ();
// 获取背景颜色
shapeColor = btn.getBackground ();
// 设置颜色给画笔
g.setColor (shapeColor);
} else{
shapeType = ac;
}
}
int x3,y3,x4,y4;
int count=0;
public void mouseClicked(MouseEvent e){
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;
g.drawLine (x3, y3, x4, y4);
count++;
}
}
}
public void mousePressed(MouseEvent e){
x1 = e.getX ();
y1 = e.getY ();
}
public void mouseReleased(MouseEvent e){
x2 = e.getX ();
y2 = e.getY ();
if(shapeType.equals ("直线")){
g.drawLine (x1, y1, x2, y2);
}else if(shapeType.equals ("矩形")){
g.drawRect (Math.min (x1, x2), Math.min (y1, y2), Math.abs (x2 - x1),
Math.abs (y2 - y1));
}else if(shapeType.equals ("圆形")){
g.drawOval (Math.min (x1, x2), Math.min (y1, y2), Math.abs (x2 - x1),
Math.abs (y2 - y1));
}else if(shapeType.equals ("实心矩形")){
g.fillRect (Math.min (x1, x2), Math.min (y1, y2), Math.abs (x2 - x1),
Math.abs (y2 - y1));
}else if(shapeType.equals ("实心圆形")){
g.fillOval (Math.min (x1, x2), Math.min (y1, y2), Math.abs (x2 - x1),
Math.abs (y2 - y1));
}else if(shapeType.equals ("橡皮擦")){
// 设置背景色
Color color = new Color (238, 238, 238);// 窗体的背景色
g.setColor (color);
g.fillRect (Math.min (x1, x2), Math.min (y1, y2), Math.abs (x2 - x1),
Math.abs (y2 - y1));
// 还原颜色
g.setColor (shapeColor);
}
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
}
运行结果:

该程序使用JavaSwing创建了一个画板应用,包含多个图形和颜色按钮。用户可以通过选择不同的图形类型和颜色进行绘制,支持直线、矩形、圆形等基本图形,以及橡皮擦功能。图形的创建和颜色选择通过按钮数组实现,点击按钮触发相应操作。
291

被折叠的 条评论
为什么被折叠?



