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

📄 sketchview.java

📁 用JAVA开发的一个小型画图软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JMenuItem;
import java.util.Observer;                  
import java.util.Observable;                  
import java.awt.Graphics;                                
import java.awt.Graphics2D;
import java.awt.Point; 
import java.awt.Rectangle;                               
import java.awt.Component;                               
import java.awt.Font;
                               
import java.awt.print.Printable;
import java.awt.print.PageFormat;
import java.awt.print.Pageable;
import java.awt.print.Paper;
import java.awt.print.PrinterException;

import java.awt.font.TextLayout;  
import java.awt.geom.Line2D;    
import java.awt.event.MouseEvent;    
import java.awt.event.ActionEvent;    
import java.awt.event.ActionListener;    
import javax.swing.event.MouseInputAdapter;    
import static Constants.SketcherConstants.*;

class SketchView extends JComponent implements Observer, ActionListener, Printable, Pageable {
  public SketchView(Sketcher theApp) {
    this.theApp = theApp;
    MouseHandler handler = new MouseHandler();        // create the mouse listener
    addMouseListener(handler);                        // Listen for button events
    addMouseMotionListener(handler);                  // Listen for motion events

    // Add the pop-up menu items
    moveItem = elementPopup.add(new JMenuItem("Move"));
    deleteItem = elementPopup.add(new JMenuItem("Delete"));
    rotateItem = elementPopup.add(new JMenuItem("Rotate"));
    sendToBackItem = elementPopup.add(new JMenuItem("Send-to-back"));

    // Add the menu item listeners
    moveItem.addActionListener(this);
    deleteItem.addActionListener(this);
    rotateItem.addActionListener(this);
    sendToBackItem.addActionListener(this);
  }

  // Method to return page count - always two pages
  public int getNumberOfPages() {
    return 2;
  }

  // Method to return the Printable object that will render the page
  public Printable getPrintable(int pageIndex) {
    if(pageIndex == 0) {                         // For the first page
      return new SketchCoverPage(theApp);        // return the cover page
    } else {
      return this;                               
    }
  }

  // Method to return a PageFormat object to suit the page
  public PageFormat getPageFormat(int pageIndex) {
    if(pageIndex==0) {               // If it's the cover page...
                                     // ...make the margins twice the size
      // Create a duplicate of the current page format
      PageFormat pageFormat = (PageFormat)(theApp.getWindow().getPageFormat().clone());
      Paper paper = pageFormat.getPaper();

      // Get top and left margins - x & y coordinates of top-left corner
      // of imageable area are the left & top margins  
      double leftMargin = paper.getImageableX();
      double topMargin = paper.getImageableY();

      // Get right and bottom margins
      double rightMargin = paper.getWidth()-paper.getImageableWidth()-leftMargin;
      double bottomMargin = paper.getHeight()-paper.getImageableHeight()-topMargin;

      // Double the margin sizes
      leftMargin *= 2.0;
      rightMargin *= 2.0;
      topMargin *= 2.0;
      bottomMargin *= 2.0;

      // Set new printable area for the paper
      paper.setImageableArea(leftMargin, topMargin,
                           paper.getWidth()-leftMargin-rightMargin,
                           paper.getHeight()-topMargin-bottomMargin);

      pageFormat.setPaper(paper);                // Restore the paper
      pageFormat.setOrientation(PageFormat.LANDSCAPE);      
      return pageFormat;                             // Return the page format
    }
    // For pages after the first, use the object from the app window
    return theApp.getWindow().getPageFormat();
  }

  // Method to print the sketch
  public int print(Graphics g,              // Graphics context for printing
                   PageFormat pageFormat,   // The page format
                   int pageIndex)           // Index number of current page
             throws PrinterException {
    Graphics2D g2D = (Graphics2D) g;
    // Get sketch bounds
    Rectangle rect = theApp.getModel().getModelExtent();

    // Calculate the scale to fit sketch to page
    double scaleX = pageFormat.getImageableWidth()/rect.width;
    double scaleY = pageFormat.getImageableHeight()/rect.height;

    // Get minimum scale factor
    double scale = Math.min(scaleX, scaleY);  

    // Move origin to page printing area corner
    g2D.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

    g2D.scale(scale, scale);              // Apply scale factor

    g2D.translate(-rect.x, -rect.y);      // Move origin to rect top left

    paint(g2D);                           // Draw the sketch
    return PAGE_EXISTS;
  }

  // Method called by Observable object when it changes
  public void update(Observable o, Object rectangle) {
    if(rectangle != null & rectangle instanceof Rectangle) {
      repaint((Rectangle)rectangle);
    } else {
      repaint();
    }
  }

  // Handle context menu events
  public void actionPerformed(ActionEvent e ) {
    Object source = e.getSource();
    if(source == moveItem) {
      mode = MOVE;
      selectedElement = highlightElement;
    } else if(source == deleteItem) {
      if(highlightElement != null) {                    // If there's an element
        theApp.getModel().remove(highlightElement);     // then remove it
        highlightElement = null;                        // Remove the reference
      }

    } else if(source == rotateItem) {
      mode = ROTATE;
      selectedElement = highlightElement;
    } else if(source == sendToBackItem) {
      if(highlightElement != null) {
        theApp.getModel().remove(highlightElement);
        theApp.getModel().add(highlightElement);
        highlightElement.setHighlighted(false);
        highlightElement = null;
        repaint();
      }
    }
  }

  public void paint(Graphics g) {
    Graphics2D g2D = (Graphics2D)g;                     // Get a 2D device context
    for(Element element : theApp.getModel()) {          // Go through the list
      element.draw(g2D);                                // Element draws itself
    }
  }

  private Sketcher theApp;                              // The application object
  private Element highlightElement;                     // Highlighted element
  private JPopupMenu elementPopup = new JPopupMenu("Element");
  private JMenuItem moveItem, deleteItem,rotateItem, sendToBackItem;
  private int mode = NORMAL;
  private Element selectedElement;
  
  class MouseHandler extends MouseInputAdapter {
      // Process pop-up trigger event
      public void processPopupTrigger(MouseEvent e) {
        start = e.getPoint();                           // Save the cursor position in start
        if(highlightElement == null) {
          theApp.getWindow().getPopup().show((Component)e.getSource(),start.x, start.y);
        } else {
          elementPopup.show((Component)e.getSource(), start.x, start.y);

⌨️ 快捷键说明

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