前面发布图形绘制存在许多不足:1.缺少功能,图形选择过于少,颜色不够丰富 2.轨迹直线是一大改进,然后采用的布局发生改变不再采用流式布局 3.增加了图形重绘功能 与图形橡皮擦4.简化了按钮代码,极大的减少了工作量。
package Cl.Bdraw;
import javax.swing.*;
import java.awt.*;
public class draw {
//第一步需要做的是定义方法然后绘制界面
//JFrame 默认是边框布局
//swing 组件都有一个paint方法,绘制组件本身
public void intui(){
JFrame jf=new JFrame();
//设置窗体的大小
jf.setSize(900,900);
//设置窗体的名称
jf.setTitle("小浩的画板");
//设置屏幕出现时候的位置
jf.setLocationRelativeTo(null);
//设置退出窗口
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置窗体背景颜色
jf.getContentPane().setBackground(Color.white);
//面板容器(JPanel):默认流式布局
JPanel northPanel = new JPanel();
northPanel.setBackground(Color.white);
northPanel.setPreferredSize(new Dimension(0,120));
jf.add(northPanel, BorderLayout.NORTH);
//画图画板
MyPanel centerPanel=new MyPanel();
centerPanel.setBackground(Color.WHITE);
jf.add(centerPanel,BorderLayout.CENTER);
//设置监听器
drawmouse mouse=new drawmouse();
//定义字符串数组来存储按钮名称
String[]name={"直线","三角形","矩形","椭圆","圆","多边形","橡皮擦","轨迹直线"};
for (int i=0;i<name.length;i++){
JButton jb=new JButton(name[i]);
Dimension ds=new Dimension(130,50);
jb.setPreferredSize(ds);
northPanel.add(jb);
jb.addActionListener(mouse);
}
Color[]color={Color.blue,Color.cyan,Color.green,Color.yellow};
for (int i=0;i<color.length;i++){
JButton ja=new JButton();
ja.setBackground(color[i]);
Dimension dsa=new Dimension(130,50);
ja.setPreferredSize(dsa);
northPanel.add(ja);
ja.addActionListener(mouse);
}
//窗体显示
jf.setVisible(true);
//从窗体获取画笔
Graphics graphics=centerPanel.getGraphics();
//拖动监听器的设置
centerPanel.addMouseListener(mouse);
//添加鼠标拖动监听器
centerPanel.addMouseMotionListener(mouse);
//画笔赋予
mouse.gr=graphics;
//把Shape数组从drawmouse类传递给MyPanel类
centerPanel.shapeArr=mouse.shapeArr;
}
public static void main(String[] args) {
draw b=new draw();
b.intui();
}
}
package Cl.Bdraw;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class drawmouse implements MouseListener, ActionListener, MouseMotionListener {
//画笔
public Graphics gr;
//绘制图形时的坐标
public int x1,y1,x2,y2,x3,y3,x4,y4;
//初始位置的标记
public int flag=0;
//获取按钮上的东西
public String name;
//定义Shape数组保存图形对象
public Shape[]shapeArr=new Shape[100];
//操作数组的下标
public int index=0;
//动作监听器,对接口方法进行重写
public void actionPerformed(ActionEvent e){
if("".equals(e.getActionCommand())){
//获取当前事件源
JButton jbu=(JButton) e.getSource();
//获取按钮上的背景颜色,并设置给画笔
Color color=jbu.getBackground();
gr.setColor(color);
}else {
//获取按钮内容
name=e.getActionCommand();
System.out.println("name:"+name);
}
}
public void mouseClicked(MouseEvent e){
System.out.println("点击");
x3=e.getX();
y3=e.getY();
if("三角形".equals(name)&&flag==1){
gr.drawLine(x1,y1,x3,y3);
gr.drawLine(x2,y2,x3,y3);
flag-=1;
}
// if("多边形".equals(name)&&flag==2){
gr.drawLine(x1,y1,x3,y3);
// gr.drawLine(x2,y2,x3,y3);
// gr.drawLine(x3,y3,x2,y2);
// flag-=2;
// }
}
public void mousePressed(MouseEvent e){
System.out.println("按下");
//获取当前坐标值
if(flag==0){
x1=e.getX();
y1=e.getY();
}
}
public void mouseReleased(MouseEvent e){
//获取当前坐标值
if (flag == 0) {
x2 = e.getX();
y2 = e.getY();
}
//开始判别图形标识信息
if ("直线".equals(name)) {
//画线开始
gr.drawLine(x1, y1, x2, y2);
//创建Shape对象来保存数据
Shape shape=new Shape(x1,y1,x2,y2,name);
//保存到数组中
shapeArr[index]=shape;
index++;
}
if ("圆".equals(name)) {
//画圆开始
gr.drawOval(x1,y1,Math.abs(x2-x1),Math.abs(x2-x1));
//创建Shape对象来保存数据
Shape shape=new Shape(x1,y1,x2,y2,name);
//保存到数组中
shapeArr[index]=shape;
index++;
}
if ("椭圆".equals(name)) {
//画椭圆开始
gr.drawOval(x1,y1,Math.abs(x2-x1),Math.abs(y2-y1));
//创建Shape对象来保存数据
Shape shape=new Shape(x1,y1,x2,y2,name);
//保存到数组中
shapeArr[index]=shape;
index++;
}
if ("矩形".equals(name)) {
gr.drawRect(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1));
//创建Shape对象来保存数据
Shape shape=new Shape(x1,y1,x2,y2,name);
//保存到数组中
shapeArr[index]=shape;
index++;
}
if ("三角形".equals(name) && flag == 0) {
gr.drawLine(x1, y1, x2, y2);
flag += 1;
}
// if ("多边形".equals(name)&& flag == 0) {
// //画多边形开始
// gr.drawLine(x1,y1,x2,y2);
// flag+=2;
// }
if("橡皮擦".equals(name)){
{
gr.setColor(Color.WHITE);
gr.drawRect(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1));
gr.fillRect(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1));
}
}
}
public void mouseDragged(MouseEvent e) {
if ("轨迹直线".equals(name)) {
gr.setColor(Color.white);
gr.drawLine(x1, y1, x4, y4);
x4 = e.getX();
y4 = e.getY();
gr.setColor(Color.black);
gr.drawLine(x1, y1, x4, y4);
//创建Shape对象来保存数据
Shape shape=new Shape(x1,y1,x2,y2,name);
//保存到数组中
shapeArr[index]=shape;
index++;
}
}
public void mouseMoved(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
System.out.println("进入");
}
public void mouseExited(MouseEvent e){
System.out.println("退出");
}
}
package Cl.Bdraw;
import java.awt.*;
public class Shape {
//定义图形的属性
public int x1,y1,x2,y2;
public String name;
//使用定义构造方法来初始化属性
public Shape(int x1,int y1,int x2,int y2,String name) {
this.x1 = x1;
this.x2=x2;
this.y1=y1;
this.y2=y2;
this.name=name;
}
//根据所保存的数据来进行图形的还原
public void drawShape(Graphics g){
switch (name){
case "直线":
g.drawLine(x1,y1,x2,y2);
break;
case "矩形":
g.drawRect(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1));
break;
case "圆":
g.drawOval(x1,y1,Math.abs(x2-x1),Math.abs(x2-x1));
break;
case "椭圆":
g.drawOval(x1,y1,Math.abs(x2-x1),Math.abs(y2-y1));
break;
// case "轨迹直线":
// g.setColor(Color.white);
// break;
}
}
}
package Cl.Bdraw;
import javax.swing.*;
import java.awt.*;
//自定义面板对象
public class MyPanel extends JPanel {
//定义属性来保存数据
public Shape[] shapeArr;
//重写paint方法
public void paint(Graphics g) {
//绘制组件本身
super.paint(g);
System.out.println("绘制组件");
//绘制图形,通过遍历shapeArr数组中Shape对象,调用drawShape方法
for (int i = 0; i < shapeArr.length; i++) {
Shape shape = shapeArr[i];
if (shape == null) break;
System.out.println("shape:"+shape);
shape.drawShape(g);
}
}
把效果放在这,部分效果还不够完善,还在进一步探索中,欢迎call我