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

📄 paintpanel.java

📁 java实现的画板程序
💻 JAVA
字号:
package drawpane;import javax.swing.JPanel;import java.awt.*;import java.util.*;import java.awt.image.*;import java.io.*;import java.awt.datatransfer.*;//PaintPanel类,继承自JPanel类public class PaintPanel extends javax.swing.JTextArea{  public int maxX = 600, maxY = 500;  private ArrayList shape = new ArrayList(), undo = new ArrayList();  private Color bgCol = Color.white;  private BufferedImage offScreenImg = null;  private MemoryImageSource mis = null;  private int[] pixel;  private Graphics2D offScreenG = null;  private Clipboard cb;  //构造函数  public PaintPanel() {    super();    this.shape = shape;    pixel = new int[this.getWidth() * this.getHeight()];    //mis=new MemoryImageSource(this.getWidth(),this.getHeight(),pixel,0,this.getWidth());    offScreenImg = new BufferedImage(this.maxX, this.maxY,                                     BufferedImage.TYPE_INT_RGB);    offScreenG = offScreenImg.createGraphics();    offScreenG.setColor(Color.white);    offScreenG.fillRect(0, 0, this.maxX, this.maxY);    String str = null;    this.setFont(new Font("", 0, 1));    this.setColumns(this.maxX);    this.setRows(this.maxY / 2);    cb=Toolkit.getDefaultToolkit().getSystemClipboard();  }  /**   * 重写paintComponent方法   * 先调用其父类的paintComponent方法      */  public void paintComponent(Graphics g) {    super.paintComponent(g);    g.drawImage(offScreenImg, 0, 0, this.maxX, this.maxY, this);  }  //绘制一个元素的方法  public void paintElement(int index) {    if (index < shape.size() && index >= 0) {      ( (CusShape) shape.get(index)).draw(offScreenG);    }    repaint();  }  //添加图形对象  public void addShape(Object element) {    shape.add(element);    undo.clear();  }  //删除图形对象  public void deleteShape(int index) {    shape.remove(index);    rePaintArea();    repaint();  }  public void paintLastElement() {    paintElement(shape.size() - 1);  }  public void rePaintArea() {    offScreenG.setColor(bgCol);    offScreenG.fillRect(0, 0, this.maxX, this.maxY);    for (int i = 0; i < shape.size(); i++) {      paintElement(i);    }  }  //得到与x,y,w,h构成的矩形相交的图形对象  public int getIntersectsShape(double x, double y, double w, double h) {    for (int i = shape.size() - 1; i >= 0; i--) {      if ( ( (CusShape) shape.get(i)).intersects(x, y, w, h)) {        return i;      }    }    return -1;  }  //填充图形对象  public void fillShape(int index, Color col) {    ( (CusShape) shape.get(index)).setIsFill(true);    ( (CusShape) shape.get(index)).setColor(col);    rePaintArea();    repaint();  }  //得到图形对象的边界  public Rectangle getElementBounds(int index) {    if (index < shape.size() && index >= 0) {      return ( (CusShape) shape.get(index)).getBounds();    }    else return null;  }  //得到图形对象  public CusShape getElement(int index) {    return (CusShape) shape.get(index);  }  public Graphics2D getOffScreenGraphics() {    return offScreenG;  }  // 设置画布大小  public void setMaxSize(int x, int y) {    if (x * y <= 9000000) {      this.maxX = x;      this.maxY = y;      this.setFont(new Font("", 0, 1));      this.setColumns(this.maxX);      this.setRows(this.maxY / 2);      offScreenImg = new BufferedImage(this.maxX, this.maxY,                                       BufferedImage.TYPE_INT_RGB);      offScreenG = offScreenImg.createGraphics();      rePaintArea();      repaint();    }  }  //绘制图形对象的边界  public void paintElementBounds(int index) {    Rectangle rec = getElementBounds(index);    rec.setRect(rec.getX() - 1, rec.getY() - 1, rec.getWidth() + 2,                rec.getHeight() + 2);    Stroke stk = offScreenG.getStroke();    float[] dash = new float[1];    dash[0] = 3;    offScreenG.setStroke(new BasicStroke(1, 0, 0, 3, dash, 5));    offScreenG.setColor(Color.gray);    offScreenG.draw(rec);    offScreenG.setStroke(stk);    repaint();  }  // 清除画布  public void clear() {    shape.clear();    rePaintArea();    repaint();  }        /**   * 得到图形对象   * @param index int   * @return CusShape   */  public CusShape getShape(int index) {    if (index < shape.size()) {      return (CusShape) shape.get(index);    }    else {      return null;    }  }  /**   * 设置画布的背景色   * @param bgCol Color   */  public void setBgColor(Color bgCol) {    this.bgCol = bgCol;    rePaintArea();    repaint();  }  /**   * 导出为jpeg格式的图片   * @param aFile File   * @return boolean   */  public boolean outPut(File aFile) {    return ImageToJpeg.toJpeg(aFile, offScreenImg);  }  /**   * 撤消   */  public void unDo() {    if (canUnDo()) {      undo.add(shape.remove(shape.size() - 1));      rePaintArea();      repaint();    }  }  /**   * 重做   */  public void reDo() {    if (canReDo()) {      shape.add(undo.remove(undo.size() - 1));      rePaintArea();      repaint();    } } /**  * 判断是否可以撤消  * @return boolean  */ public boolean canUnDo() {    return !shape.isEmpty();  }  /**   * 判断是否可以重做   * @return boolean   */  public boolean canReDo(){    return !undo.isEmpty();  }  /**   * 保存   * @param aFile File   */  public void saveFile(File aFile)  {      try {        FileOutputStream fos = new FileOutputStream(aFile);        ObjectOutputStream oos = new ObjectOutputStream(fos);        for (int i = 0; i < shape.size(); i++)        {          CusShape aCusShape=(CusShape)shape.get(i);          if(aCusShape.isImage())          {            ((CusImage)aCusShape).setSaveImage();          }          oos.writeObject(shape.get(i));        }        oos.flush();        oos.close();      }      catch (Exception ex)      {        ex.printStackTrace();      }  }  /**   * 打开   * @param aFile File   */  public void openFile(File aFile)  {     try {       FileInputStream fis = new FileInputStream(aFile);       ObjectInputStream ois = new ObjectInputStream(fis);       shape.clear();       while (true) {         CusShape aCusShape=(CusShape)ois.readObject();         if(aCusShape.isImage())         {           ((CusImage)aCusShape).setOpenImage();         }         shape.add(aCusShape);       }     }     catch (EOFException ex) {       rePaintArea();       repaint();       undo.clear();     }     catch (Exception ex) {       ex.printStackTrace();     }  }  /**   * 判断画布是否为空   * @return boolean   */  public boolean isEmpty()  {    return shape.isEmpty();  }  /**   * 得到剪切的图形   * @param x int   * @param y int   * @param w int   * @param h int   * @return BufferedImage   */  public BufferedImage getScaleImage(int x,int y,int w,int h)  {    return offScreenImg.getSubimage(x,y,w,h);  }  /**   * 得到画布上的图形个数   * @return int   */  public int getShapeNum()  {    return shape.size();  }  /**   * 合并图形对象为图片对象   */  public void shapesToImage()  {    if(shape.size()>0)    {      rePaintArea();      CusImage aCusShape=new CusImage(getScaleImage(0,0,maxX,maxY),0,0);      shape.clear();      addShape(aCusShape);      rePaintArea();      repaint();    }  }  /**   * 复制图形对象到系统剪切板   * @param index int   */  public void copyElement(int index)  {    if(index<shape.size())    {      cb.setContents((CusShape)shape.get(index),null);    }  }  /**   * 从系统剪切板中粘贴图形对象   * @param x int   * @param y int   */  public void parseElement(int x,int y)  {    try    {      if(cb.getContents(null).isDataFlavorSupported(DataFlavor.imageFlavor))      {        Image aImage=(Image)cb.getContents(null).getTransferData(DataFlavor.imageFlavor);        CusImage aCusShape=new CusImage(aImage,0,0);        aCusShape.setLocation(x,y);        shape.add(aCusShape);      }      else      {        CusShape aCusShape=(CusShape)cb.getContents(null).getTransferData(CusShape.CUSSHAPEFLAVOR);        aCusShape.setLocation(x,y);        shape.add(aCusShape);      }      rePaintArea();      repaint();    }    catch(Exception ex)    {      ex.printStackTrace();    }  }  /**   * 从系统剪切板中粘贴图形对象   */  public void parseElement()  {    try    {      if(cb.getContents(null).isDataFlavorSupported(DataFlavor.imageFlavor))      {        Image aImage=(Image)cb.getContents(null).getTransferData(DataFlavor.imageFlavor);        CusImage aCusShape=new CusImage(aImage,0,0);        shape.add(aCusShape);      }      else      {        shape.add(cb.getContents(null).getTransferData(CusShape.CUSSHAPEFLAVOR));      }      rePaintArea();      repaint();    }    catch(Exception ex)    {      ex.printStackTrace();    }  }  /**   * 剪切图形对象到系统剪切板   * @param index int   */  public void cutElement(int index)  {    if(index<shape.size())    {      cb.setContents((CusShape)shape.get(index),null);      deleteShape(index);    }  }  /**   * 判断系统剪切板中的内容是否可以粘贴   * @return boolean   */  public boolean canParse()  {    if (cb.getContents(null).isDataFlavorSupported(DataFlavor.imageFlavor) ||        cb.getContents(null).isDataFlavorSupported(CusShape.CUSSHAPEFLAVOR)) {      return true;    }    else {      return false;    }  }}

⌨️ 快捷键说明

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