重绘-刷新的程序语言

画板实现重绘和橡皮擦


重绘的实现主要在首先要自定义一个通用队列 然后再处理鼠标监听的
类中 实例化静态一个队列对象 以方便直接调用 然后画完各种图形后
将画图的对象加载到队列中保存 然后再重写paint函数 首先调用父类的paint函数
再利用循环调用加载在队列中的画图对象

橡皮擦首先是创建按钮 然后按处理曲线的方法一样处理监听器 唯一的不同之处是
首先在传递颜色的参数时 将颜色定义成画布颜色一致 RGB的(238 238 238)


package net.java.t071301;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;


public class DrawFrame extends JFrame{

public static void main (String args []){

DrawFrame df = new DrawFrame();
df.init();
}

public void init() {
this.setTitle("简易画板");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(2);
this.setSize(600, 400);
this.setResizable(false);
FlowLayout fl= new FlowLayout();
this.setLayout(fl);
JButton jbt1= new JButton("直线");
jbt1.setActionCommand("line");
this.add(jbt1);
JButton jbt2= new JButton("曲线");
jbt2.setActionCommand("curve");
this.add(jbt2);
JButton jbt3= new JButton("圆形");
jbt3.setActionCommand("oval");
this.add(jbt3);
JButton jbt4= new JButton("矩形");
jbt4.setActionCommand("rect");
this.add(jbt4);
JButton jbt5= new JButton("填充圆");
jbt5.setActionCommand("filloval");
this.add(jbt5);
JButton jbt6= new JButton("填充矩形");
jbt6.setActionCommand("fillrect");
this.add(jbt6);
JButton jbt7= new JButton("颜色");
jbt7.setActionCommand("color");
this.add(jbt7);
JButton jbt8= new JButton("橡皮擦");
jbt8.setActionCommand("erase");
this.add(jbt8);

ActionListener al = new ActionListener(){

public void actionPerformed(ActionEvent e) {

if(e.getActionCommand()=="color" ){
color = JColorChooser.showDialog(null,"颜色选择器",Color.BLACK);
}
else{
items = e.getActionCommand();
}
}
};

jbt1.addActionListener(al);
jbt2.addActionListener(al);
jbt3.addActionListener(al);
jbt4.addActionListener(al);
jbt5.addActionListener(al);
jbt6.addActionListener(al);
jbt7.addActionListener(al);
jbt8.addActionListener(al);

this.setVisible(true);

Graphics g = this.getGraphics();

DrawListener dl = new DrawListener(g,this);

this.addMouseListener(dl);
this.addMouseMotionListener(dl);

}
//重写一个绘制方法
public void paint(Graphics g){
super.paint(g);
for(int i=0;i<DrawListener.qi.size();i++){
Draw draw=DrawListener.qi.get(i);
draw.draw(g);
}

}
public Color getColor(){
return color;
}
public String getItems(){
return items;
}
private Color color =Color.BLACK;
private String items="line";
}



package net.java.t071301;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class DrawListener implements MouseListener, MouseMotionListener{

private Graphics g;
private DrawFrame df;
int x1,y1,x2,y2;
public static QueueIplm<Draw> qi=new QueueIplm<Draw> ();
public DrawListener(Graphics g , DrawFrame df){
this.g=g;
this.df=df;
}

public void mousePressed(MouseEvent e) {
x1=e.getX();
y1=e.getY();
}

public void mouseReleased(MouseEvent e) {
x2=e.getX();
y2=e.getY();
Draw draw;
if(df.getItems().equals("line")){
draw = new DrawLine(x1,y1,x2,y2,df.getColor());
draw.draw(g);
qi.add(draw);
}
else if(df.getItems().equals("oval")){
draw = new DrawOval(x1,y1,Math.abs(x2-x1), Math.abs(y2-y1),df.getColor());
draw.draw(g);
qi.add(draw);
}
else if(df.getItems().equals("rect")){
draw = new DrawRect(x1,y1,Math.abs(x2-x1), Math.abs(y2-y1),df.getColor());
draw.draw(g);
qi.add(draw);
}
else if(df.getItems().equals("fillrect")){
draw = new DrawFillRect(x1,y1,Math.abs(x2-x1), Math.abs(y2-y1),df.getColor());
draw.draw(g);
qi.add(draw);
}
else if(df.getItems().equals("filloval")){
draw = new DrawFillOval(x1,y1,Math.abs(x2-x1), Math.abs(y2-y1),df.getColor());
draw.draw(g);
qi.add(draw);
}

}
public void mouseDragged(MouseEvent e) {
x2=e.getX();
y2=e.getY();
Draw draw;
if(df.getItems().equals("curve")){
draw = new DrawLine(x1,y1,x2,y2,df.getColor());
draw.draw(g);
x1=x2;
y1=y2;
qi.add(draw);}
else if(df.getItems().equals("erase")){
Color cr = new Color(238,238,238);
float[] hsbvals = null;
float[] objx=cr.RGBtoHSB(238, 238, 238, hsbvals);
float a=objx[0];
float b=objx[1];
float c=objx[2];
draw = new DrawLine(x1,y1,x2,y2,Color.getHSBColor(a, b, c));
draw.draw(g);
x1=x2;
y1=y2;
qi.add(draw);
}
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mouseMoved(MouseEvent e) {
}

public void mouseClicked(MouseEvent e) {
}
}



ackage net.java.t071301;

import java.awt.Graphics;

public abstract class Draw {

public abstract void draw(Graphics g);
}


package net.java.t071301;

import java.awt.Color;
import java.awt.Graphics;

public class Drawerase extends Draw{
private Color color;
private int x1,y1,x2,y2;

public Drawerase(int x1,int y1,int x2,int y2,Color color){
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
this.color=color;
}

public void draw(Graphics g) {
g.setColor(color);
g.drawLine(x1, y1, x2, y2);
}

}


ackage net.java.t071301;

import java.awt.Color;
import java.awt.Graphics;

public class DrawFillOval extends Draw{
private int x1,y1,x2,y2;
private Color color;

public DrawFillOval(int x1,int y1,int x2,int y2, Color color){
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
this.color=color;
}

public void draw(Graphics g) {
g.setColor(color);
g.fillOval(x1, y1, x2, y2);
}

}



package net.java.t071301;

import java.awt.Color;
import java.awt.Graphics;

public class DrawFillRect extends Draw{
private int x1,y1,x2,y2;
private Color color;

public DrawFillRect(int x1,int y1,int x2,int y2, Color color){
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
this.color=color;
}

public void draw(Graphics g) {
g.setColor(color);
g.fillRect(x1, y1, x2, y2);
}

}




ackage net.java.t071301;

import java.awt.Color;
import java.awt.Graphics;

public class DrawLine extends Draw{
private int x1,y1,x2,y2;
private Color color;
public DrawLine(int x1,int y1,int x2,int y2, Color color){
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
this.color=color;
}

public void draw(Graphics g) {
g.setColor(color);
g.drawLine(x1, y1, x2, y2);
}

}



package net.java.t071301;

import java.awt.Color;
import java.awt.Graphics;

public class DrawOval extends Draw{
private int x1,y1,x2,y2;
private Color color;

public DrawOval(int x1,int y1,int x2,int y2, Color color){
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
this.color=color;
}

public void draw(Graphics g) {
g.setColor(color);
g.drawOval(x1, y1, x2, y2);
}

}


package net.java.t071301;

import java.awt.Color;
import java.awt.Graphics;

public class DrawRect extends Draw{
private int x1,y1,x2,y2;
private Color color;

public DrawRect(int x1,int y1,int x2,int y2, Color color){
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
this.color=color;
}

public void draw(Graphics g) {
g.setColor(color);
g.drawRect(x1, y1, x2, y2);
}

}




package net.java.t071301;

public interface Queue<E> {

//得到指定位置的对象
public E get(int index);

//加入一个对象
public void add(E e);

//在指定的位置插入一个对象
public void insert (E e, int index);

public void addall(QueueIplm<E> list);

//删除指定位置的对象
public E delete(int index);

//得到队列中的元素
public int size();
}


package net.java.t071301;

public class QueueIplm<E> implements Queue<E>{

private Object[] obj=null;
private int initCount =0;
private int incresCount =1;

public QueueIplm(){
obj = new Object[0];
}
public QueueIplm(int initCount){
this.initCount=initCount;
obj = new Object[initCount];
}
public QueueIplm(int initCount, int incresCount){
this.initCount=initCount;
this.incresCount=incresCount;
obj = new Object[initCount];
}


public void add(E e) {
Object[] temp = new Object[obj.length + incresCount];
for (int i = 0; i < obj.length; i++) {
temp[i] = obj[i];
}
temp[obj.length] = e;
this.initCount++;
obj = temp;
}

public void addall(QueueIplm<E> list) {
Object[] temp = new Object[obj.length + list.size()];
for (int i = 0; i < obj.length; i++) {
temp[i] = obj[i];
}
for (int i = 0; i < list.size(); i++) {
temp[i + obj.length] = list.get(i);
this.initCount++;
}
obj = temp;

}


public E delete(int index) {
E item = (E) obj[index];
for (int i = index; i < obj.length; i++) {
obj[i] = obj[i + 1];
}
this.initCount--;
return item;
}
public E get(int index) {
if (index < 0 || index > initCount)
return null;
E item = (E) obj[index];
return item;

}


public void insert(E e,int index) {
obj[index] = e;
}


public int size(){
return initCount;

}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值