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

📄 sketchview.java

📁 Beginning Java 2, SDK 1.4 Edition Exercise Code samples for this book
💻 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.util.Iterator;

import java.awt.Graphics;
import java.awt.Graphics2D;                          
import java.awt.Point;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.Font;
import java.awt.Component;

import java.awt.event.ActionEvent;    
import java.awt.event.ActionListener;    
import java.awt.event.MouseEvent;    

import java.awt.geom.Line2D;    
import java.awt.geom.Point2D;    

import java.awt.font.TextLayout;  

import javax.swing.event.MouseInputAdapter;    

class SketchView extends JComponent implements Observer, Constants, ActionListener {
  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 to the Element popup
    moveItem = elementPopup.add("Move");
    deleteItem = elementPopup.add("Delete");
    rotateItem = elementPopup.add("Rotate");
    sendToBackItem = elementPopup.add("Send-to-back");
    infoItem = elementPopup.add("Info");
    editItem = new JMenuItem("Edit");
    scaleItem = elementPopup.add("Scale");

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

  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();
      }
    } else if(source == infoItem) {
        JOptionPane.showMessageDialog(null, highlightElement.getInfo());
    } else if(source == editItem) {
        String newText = JOptionPane.showInputDialog("Edit the current element text:",
                                        ((Element.Text)highlightElement).getText());

        if(newText != null && newText.length()!=0)  {          // Do we have new text?        
          // If so, update the element
          // Note that if we change the text we need to obtain the
          // bounds for the new text, otherwise it might not display properly.
          ((Element.Text)highlightElement).setText(newText,       // The new text plus
          new java.awt.font.TextLayout(newText,
                      ((Element.Text)highlightElement).getFont(), // its bounding rectangle
          ((Graphics2D)getGraphics()).getFontRenderContext()).getBounds().getBounds());
        }
    } else if(source == scaleItem) {
        mode = SCALE;
        selectedElement = highlightElement;
    }
  }

  public void paint(Graphics g) {
    Graphics2D g2D = (Graphics2D)g;                // Get a 2D device context 
    Iterator elements = theApp.getModel().getIterator();
    while(elements.hasNext())                       // Go through the list
      ((Element)elements.next()).draw(g2D);         // Get the next element to draw 
                                                    // itself
  }

  public void update(Observable o, Object rectangle) {
    if(rectangle == null)
      repaint();
    else
      repaint((Rectangle)rectangle);
  }

  class MouseHandler extends MouseInputAdapter {
    public void mousePressed(MouseEvent e) {
      start = e.getPoint();                   // Save the cursor position in start
      if((button1Down = (e.getButton()==MouseEvent.BUTTON1))) { 
        g2D = (Graphics2D)getGraphics();                 // Get graphics context
        g2D.setXORMode(getBackground());                 // Set XOR mode
        g2D.setPaint(theApp.getWindow().getElementColor());     // Set color
      }
      if(mode == SCALE && selectedElement != null) {
        // Get the current element position
        elementPosition = selectedElement.getPosition();
      }
    }

    public void mouseDragged(MouseEvent e) {
      last = e.getPoint();                               // Save cursor position

      if(button1Down && (theApp.getWindow().getElementType() != TEXT) 
                     && (mode == NORMAL)) {
        if(tempElement == null) {                       // Is there an element?
          tempElement = createElement(start, last);     // No, so create one
        } else {
          tempElement.draw(g2D);                          // Yes - draw to erase it
          tempElement.modify(start, last);              // Now modify it
        }
        tempElement.draw(g2D);                          // and draw it      }

      } else if(button1Down && mode == MOVE && selectedElement != null) {
        selectedElement.draw(g2D);                    // Draw to erase the element
        selectedElement.move(last.x-start.x, last.y-start.y);  // Move it
        selectedElement.draw(g2D);                     // Draw in its new position
        start = last;                                  // Make start current point

      } else if(button1Down && mode == ROTATE && selectedElement != null) {
        selectedElement.draw(g2D);                   // Draw to erase the element
        selectedElement.rotate(getAngle(selectedElement.getPosition(),
                                                                    start, last));
        selectedElement.draw(g2D);                  // Draw in its new position
        start = last;                               // Make start current point
      } else if(mode == SCALE && selectedElement != null) {
        selectedElement.draw(g2D);                     // Draw to erase the element

        double angle = Math.atan(
              (start.y-elementPosition.getY())/(start.x-elementPosition.getX()));
        double distance = start.distance(elementPosition);
        double xDistance = distance*Math.cos(angle);
        double yDistance = distance*Math.sin(angle);

        angle = Math.atan(
          (last.y-elementPosition.getY())/(last.x-elementPosition.getX()));
        distance = last.distance(elementPosition);
        selectedElement.scale(distance*Math.cos(angle)/xDistance,
                                                 distance*Math.sin(angle)/yDistance);
         
        selectedElement.draw(g2D);                     // Draw in its new position
        start = last;
      }
    }

    public void mouseReleased(MouseEvent e) {
      if(e.isPopupTrigger()) {

⌨️ 快捷键说明

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