java应用——高仿XP画板(二:实现部分监听)

来源:互联网 发布:大连爱知时计科技 编辑:程序博客网 时间:2024/05/05 10:29

普通类,抽象类,接口:
1)类方法:普通方法,抽象方法,构造方法
2)类分类:普通类,抽象类,接口
3)其中,抽象类和接口起到被继承时约束子类的方法定义
接口起到绝对约束,即接口内的所有抽象方法都要被重写;
抽象类中的抽象方法被继承时需要被重写,抽象类在普通类和接口之间起到适配器的作用。
监听器:
1)定义:负责监听事件源上发生的事件,并对各种事件作出相应处理的对象(对象中包含 事件处理器)。
2)分类:事件监听器,鼠标监听器,键盘监听器
事件监听器:ActionListener,主要是加在按钮上(不包括单选按钮,单选按钮组有强大 的功能)
鼠标监听器:MouseListener、MouseMotionListener、MouseWheelListener,主要加在 容器组件上(JFrame,JWindow,Jpanel),监听指定范围内鼠标的动作(按下,释放, 点击,移动……)
*什么情况下形成点击的操作:1.按下和释放的动作合起来 2.按下和释放的在同一处
键盘监听器:KeyListener,监听指定范围内键盘的动作(按下,释放)
3)案例:从一个界面转换到另一个界面
1.首先创建一个窗体和一个按钮

public class CanvasJframe extends JFrame{public static void main(String[] args) {JFrame jf=new JFrame();jf.setSize(200,200);//事件监听主要监听的是按钮(JButton,JRadioButton,JMenuItem(JMenu有反应机制))MyActtionListener myActtionListener=new MyActtionListener();JButton jButton=new JButton("详情请点击");jf.add(jButton);jButton.addActionListener(myActtionListener);jf.setVisible(true);}}

2.创建MyActtionListener类

public class MyActtionListener implements ActionListener{    public void actionPerformed(ActionEvent e) {        JFrame jFrame=new JFrame("欢迎");        jFrame.setSize(900,600);        jFrame.setLocationRelativeTo(null);        jFrame.setDefaultCloseOperation(1);        jFrame.setVisible(true);    }}

各类之间的参数传递:
这里写图片描述
代码实现:
上一节将XP画板的界面基本上实现完,在这一节将实现西边区域部分画笔的功能,颜料盘调颜色的功能还有画布的功能;
1.图形绘制的实现

//从画布获取画笔        Graphics graphicsBrush=jPanelCenter.getGraphics();        //添加画布的鼠标监听器        WestGraphListener westGraphListener=new WestGraphListener(graphicsBrush,buttonGroupPen,jButtonPen);        jPanelCenter.addMouseListener(westGraphListener);

画笔类:Graphics 特殊:主要用于绘制图形功能,对象必须从指定的容器组件上获取
画笔获取必须在窗体的setVisible之后获取
WestGraphListener类:

import java.awt.Graphics;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.ButtonGroup;import javax.swing.ButtonModel;import javax.swing.JButton;public class WestGraphListener implements MouseListener{    public Graphics graphicsBrush;    public ButtonGroup buttonGroupPen;    public JButton jButtonPen;    public String command;    public WestGraphListener(Graphics graphicsBrush,ButtonGroup buttonGroupPen,JButton jButtonPen) {        this.graphicsBrush=graphicsBrush;        this.buttonGroupPen=buttonGroupPen;        this.jButtonPen=jButtonPen;    }    public int x1,x2,y1,y2;    public void mousePressed(MouseEvent e) {        x1=e.getX();        y1=e.getY();        ButtonModel buttonModel=buttonGroupPen.getSelection();        command=buttonModel.getActionCommand();        //画笔设置颜色        graphicsBrush.setColor(jButtonPen.getBackground());    }    public void mouseReleased(MouseEvent e) {        x2=e.getX();        y2=e.getY();        if(command.equals("jpg-10")) {            graphicsBrush.drawLine(x1, y1, x2, y2);        }else if(command.equals("jpg-12")){            graphicsBrush.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));        }else if(command.equals("jpg-14")) {            graphicsBrush.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));        }else if(command.equals("jpg-15")){            graphicsBrush.drawRoundRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2), 20,20);        }    }    //用不到的类方法    public void mouseEntered(MouseEvent e) {            }    public void mouseExited(MouseEvent e) {         }    public void mouseClicked(MouseEvent e) {        }}

2.西边区域部分画笔的实现:
因为在西边区域的画笔用的是单选按钮组来实现,而单选按钮组有强大的功能可以获取 到事件源并不需要绑定监听器。
所以在WestGraphListener类中这样实现

ButtonModel buttonModel=buttonGroupPen.getSelection();    command=buttonModel.getActionCommand();

3.南边颜料盘的实现

import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;public class SouthColorListener implements ActionListener{    public JButton jButtonPen;    public SouthColorListener(JButton jButtonPen) {        this.jButtonPen=jButtonPen;    }    public void actionPerformed(ActionEvent e) {        //获取事件源        JButton jButtonPigment=(JButton)e.getSource();        Color color=jButtonPigment.getBackground();        jButtonPen.setBackground(color);    }}

至此,实现了部分监听。

原创粉丝点击