⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 picpanel.java

📁 Java门电路画图器 基本功能: 1。文件的保存
💻 JAVA
字号:
package ding;

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

/**
 * <p>Title: 门电路画图器</p>
 * <p>Description: www.1shu.net</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: EyeSome</p>
 * @author ES
 * @version 1.0
 */

public class picPanel
    extends JPanel
    implements MouseMotionListener, MouseListener, Runnable {
  // 定义作图区域的大小
  int x1 = 2, x2 = 1023, y1 = 2, y2 = 669;
  int i, j;
  int flag=0;
  int icount, jcount;
  public int mouseX, mouseY;
  public boolean selectFlag = false; // 元件是否选中
  public int inRange = -1; // 在哪个元件的坐标范围内,值表示元件数组的下标
  int bwX;
  int bwY;
  /** 定义画线变量 */
  int lineX1, lineY1, lineX2, lineY2;
  int drawLineNo = 0; // 线的根数
  public boolean drawLine = false, lineBg = false, lineEnd = false;
  //yjLine forDrawLine[] = new yjLine[100];

  /** 定义画点变量 */
  int pointX, pointY;
  boolean drawPoint;

  /** 定义画字符变量 */
  int charX, charY;
  boolean drawChar;


  Thread runner;
  mainFrame mFrame;
  public picPanel(mainFrame frame) {
    mFrame = frame;
    if (runner == null) {
      runner = new Thread(this);
      runner.start();
    }
  }

  public void paintComponent(Graphics g) {

    Graphics2D g2D = (Graphics2D) g;
    super.paintComponent(g);
    /////////////////
    //   画背景
    Color cbg = new Color(0x00, 0x00, 0x33);
    g2D.setPaint(cbg);
    Rectangle2D.Float rcBG = new Rectangle2D.Float(x1, y1, x2 - x1, y2 - y1);
    g2D.fill(rcBG);
    ////////////////
    //   布点
    Color pointC = new Color(0x70, 0x70, 0x70);
    g2D.setColor(pointC);
    icount = (x2 - x1) / 8;
    jcount = (y2 - y1) / 8;
    for (i = x1; i < icount; i++) {
      for (j = y1; j < jcount; j++) {
        g2D.drawLine(i * 8, j * 8, i * 8, j * 8);
      }
    }

    /**  画所有元件 */
    for (i = 0; i < 1000; i++) {
      if (mFrame.yjc[i] != null) {
        mFrame.yjc[i].yjpaint(g2D);
      }
    }

    /** 如果在元件范围内,画虚框*/
    if ( (inRange != -1) & (!drawLine) & (!drawPoint)) {
      mFrame.yjc[inRange].yjpaintArc(g2D, Color.yellow);
    }

    /** 画线  */
    g2D.setColor(Color.white);
    if (drawLine) {
      if (lineBg) {
        g2D.drawLine(lineX1, lineY1, lineX2, lineY2);
      }
    }

  }

  public void mouseDragged(MouseEvent evt) {
  }

  public void mouseMoved(MouseEvent evt) {
    /**
     * 实时记录鼠标的值,存入mouseX mouseY
     */
    mouseX = evt.getX();
    mouseY = evt.getY();
    /**
     * 如果lineBg为True,表示正在画线:改变画线用的坐标
     */
    if (lineBg) {
      lineX2 = mouseX;
      lineY2 = mouseY;
    }
    /** 改变主框架中状态栏的内容 */
    mFrame.statusBar.setText("X=" + String.valueOf(mouseX) + "Y=" +
                             String.valueOf(mouseY) + " inRange:" + inRange);
    if (selectFlag == true) {
      mFrame.yjc[inRange].yjMovie(mouseX - bwX, mouseY - bwY);
    }

    repaint();
  }

  public void mouseClicked(MouseEvent evt) {
    /**
     * whichKey = 1  :鼠标左键
     * whickKey = 2  :鼠标右键
     */
    int whichKey = evt.getButton();

    /** 如果在元件范围内点击鼠标,则选定该元件*/
    if ( (inRange != -1) & (!drawLine) & (!drawPoint)) {
      if (selectFlag) {
        selectFlag = false;
        inRange = -1;
        repaint();
      }
      else {
        selectFlag = true;
        /** 锁定点击鼠标时,鼠标坐标与当前元件的坐标差值   */
        bwX = mouseX - mFrame.yjc[inRange].minX;
        bwY = mouseY - mFrame.yjc[inRange].minY;
      }
    }

    /** 画线 */
    if (drawLine) {
      if (whichKey == 1) {
        if (lineBg) {
          drawLineNo++;
          yjLine tmpLine[] = new yjLine[1];
          //forDrawLine[drawLineNo - 1] = new yjLine();
          //forDrawLine[drawLineNo - 1].setValue(lineX1, lineY1, lineX2, lineY2);
          tmpLine[0] = new yjLine();
          tmpLine[0].setValue(lineX1, lineY1, lineX2, lineY2);
          lineX1 = mouseX;
          lineY1 = mouseY;
          mFrame.yjc[mFrame.yjcNo] = new yuanjian(null, tmpLine, null, null, null);
          mFrame.yjcNo++;
          //mFrame.yjc[mFrame.yjcNo-1]=new yuanjian(null,);
        }
        else {
          lineX1 = mouseX;
          lineY1 = mouseY;
          lineBg = true;
        }
      }
      else if (whichKey == 3) {
        drawLine = false;
        lineBg = false;
        /**for (int i = 0; i < drawLineNo; i++) {
          forDrawLine[drawLineNo - 1] = null;
                 }*/
        drawLineNo = 0;
      }
    }
    //System.out.println("mouseClick:"+whichKey+"  drawLine:"+drawLine);

    /**
     * 画点
     */
    if (drawPoint) {
      if (whichKey == 3) {
        drawPoint = false;
      }
      else {
        yjCircle tmpCircle[] = new yjCircle[1];
        tmpCircle[0] = new yjCircle();
        tmpCircle[0].setValue(mouseX - 4, mouseY - 4, 4, 1);
        //tmpCircle[0].fillFlag =1;
        mFrame.yjc[mFrame.yjcNo] = new yuanjian(null, null, tmpCircle, null, null);
        mFrame.yjcNo++;
      }
    }

    /**
     * 画字符
     */
    if (drawChar) {
      if (whichKey == 3) {
        drawChar = false;
      }
      else {
        yjStr tmpStr[] = new yjStr[1];
        tmpStr[0] = new yjStr();
        tmpStr[0].setValue(mouseX - 4, mouseY - 4, mFrame.resChar);
        //tmpCircle[0].fillFlag =1;
        mFrame.yjc[mFrame.yjcNo] = new yuanjian(null, null, null, tmpStr, null);
        mFrame.yjcNo++;
      }
      drawChar = false;
    }


  }

  public void mouseEntered(MouseEvent evt) {

  }

  public void mouseExited(MouseEvent evt) {

  }

  public void mousePressed(MouseEvent evt) {

  }

  public void mouseReleased(MouseEvent evt) {

  }

  public void run() {
    int i, mouseX, mouseY;
    while (true) {

      /**
       * 如果selectFlag为真,表示已经选中元件,则不作操作
       * 如果selectFlag为假,表示没有选中元件,开始循环判断鼠标目前的范围
       * 从而设定inRange
       */
      mouseX = this.mouseX;
      mouseY = this.mouseY;
      if (!selectFlag) {
        for (i = 0; i < 1000; i++) {
          if (mFrame.yjc[i] != null) {
            if (mouseX > mFrame.yjc[i].minX && mouseX < mFrame.yjc[i].maxX &&
                mouseY > mFrame.yjc[i].minY && mouseY < mFrame.yjc[i].maxY) {
              inRange = i;
              break; //如果没有这句,将只对循环的最后一个元件作判断
            }
            else {
              inRange = -1;
            }
          }
        }
      }

      try {
        Thread.sleep(500);
      }
      catch (InterruptedException e) {}
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -