JAVA画图板03 —— 颜色的添加

添加颜色

  • 在之前的基础上,我希望能实现改变颜色的功能,但是创建的选色按钮应该和画图按钮加以区分,一个做法是对两种按钮采取不同的编号,这里就需要自定义一个MyButton类来取代JButton。
class MyButton extends JButton
{
	static final SHAPE_BUTTON = 1;
	static final COLOR_BUTTON = 2;

	private int btnID;

	public MyButton(){ super(); }
  
    public MyButton(String str){ super(str); }
  	
  	public int getBtnID(){ return this.btnID }

	public setBtnID(int id){ this.btnID = id }
}
package Test;

import javax.swing.*;
import java.awt.*;

public class MyShapeUI extends JFrame
{
    ShapeListener s = new ShapeListener();
    
    String[] shapeName = {"直线","矩形","圆","填充矩形","填充圆",
            "三角形","多边形","等腰三角形","铅笔"};

    Color[] colors =
            {Color.BLACK,Color.WHITE,Color.GRAY,Color.LIGHT_GRAY,Color.DARK_GRAY,
            Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW,Color.ORANGE,Color.CYAN,
             Color.PINK,Color.MAGENTA,new Color(0,150,0),new Color(150,0,0)
            };

    public void initUI()
    {
        this.setTitle("绘图界面");
        this.setSize(600, 600);
        this.setLayout(new FlowLayout());

        for(int i = 0; i < shapeName.length; i++)
       {
            MyButton btn = new MyButton(shapeName[i]);
            btn.setBackground(Color.white);
            btn.setBtnID(MyButton.SHAPE_BUTTON);
            this.add(btn);
            btn.addActionListener(s);
        }
		
		for(int i = 0; i < colors.length; i++)
        {
            MyButton btn = new MyButton();
            btn.setBtnID(MyButton.COLOR_BUTTON);
            btn.setBackground(colors[i]);
            btn.setPreferredSize(new Dimension(35, 35));
            this.add(btn);
            btn.addActionListener(s);
        }
        
        this.setVisible(true);

        this.addMouseListener(s);
        s.setGraphics(this.getGraphics());
    }

    public static void main(String[] args) {
        MyShapeUI myShapeUI = new MyShapeUI();
        myShapeUI.initUI();
    }

}

package Test;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class ShapeListener implements MouseListener,
        ActionListener
{
    //ActionListener
    String action = " ";
    Color color;

    public void actionPerformed(ActionEvent e)
    {
        MyButton btn = (MyButton) e.getSource();

        if(btn.getBtnID() == MyButton.SHAPE_BUTTON)
        {
            action = btn.getText();
        } else if (btn.getBtnID() == MyButton.COLOR_BUTTON) {
            color = btn.getBackground();
            g.setColor(color);
        }
    }

    private int x1, y1, x2, y2, x3, y3, x4, y4, x5, y5 ;
    Graphics g = null;
    public void setGraphics(Graphics g){ this.g=g; }

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

        switch(action)
        {
            case "直线": g.drawLine(x1, y1, x2, y2);break;
            case "矩形": g.drawRect(Math.min(x1, x2),Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));break;
            case "圆": g.drawOval(Math.min(x1, x2),Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));break;
            case "填充矩形" : g.fillRect(Math.min(x1, x2),Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));break;
            case "填充圆" : g.fillOval(Math.min(x1, x2),Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));break;
            case "等腰三角形": drawIsoTriangle(x1, y1, x2, y2, g);break;
        }
    }
    //等腰三角形的实现
    public void drawIsoTriangle(int x1, int y1, int x2, int y2, Graphics g)
    {
        g.drawLine(Math.min(x1, x2), Math.max(y1, y2), Math.max(x1, x2), Math.max(y1, y2));
        g.drawLine(Math.min(x1, x2), Math.max(y1, y2), (x1 + x2)/2, Math.min(y1, y2));
        g.drawLine(Math.max(x1, x2), Math.max(y1, y2), (x1 + x2)/2, Math.min(y1, y2));
    }

    int cnt = 0;
    public void mouseClicked(MouseEvent e)
    {
        if(action.equals("三角形"))
        {
            if(cnt == 0)
            {
                x3 = e.getX();
                y3 = e.getY();
                cnt++;
            }else if(cnt == 1)
            {
                x4 = e.getX();
                y4 = e.getY();
                g.drawLine(x3, y3, x4, y4);
                cnt++;
            }else if(cnt == 2)
            {
                x5 = e.getX();
                y5 = e.getY();
                g.drawLine(x5, y5, x3, y3);
                g.drawLine(x4, y4, x5, y5);
                cnt = 0;
            }
        }
        else if(action.equals("多边形"))
        {
            if(cnt == 0)
            {
                x3 = e.getX();
                y3 = e.getY();
                cnt++;
            }else if(cnt == 1)
            {
                x4 = e.getX();
                y4 = e.getY();
                g.drawLine(x3, y3, x4, y4);
                cnt++;
            }else if(cnt == 2)
            {
                x5 = e.getX();
                y5 = e.getY();
                g.drawLine(x4, y4, x5, y5);
                x4 = x5;
                y4 = y5;
                if(e.getButton() == MouseEvent.BUTTON3)	
                {
                    cnt = 0;
                    g.drawLine(x3, y3, x5, y5);
                }
            }
        }
    }

    public void mouseEntered(MouseEvent e){}

    public void mouseExited(MouseEvent e){}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值