第四周

2016-12-24  本文已影响0人  我单杀过FAKER

第一天写了一个绘图小窗口

  import java.awt.BorderLayout;
  import java.awt.Color;
  import java.awt.Graphics;
  import java.awt.event.MouseAdapter;
  import java.awt.event.MouseEvent;
  import java.awt.image.BufferedImage;
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.List;
  import javax.imageio.ImageIO;
 import javax.swing.JButton;
  import javax.swing.JColorChooser;
  import javax.swing.JFileChooser;
  import javax.swing.JFrame;
  import javax.swing.JPanel
  import com.huawei.Shape;
  import com.huawei.ShapeFactory;


  @SuppressWarnings("serial")
  public class PaintBrushFrame extends JFrame {
private BufferedImage image = new BufferedImage(666, 666, 1);
private List<Shape> shapesArray = new ArrayList<>();
private String currentType = "线条";
private Color defaultcolor = Color.BLACK;
private int defaultwidth = 9;

public PaintBrushFrame() {
    setTitle("我的绘图工具");
    setSize(666, 666);
    setResizable(false);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JPanel buttonPanel = new JPanel();
    this.add(buttonPanel, BorderLayout.SOUTH);
    String[] buttonNames = { "线条", "矩形", "椭圆", "三角形" };
    for (String name : buttonNames) {
        JButton button = new JButton(name);
        button.addActionListener(e -> {
            currentType = button.getActionCommand();
        });
        buttonPanel.add(button);

    }
    String[] buttonNames2 = { "颜色", "-", "+", "撤销", "清空", "保存" };
    for (String name : buttonNames2) {
        JButton button = new JButton(name);
        button.addActionListener(e -> {
            String command = e.getActionCommand();
            if (command.equals("颜色")) {

                Color currentColor = JColorChooser.showDialog(PaintBrushFrame.this, "请选择颜色", defaultcolor);
                defaultcolor = currentColor != null ? currentColor : defaultcolor;
            } else if (command.equals("+")) {
                if (defaultwidth < 23) {
                    defaultwidth++;
                }
            } else if (command.equals("-")) {
                if (defaultwidth > 1) {
                    defaultwidth--;
                }
            } else if (command.equals("撤销")) {
                if (shapesArray.size() > 0) {
                    shapesArray.remove(shapesArray.size() - 1);
                    repaint();
                }
            } else if (command.equals("清空")) {
                // Java虽然有垃圾回收机制(Garbage Collection
                // 但如果程序编写不当仍有可能造成内存泄漏
                // 垃圾回收是针对内存堆空间的无用对象清理工作
                if (!shapesArray.isEmpty()) {
                    shapesArray.clear();
                    repaint();
                }
            }

            else if (command.equals("保存")) {
                JFileChooser chooser = new JFileChooser();
                int choice = chooser.showSaveDialog(PaintBrushFrame.this);
                if (choice == JFileChooser.APPROVE_OPTION) {
                    BufferedImage newimage = new BufferedImage(666, 666, 1);
                    Graphics graphics = newimage.getGraphics();
                    graphics.setColor(Color.WHITE);
                    graphics.fillRect(0, 0, 666, 666);
                    for (Shape shape : shapesArray) {
                        shape.draw(graphics);
                    }
                    try {
                        ImageIO.write(newimage, "PNG", chooser.getSelectedFile());
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }

            }
        });
        buttonPanel.add(button);

    }
    // 缺省适配模式
    // 给窗口或者窗口上的有三种做法
    // 1,创建匿名内部类的对象(就地实例化
    // 2.创建一个内部类对象来充当监听器(它有 名字随时可以创建对象
    // 3.让窗口实现接口,用窗口对象(this充当监听器
    // 从Java8开始,对于但方法接口可以使用Lambda表达式(λ
    // Lambda表达式其实就是写一个匿名方法来编写事件回调代码
    // 创建匿名内部类的对象
    // 内部类可以直接使用外部类的私有成员(属性和方法
    MouseAdapter adapter = new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent e) {
            int x = e.getX();
            int y = e.getY();
            // 用工厂创建对象(跟具体的图形类型解耦合
            Shape currentShape = ShapeFactory.createShape(currentType);
            currentShape.setColor(defaultcolor);
            currentShape.setLineWidth(defaultwidth);
            currentShape.setStartX(x);
            currentShape.setStartY(y);
            currentShape.setEndX(x);
            currentShape.setEndY(y);
            shapesArray.add(currentShape);

        }

        @Override
        public void mouseDragged(MouseEvent e) {

            Shape currentShape = shapesArray.get(shapesArray.size() - 1);
            int x = e.getX();
            int y = e.getY();
            currentShape.setEndX(x);
            currentShape.setEndY(y);
            repaint();

        }
    };
    this.addMouseListener(adapter);
    this.addMouseMotionListener(adapter);

}

@Override
public void paint(Graphics g) {
    Graphics otherGraphics = image.getGraphics();
    super.paint(otherGraphics);
    for (Shape shape : shapesArray) {
        shape.draw(otherGraphics);
    }
    g.drawImage(image, 0, 0, null);
}

public static void main(String[] args) {
    new PaintBrushFrame().setVisible(true);
}

}

怎么样去实现图形的画法就省略了,做一个绘图工具看似简单,但是对于现阶段的我来说还是相当困难的,还是得回去抄抄这个代码,多去理解老师写代码的思路,练好英语。

第二,三天的集合听的也是挺懵比的,集合的用处还是非常大的,List,Set。还得重写方法,真的是爆炸啊。

这个正则表达式还是非常神奇的,反正不当搞得懂,老师都说了,去网上找,不需要自己写,只要懂原理就行了

上一篇下一篇

猜你喜欢

热点阅读