/**
* 画图板的升级 ,可以实现图形和颜色的选择,可以以此为模版继续添加图形和颜色
*/
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
public class DrawUI {
public static void main(String args[]) {
DrawUI dr = new DrawUI();
dr.initUI();
}
public void initUI() {
JFrame jf = new JFrame("画图板");
jf.setSize(500,600);
jf.setResizable(false); //窗体大小不能变化
jf.setLocation(300, 20);
jf.setLayout(null);
JLabel jlb = new JLabel("请选择");
jlb.setBounds(10,420,100,30);
jf.add(jlb);
/**
* 画图区域
*/
JPanel jp = new JPanel();
jp.setSize(500,400);
jp.setBackground(Color.DARK_GRAY);
jf.add(jp);
/**
* 图形选择
*/
JRadioButton jrb1 = new JRadioButton("直线");
JRadioButton jrb2 = new JRadioButton("矩形");
JRadioButton jrb3 = new JRadioButton("圆");
JRadioButton jrb4 = new JRadioButton("3D-rect");
JRadioButton jrb5 = new JRadioButton("Full3DRect");
/**
* 设置相应图形的指令
*/
jrb1.setActionCommand("line");
jrb2.setActionCommand("rect");
jrb3.setActionCommand("oval");
jrb4.setActionCommand("3D-rect");
jrb5.setActionCommand("Full3DRect");
jrb1.setBounds(10, 450,70, 50);
jrb2.setBounds(100, 450,70, 50);
jrb3.setBounds(200, 450,70, 50);
jrb4.setBounds(300, 450,70, 50);
jrb5.setBounds(400, 450,90, 50);
ButtonGroup group1 = new ButtonGroup(); //组管理,实现图形单选
group1.add(jrb1);
group1.add(jrb2);
group1.add(jrb3);
group1.add(jrb4);
group1.add(jrb5);
jrb1.setSelected(true); //默认选择为直线
jf.add(jrb1);
jf.add(jrb2);
jf.add(jrb3);
jf.add(jrb4);
jf.add(jrb5);
/**
* 颜色选择
*/
JRadioButton jrba = new JRadioButton("红色");
JRadioButton jrbb = new JRadioButton("蓝色");
JRadioButton jrbc = new JRadioButton("黑色");
JRadioButton jrbd = new JRadioButton("黄色");
JRadioButton jrbe = new JRadioButton("绿色");
/**
* 设置相颜色形的指令
*/
jrba.setActionCommand("red");
jrbb.setActionCommand("blue");
jrbc.setActionCommand("black");
jrbd.setActionCommand("yellow");
jrbe.setActionCommand("green");
jrba.setBounds(10, 500,70, 50);
jrbb.setBounds(100, 500,70, 50);
jrbc.setBounds(200, 500,70, 50);
jrbd.setBounds(300, 500,70, 50);
jrbe.setBounds(400, 500,70,50);
ButtonGroup group2 = new ButtonGroup(); //组管理,实现颜色单选
group2.add(jrba);
group2.add(jrbb);
group2.add(jrbc);
group2.add(jrbd);
group2.add(jrbe);
jrba.setSelected(true); //默认选择为红色
jf.add(jrba);
jf.add(jrbb);
jf.add(jrbc);
jf.add(jrbd);
jf.add(jrbe);
jf.setDefaultCloseOperation(3);
jf.setVisible(true);
/**
* 获取可以画的区域
*/
Graphics g = jp.getGraphics();
DrawUIListener dl = new DrawUIListener(g, group1, group2);
jp.addMouseListener(dl);
}
}
/**
* 画图的监听器,实现鼠标监听器接口
*/
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
public class DrawUIListener implements MouseListener {
private Graphics g;
private ButtonGroup group1,group2;
private int x1,x2,y1,y2;
String shapetype, colortype;
public DrawUIListener(Graphics g,ButtonGroup group1,ButtonGroup group2) {
this.g = g;
this.group1 = group1;
this.group2 = group2;
}
public void mousePressed(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
ButtonModel model1 = group1.getSelection();
ButtonModel model2 = group2.getSelection();
/**
* 获取相应图形和颜色对应的指令
*/
String commond1 = model1.getActionCommand();
String commond2 = model2.getActionCommand();
shapetype = commond1;
colortype = commond2;
/**
* 判断得到的指令,并执行相应的颜色选择
*/
if(colortype.equals("red")) {
g.setColor(Color.RED);
}
else if(colortype.equals("blue")) {
g.setColor(Color.blue);
}
else if(colortype.equals("black")) {
g.setColor(Color.BLACK);
}
else if(colortype.equals("yellow")) {
g.setColor(Color.YELLOW);
}
else if(colortype.equals("green")) {
g.setColor(Color.GREEN);
}
}
public void mouseReleased(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
/**
* 判断得到的指令,并执行相应的图形选择
*/
if(shapetype.equals("line")) {
drawLine();
}
else if(shapetype.equals("rect")) {
drawRect();
}
else if(shapetype.equals("oval")) {
drawOval();
}
else if(shapetype.equals("3D-rect")) {
draw3DRect();
}
else if(shapetype.equals("Full3DRect")) {
fill3DRect();
}
}
/**
* 画直线的方法
*/
public void drawLine() {
g.drawLine(x1, y1, x2, y2);
}
/**
* 画矩形的方法
* 画矩形四中选择drawRect(x,y,width,height)
* 铭记(x,y)为矩形左上角的坐标,wedth,heigth 为大的减小的
* 1.从左上角到右下角
* 2.从左下角到右上角
* 3.从右下角到左上角
* 4.从右上角到左下角
*/
public void drawRect() {
if(x1<x2&&y1<y2) {
g.drawRect(x1, y1,x2-x1,y2-y1); //从左上角到右下角
}
else if(x1<x2&&y1>y2) {
g.drawRect(x1, y2, x2-x1,y1-y2); //从左下角到右上角
}
else if(x1>x2&&y2>y1) {
g.drawRect(x2, y1, x1-x2,y2-y1); //从右上角到左下角
}
else if(x1>x2&&y1>y2) {
g.drawRect(x2, y2, x1-x2, y1-y2); //从右下角到左上角
}
}
/**
* 画圆或者椭圆的方法
*/
public void drawOval() {
if(x1<x2&&y1<y2) {
g.drawOval(x1, y1,x2-x1,y2-y1); //从左上角到右下角
}
else if(x1<x2&&y1>y2) {
g.drawOval(x1, y2, x2-x1, y1-y2); //从左下角到右上角
}
else if(x1>x2&&y2>y1) {
g.drawOval(x2, y1, x1-x2,y2-y1); //从右上角到左下角
}
else if(x1>x2&&y1>y2) {
g.drawOval(x2, y2, x1-x2, y1-y2); //从右下角到左上角
}
}
/**
* 画3D rect 的方法
*/
public void draw3DRect() {
if(x1<x2&&y1<y2) {
g.draw3DRect(x1, y1,x2-x1,y2-y1,true); //从左上角到右下角
}
else if(x1<x2&&y1>y2) {
g.draw3DRect(x1, y2, x2-x1,y1-y2,true); //从左下角到右上角
}
else if(x1>x2&&y2>y1) {
g.draw3DRect(x2, y1, x1-x2,y2-y1,true); //从右上角到左下角
}
else if(x1>x2&&y1>y2) {
g.draw3DRect(x2, y2, x1-x2, y1-y2,true); //从右下角到左上角
}
}
/**
* 画fill3D rect 的方法
*/
public void fill3DRect() {
if(x1<x2&&y1<y2) {
g.fill3DRect(x1, y1,x2-x1,y2-y1,true); //从左上角到右下角
}
else if(x1<x2&&y1>y2) {
g.fill3DRect(x1, y2, x2-x1,y1-y2,true); //从左下角到右上角
}
else if(x1>x2&&y2>y1) {
g.fill3DRect(x2, y1, x1-x2,y2-y1,true); //从右上角到左下角
}
else if(x1>x2&&y1>y2) {
g.fill3DRect(x2, y2, x1-x2, y1-y2,true); //从右下角到左上角
}
}
/**
* 没有用到的MouseListener接口里面的抽象方法,抽象方法为空
*/
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
}
实现效果 简单图像的绘画: