• 画图板中的画图工具的使用


    重绘指的是,将画出来的图形进行保存,重绘分为三种:记坐标,记点,记步骤。

    我们在此主要讲习记坐标的方法;

    如果我们想要画出一个直线,最主要的是要记录它的两个坐标,所以我们应该建立一个数组来记录,这两个坐标。

    代码如下:

    public class Shape {

    int x1,y1,x2,y2;

    String name;

    Color color;

    public Shape(int x1, int y1, int x2, int y2) {

    this.x1 = x1;

    this.y1 = y1;

    this.x2 = x2;

    this.y2 = y2;

    }

    }

    //这里就需要重新建一个类来保存坐标。这一步应该为第一步,应首先建立一个类

    public class DrawFrame extends JPanel {

    Shape[] shape = new Shape[20];

    //这里建立一个shape数组;同时extends为的是要重写那个JPanel中的方法;

    // 设计左侧的布局

    // JPanel jp2 = new JPanel();

    this.setPreferredSize(new Dimension(850, 700));

    this.setBackground(Color.WHITE);

    jf.add(this);

    /***********/

    this.addMouseListener(lg);

    this.addMouseMotionListener(lg);

    这里的this指的是我们的面板,我们在重写方法的时候要吧画图的那个JPanel来用this的方法使用。

    public void paint(Graphics g) {

    super.paint(g);

    for (int i = 0; i < shape.length; i++) {

    if (shape[i] != null) {

    if ("直线".equals(shape[i].name)) {

    g.setColor(shape[i].color);

    g.drawLine(shape[i].x1, shape[i].y1, shape[i].x2,

    shape[i].y2);

    }

    if ("圆形".equals(shape[i].name)) {

    g.setColor(shape[i].color);

    g.drawOval(Math.min(shape[i].x1, shape[i].x2), Math.min(shape[i].y1, shape[i].y2), Math.abs(shape[i].x1 - shape[i].x2),

    Math.abs(shape[i].y1 - shape[i].y2));

    }

    if ("正方形".equals(shape[i].name)) {

    g.setColor(shape[i].color);

    g.drawRect(Math.min(shape[i].x1,shape[i].x2), Math.min(shape[i].y1, shape[i].y2), Math.abs(shape[i].x1 - shape[i].x2),

    Math.abs(shape[i].y1 - shape[i].y2));

    }

    }

    }

    }

    //这里的是最重要的一步重写,是重写JPanel中的方法,paint方法是在JPanel中自动调用的。所以不用刻意添加。

    private Shape[] shape;

    private int index;

    public void setShape(Shape[] shape) {

    this.shape = shape;

    }

    //这里就是在监听类中添加方法,这里是将shape传到监听机制的类中。

    Shape line = new Shape(x1, y1, x4, y4);

    line.name = "直线";

    line.color = co;

    shape[index] = line;

    index++;

    //接着就是在每一个方法中添加那个数组来保存坐标。

    这样重写就完成了,谢谢!

  • 相关阅读:
    回测框架pybacktest简介(二)
    量化分析:把Tushare数据源,规整成PyalgoTrade所需格式
    CtaAlgo vs PyAlgoTrade
    Yahoo! Finance财经数据PYTHON临时读取方法
    linux screen 命令详解(转载)
    linux screen 命令详解
    centos7 安装mysql--python模块出现EnvironmentError: mysql_config not found和error: command 'gcc' failed with exit status 1
    redis安装全过程
    (转)ZooKeeper-3.3.4集群安装配置
    windows下 两个版本的JDK环境变量进行切换 MARK
  • 原文地址:https://www.cnblogs.com/chang1203/p/5664930.html
Copyright © 2020-2023  润新知