画板总结

1.引入的包import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
2.程序 Hu.javapublic class Hu extends javax.swing.JFrame
{  
//创建一个用来保存画过的形状的对象
NJListImpl<Shap> sh = new NJListImpl<Shap>();
/**
* 主函数
*/
public static void main(String args[])
{ Hu h=new Hu();
      h.tui();
    }
//重写父类绘制窗体的方法
public void paint(Graphics g)
{ //调用父类的方法来绘制窗体
super.paint(g);
//将队列中的形状取出来绘制
for(int i=0;i<sh.size();i++)
{//根据下标取出一个形状对象
Shap s = sh.get(i);
//绘制
s.draw(g);
}
}                             
//显示界面的方法
public void tui()
{  this.setTitle("画板"); //设置窗口标题
   this.setSize(400,400); //设置窗口大小
   this.setLayout(new FlowLayout());  //设置窗口布局
   ButtonGroup group=new ButtonGroup();  //创建选择组对象
   JRadioButton line = new JRadioButton("直线");
line.setActionCommand("直线");
line.setSelected(true);// 默认选中
   JRadioButton rect = new JRadioButton("矩形");
rect.setActionCommand("矩形");
    JRadioButton oval = new JRadioButton("椭圆");
oval.setActionCommand("椭圆");
JRadioButton r= new JRadioButton("fillRect");
r.setActionCommand("fillRect");
JRadioButton o = new JRadioButton("fillOval");
o.setActionCommand("fillOval");
  // 将五个单选按钮分为一个组
group.add(line);
group.add(rect);
group.add(oval);
group.add(r);
group.add(o);
//将单选按钮加到界面上
this.add(line);
this.add(rect);
this.add(oval);
this.add(r);
this.add(o);
    this.setLocationRelativeTo(null);  //窗体居中显示
   //this.setLocation(x, y);//根据指定的x、y坐标来显示窗体
    this. setDefaultCloseOperation(3);//表示点击关闭按钮的时候,窗体隐藏,并且关闭程序
    this. setVisible(true);
   this. validate();
   Graphics g=this.getGraphics();
//获取窗体上的画布对象。注意:要获取窗体的画布对象,必须在窗体可见后才可以获取
      //创建AListener 类的对象,画图形的事件的处理类
    ArListener  l=new ArListener(g, group,sh);
   //事件源是组件,窗体组件
   //给事件源添加一个鼠标的监听器方法
    this.addMouseListener(l);
    }
}

Shap.java
public abstract class Shap {
/**
* 绘制的方法
* @param g
*/
public abstract void draw(Graphics g);
}

NJListl.java
public interface NJListl<E> {
/**
* 定义一个添加元素的方法
*/
public void add(E e);

/**
* 定义一个返回指定索引位值得方法
*/
public E get(int index);

/**
* 定义一个返回值存储元素个数的方法
*/
public int size();

}

NJListImpl.java
public class NJListImpl<E> implements NJListl<E> {
/**
* 定义一个添加元素的方法
*/
public void add(E e){
//定义一个新数组对象
Object temp [] = new Object[this.stu.length+1];
//循环将原始数组中的值拷贝到新的数组中
for(int i=0;i<this.stu.length;i++){
temp[i] = this.stu[i];
}
//将要添加的值,追加到新数组的末尾
temp[this.stu.length] = e;
//将新数组赋给原始数组
this.stu = temp;
}
/**
* 定义一个返回指定索引位值得方法
*/
public E get(int index){
return (E)stu[index];
}
/**
* 定义一个返回值存储元素个数的方法
*/
public int size(){
return stu.length;
}
//定义一个数组
private Object stu [] = new Object[0];
}


ArListener.java
public class ArListener implements MouseListener
{   // 定义四个整数变量,来保存坐标值
private int x1, y1, x2, y2;
// 定义一个计数器
//private int count = 0;
// 定义一个画布对象
private Graphics g;
private ButtonGroup group;
private NJListImpl<Shap> sh;
    // 定义一个带参数的构造函数
public ArListener(Graphics g) {
this.g = g;
}
public ArListener(Graphics g,javax.swing.ButtonGroup group,NJListImpl<Shap> shapes){
this.g = g;
this.group = group;
sh = shapes;
}

// 鼠标按下的事件
public void mousePressed(MouseEvent e)
{
// 得到鼠标按下时的坐标值
x1 = e.getX();
y1 = e.getY();
}
// 鼠标释放的事件
public void mouseReleased(MouseEvent e) {
// 得到鼠标释放时的坐标值
x2 = e.getX();
y2 = e.getY();
//得到选中的形状
String str = group.getSelection().getActionCommand();
Shap s=null;
if("直线".equals(str)){
s= new Line(x1,y1,x2,y2);
}
if("矩形".equals(str)){
s = new Rect(x1,y1,x2,y2);
}
if("椭圆".equals(str)){
s = new Oval(x1,y1,x2,y2);
}
if("fillRect".equals(str)){
s = new FillRect(x1,y1,x2,y2);
}
if("fillOval".equals(str)){
s = new FillOval(x1,y1,x2,y2);
}
s.draw(g);
//将画过的形状对象保存到队列
sh.add(s);
}
 
public void mouseEntered(MouseEvent e)
{} //设置窗口标题
public void mouseExited(MouseEvent e)
{}// 鼠标退出的事件
public void mouseClicked(MouseEvent e)
{}// 鼠标单击的事件
}

Line.javapublic class Line extends Shap{
private int x1,y1,x2,y2;
public Line(int x1,int y1,int x2,int y2 ){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public  void draw(Graphics g){
         g.setColor(Color.red);  //设置画布颜色
g.drawLine(x1, y1, x2, y2);
}
}

Rect.java
public class Rect extends Shap{
  private int x1,y1,x2,y2;
  public Rect(int x1,int y1,int x2,int y2 ){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public void draw(Graphics g){
  g.setColor(Color.red );
  if(x1<x2)
   { if(y1<y2) g.drawRect(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1));
     else  g.drawRect(x1, y2, Math.abs(x2-x1), Math.abs(y2-y1));
   }
else
   { if(y1<y2) g.drawRect(x2, y1, Math.abs(x2-x1), Math.abs(y2-y1));
      else  g.drawRect(x2, y2, Math.abs(x2-x1), Math.abs(y2-y1));
}
   }
}
Oval.java
public class Oval extends Shap
{    private int x1,y1,x2,y2;
     public Oval(int x1,int y1,int x2,int y2 ){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
     public  void draw(Graphics g){
     g.setColor(Color.red);  //设置画布颜色
      if(x1<x2)
     { if(y1<y2) g.drawOval(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1));
       else  g.drawOval(x1, y2, Math.abs(x2-x1), Math.abs(y2-y1));
     }
     else
     { if(y1<y2) g.drawOval(x2, y1, Math.abs(x2-x1), Math.abs(y2-y1));
       else  g.drawOval(x2, y2, Math.abs(x2-x1), Math.abs(y2-y1));
     }
  }
}

FillRect.javapublic class FillRect extends Shap
{  private int x1,y1,x2,y2;
   public FillRect(int x1,int y1,int x2,int y2 ){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
   public  void draw(Graphics g){
   g.setColor(Color.blue);  //设置画布颜色
   if(x1<x2)   //画填充的矩形
   { if(y1<y2) g.fillRect(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1));  
     else  g.fillRect(x1, y2, Math.abs(x2-x1), Math.abs(y2-y1));
   }
   else
   { if(y1<y2) g.fillRect(x2, y1, Math.abs(x2-x1), Math.abs(y2-y1));
     else  g.fillRect(x2, y2, Math.abs(x2-x1), Math.abs(y2-y1));
   }
  }
}
FillOval.javapublic class FillOval extends Shap
{   private int x1,y1,x2,y2;
    public FillOval(int x1,int y1,int x2,int y2 ){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
    public  void draw(Graphics g){
     g.setColor(Color.green); //设置画布颜色
      if(x1<x2)//画填充的椭圆
     {if(y1<y2) g.fillOval(x1,y1,Math.abs(x2-x1),Math.abs(y2-y1));   
      else  g.fillOval(x1, y2, Math.abs(x2-x1), Math.abs(y2-y1));
     }
     else
     { if(y1<y2) g.fillOval(x2, y1, Math.abs(x2-x1), Math.abs(y2-y1));
       else  g.fillOval(x2, y2, Math.abs(x2-x1), Math.abs(y2-y1));
     }
   }
}

你可能感兴趣的:(java,swing)