• 画图,橡皮


    import javax.swing.*;
    import java.applet.Applet;
    import java.awt.event.*;
    import java.awt.*;
    import java.util.Vector;
    public class Exec76 extends Applet {
        Graphics2D g2 ;
        Vector<Point> points = new Vector<Point>();
        Polygon po = new Polygon();
        Boolean eraser = false;
        public JButton bt;
        int x=0,y=0;
        int w = 20;
        public Exec76() {
            addMouseMotionListener(new MLis());
             bt = new JButton("Draw");
            bt.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e ) {
                    if(!eraser) { 
                        bt.setText("Eraser");
                        eraser = true;
                        repaint();
                    }
                    else {
                        bt.setText("Draw");
                        eraser = false;
                        repaint();
                    }
                }
            });
            
            
        }
        public void init(){
            addMouseMotionListener(new MLis());
             bt = new JButton("Draw");
            bt.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e ) {
                    if(!eraser) { 
                        bt.setText("Eraser");
                        eraser = true;
                        repaint();
                    }
                    else {
                        bt.setText("Draw");
                        eraser = false;
                        repaint();
                    }
                }
            });
            add(bt);
        }
        public void paint(Graphics g) {
            g2 = (Graphics2D)g;
            if (eraser) { 
                Rectangle r = new Rectangle(x,y,w,w);
                g2.setColor(Color.BLUE);
                g2.fill(r);
                for(int i=0;i<points.size();i++) {
                    if (r.contains(points.get(i))) points.remove(i);
                }
            }
            g2.setColor(Color.BLACK);
            for(int i=0;i<points.size();i++) {
                g2.fillOval((int)points.get(i).getX(), (int)points.get(i).getY(), 4, 4);
            }
    
        }
    
        class MLis extends MouseAdapter {
            public void mouseDragged(MouseEvent e) {
                if (eraser) { x = e.getX(); y = e.getY();repaint();}
                else {
                    points.add(new Point(e.getX(),e.getY()));
                    repaint();
                }
            }
        }
        
        public static void main(String[] args) {
            JFrame f = new JFrame("Exec76");
            f.setSize(400,400);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setLocationRelativeTo(null);
            Exec76 panel = new Exec76();
            f.add(panel.bt,BorderLayout.NORTH);
            f.add(panel);
            f.setVisible(true);
            
        }
    
    }
  • 相关阅读:
    MPLS 知识要点1
    ISIS的SSN和SRM标志
    对比ISIS和OSPF
    ISIS帧中继实验
    ISIS 认证实验
    ISIS数据库同步
    ISIS Lab 路由泄露
    ISIS Lab 重分布直连
    32、端口有效范围是多少到多少?
    33、为何需要把 TCP/IP 协议栈分成 5 层(或7层)?开放式回答。
  • 原文地址:https://www.cnblogs.com/qqjue/p/2630142.html
Copyright © 2020-2023  润新知