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

📄 drawboard.java

📁 基于jxta的局域网P2P文件共享,可以实现局域网中的文件p2p共享,实现文件快速传输及交流
💻 JAVA
字号:
package connex.plugins.whiteboard;

import java.awt.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.awt.event.MouseEvent;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionAdapter;

import java.io.IOException;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.*;

import java.util.Enumeration;

import java.util.ArrayList;
import java.util.Vector;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class Drawboard
    extends JPanel implements LocalListener {

  private BufferedImage image;

  private Point start, end, drag;

  private boolean draging = false;

  /**
   * @directed
   * @supplierCardinality 1
   * @clientCardinality 1
   */
  private ShapesContol shapes;

  /**
   * @directed*/
  private ShapeObj gObj;

  protected int drawMode;
  private Color color = Color.black;
  private Color fillcolor = Color.gray;
  private boolean showGrid = true;
  /**
   * Listener that will be notified when a shape is selected.
   */
  private ShapeSelectionListener selectionlistener;

  public Drawboard() {
    try {
      jbInit();
    }
    catch (Exception exception) {
      exception.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    this.setPreferredSize(new Dimension(800, 600));
    shapes = ShapesContol.getInstance();
    this.addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent e) {
        this_mousePressed(e);
      }
    });
    this.addMouseMotionListener(new MouseMotionAdapter() {
      public void mouseDragged(MouseEvent e) {
        this_mouseDragged(e);
      }
    });
    this.addMouseListener(new MouseAdapter() {
      public void mouseReleased(MouseEvent e) {
        this_mouseReleased(e);
      }
    });
    this.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        this_mouseClicked(e);
      }
    });

  }

  public void setSelectionListener(ShapeSelectionListener selectionlistener) {
    this.selectionlistener = selectionlistener;

  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (image == null) {
      initImage();
      shapes.setLocalListener(this);
    }

    g.drawImage(image, 0, 0, this);

  }

  private void initImage() {
    int w = getWidth();
    int h = getHeight();

    image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

    Graphics2D g2 = image.createGraphics();
    g2.setPaint(Color.white);
    g2.fillRect(0, 0, w, h);

    g2.dispose();

    drawGrid(image.createGraphics(), getWidth(), getHeight());

  }

  public BufferedImage getImage() {
    BufferedImage saveImage = new BufferedImage(image.getWidth(), image.getHeight(),
                                                BufferedImage.TYPE_INT_ARGB);

    this.clearSelection(saveImage.createGraphics());
    this.drawGraphics(image.createGraphics());
    return saveImage;
  }

  protected void clear() {
    Graphics g = image.getGraphics();

    g.setColor(Color.white);
    g.fillRect(0, 0, image.getWidth(), image.getHeight());
    if (showGrid) {
      drawGrid(image.createGraphics(), getWidth(), getHeight());
    }
    g.dispose();
    repaint();
  }

  public Graphics2D getGraphic() {
    return image.createGraphics();
  }

  public void this_mousePressed(MouseEvent e) {

    this.start = e.getPoint();
    clearSelection(image.createGraphics());

    switch (drawMode) {

      case MainUI.SELECT:

        gObj = this.getSelectedObject();
        if (gObj != null) {

          //gObj.setGraphic(image.createGraphics());
          //gObj.setDrawColor(color);
          this.selectionlistener.selectedShape(gObj);
          shapes.ShapeSelected(gObj.getID());
        }
        break;

      case MainUI.FILLEDSHAPE:
        try {
          gObj = gObj.getClass().newInstance();
          gObj.setGraphic(image.createGraphics());
          gObj.setDrawColor(color);
          gObj.setFillColor(fillcolor);

        }
        catch (IllegalAccessException ex) {
        }
        catch (InstantiationException ex) {
        }

        break
            ;

      case MainUI.DRAWSHAPE:

        try {
          gObj = gObj.getClass().newInstance();
          gObj.setGraphic(image.createGraphics());
          gObj.setDrawColor(color);

        }
        catch (IllegalAccessException ex) {
        }
        catch (InstantiationException ex) {
        }

        break
            ;

      case MainUI.FILLSHAPE:
        gObj = this.getSelectedObject();
        if (gObj != null) {

          //gObj.setGraphic(image.createGraphics());
        }
        break;

    }

  }

  public void this_mouseDragged(MouseEvent e) {
    drag = e.getPoint();
    draging = true;
    if (drawMode == MainUI.DRAWSHAPE || drawMode == MainUI.FILLEDSHAPE) {

      gObj.draw(start, e.getPoint());
      repaint();
    }
    if (drawMode == MainUI.SELECT && gObj != null) {

      gObj.move(e.getPoint(), start);
      repaint();
      start.setLocation(drag);

    }

  }

  public void this_mouseReleased(MouseEvent e) {
    end = e.getPoint();

    if (gObj != null) {
      if (drawMode == MainUI.DRAWSHAPE || drawMode == MainUI.FILLEDSHAPE) {

        gObj.clearSelection();
        gObj.draw(start, e.getPoint());
        gObj.setID(gObj.hashCode() + "");
        shapes.put(gObj.getID(), gObj);

      }
      if (drawMode == MainUI.SELECT) {

        gObj.draw();

        shapes.shapeMoved(gObj.getID(), gObj);

      }
      if (drawMode == MainUI.FILLSHAPE) {
        gObj.fill(fillcolor);
        shapes.shapeFilled(gObj.getID(), gObj);

      }
      this.drawGraphics(image.createGraphics());
    }
    draging = false;
  }

  public void this_mouseClicked(MouseEvent e) {

    if (SwingUtilities.isLeftMouseButton(e)) {
      if (drawMode == MainUI.POLYGON || drawMode == MainUI.FillEdPOLYGON) {

        gObj.setGraphic(image.createGraphics());
        gObj.setDrawColor(color);
        if (drawMode == MainUI.FillEdPOLYGON) {
          gObj.setFillColor(fillcolor);
        }
        gObj.draw(start, null);
        shapes.putIfAbsent(gObj.getID(), gObj);
        this.drawGraphics(image.createGraphics());
      }
    }
    if (SwingUtilities.isRightMouseButton(e)) {
      if (drawMode == MainUI.POLYGON || drawMode == MainUI.FillEdPOLYGON) {

        shapes.put(gObj.getID(), gObj);
        this.drawGraphics(image.createGraphics());
        try {
          gObj = gObj.getClass().newInstance();
        }
        catch (IllegalAccessException ex) {
        }
        catch (InstantiationException ex) {
        }
      }
    }
  }

   protected void setGraphicObj(ShapeObj shape) {

    this.gObj = shape;
    gObj.setGraphic(image.createGraphics());

  }

  protected void setDrawMode(int drawMode) {
    this.drawMode = drawMode;

  }

   protected void setDrawColor(Color color) {

    this.color = color;
    /* gObj = this.getSelectedObject();
        if (gObj != null) {
          gObj.setDrawColor(color);
          gObj.draw();
          repaint();
           this.selectionlistener.selectedShape(gObj);
        }*/

  }

   protected void setFillColor(Color color) {
    System.out.println("color changed");
    this.fillcolor = color;
    /* gObj = this.getSelectedObject();
        if (gObj != null) {
          gObj.setFillColor(color);
          gObj.draw();
          repaint();
           this.selectionlistener.selectedShape(gObj);
        }*/

  }

  private ShapeObj getSelectedObject() {
    ShapeObj selectedObj = null;
    Vector v = shapes.getShapes();

    int s = v.size();
    for (int i = s - 1; i >= 0; i--) {
      selectedObj = (ShapeObj) v.get(i);
      if (selectedObj.SelectShape(start)) {


        if(selectedObj.isLocked()){
          JOptionPane.showMessageDialog(this,"Graphic locked from "+selectedObj.getLocker(),"",JOptionPane.INFORMATION_MESSAGE);
          return null;
        }


        selectedObj.draw();

        repaint();
        v.clear();

        return selectedObj;
      }
    }

    return null;
  }

  private void clearSelection(Graphics2D g2) {
    ShapeObj sh;

    Enumeration v = (Enumeration) shapes.getShapes().elements();
    //for (int i = 0; i < v.size(); i++) {
    while (v.hasMoreElements()) {
      sh = (ShapeObj) v.nextElement();
      sh.setGraphic(g2);
      sh.clearSelection();
      sh.draw();

    }
    drawGraphics(g2);

  }

  private void drawGraphics(Graphics2D g2) {
    this.clear();
    ShapeObj sh;

    Vector v = shapes.getShapes();

   int s = v.size();

    for (int i = 0; i < s; i++) {
      sh = (ShapeObj) v.get(i);
      sh.setGraphic(g2);

      sh.draw();

    }
    repaint();

  }

  protected void deleteShape() {
    ShapeObj shape = getSelectedObject();
    if (shape != null) {

      shapes.remove(shape.getID());
      System.out.println("remove " + shape.getID());
      this.drawGraphics(image.createGraphics());

    }

  }

  protected void clearGraphics() {
    // v.removeAllElements();
    shapes.clear();
    this.drawGraphics(image.createGraphics());

  }
  protected void close(){
    shapes.close();
    this.drawGraphics(image.createGraphics());
    image.flush();
    image =null;

  }

  protected void saveGraphics() {
    try {
      System.out.println(objectToBytes(shapes).length);
      FileOutputStream out = new FileOutputStream("graphic");
      out.write(objectToBytes(shapes));
      out.flush();
      out.close();
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }

  }

  protected void showGrid() {
    showGrid = true;
    this.drawGraphics(image.createGraphics());
  }

  protected void hideGrid() {
    showGrid = false;
    this.drawGraphics(image.createGraphics());
  }

  protected void loadGraphics() {
    if (!shapes.isEmpty()) {
      shapes.clear();
    }

    byte[] buf = new byte[64000];
    try {
      FileInputStream in = new FileInputStream("graphic");

      in.read(buf);

      shapes = (ShapesContol) bytesToObject(buf);
      System.out.println(shapes.size());
    }
    catch (FileNotFoundException ex1) {
    }
    catch (ClassNotFoundException ex3) {
    }
    catch (IOException ex3) {
    }

    this.drawGraphics(image.createGraphics());

  }

   protected synchronized byte[] objectToBytes(Object object) throws
      IOException {
    java.io.ObjectOutputStream out;
    java.io.ByteArrayOutputStream bs;

    bs = new java.io.ByteArrayOutputStream();
    out = new java.io.ObjectOutputStream(bs);
    out.writeObject(object);
    out.close();

    return bs.toByteArray();

  }

  protected synchronized Object bytesToObject(byte[] bytes) throws
      IOException,
      ClassNotFoundException {
    Object res;
    java.io.ObjectInputStream in;
    java.io.ByteArrayInputStream bs;

    bs = new java.io.ByteArrayInputStream(bytes);
    in = new java.io.ObjectInputStream(bs);
    res = in.readObject();
    in.close();
    bs.close();
    return res;
  }

  public int getDrawMode() {
    return drawMode;
  }

  private void drawGrid(Graphics2D g2, int w, int h) {
    int grid = 8;
    if (grid > 0) {
      g2.setColor(new Color(224, 224, 224));

      for (int i = grid; i < h; i += grid) {
        g2.drawLine(0, i, w, i);
      }
      for (int i = grid; i < w; i += grid) {
        g2.drawLine(i, 0, i, h);
      }

      g2.setColor(new Color(208, 208, 208));

      for (int i = grid * 10; i < h; i += grid * 10) {
        g2.drawLine(0, i, w, i);
      }
      for (int i = grid * 10; i < w; i += grid * 10) {
        g2.drawLine(i, 0, i, h);
      }
    }

  }

  public void update() {
    if (!draging) {
      this.drawGraphics(image.createGraphics());
    }
  }

}

⌨️ 快捷键说明

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