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

📄 picture.java

📁 java 图形处理
💻 JAVA
字号:

/** A Picture object represents a definition of a picture
    that consists of an arrangement of simple coloured shapes
    in 2D space.  There are convenience methods for generating
    a circle, a rectangle and a triangle,
    but any Java shape can be used.  
    More complex pictures are be produced by applying
    affine transformations to pictures,
    and by laying one picture on top of another.
    Picture coordinates, with y-axis upwards, are used
    to define positions in a picture.

    (2 Feb 00) First version.
    (5 Feb 00) This version handles Paint correctly.
    (5 Feb 01) Method 'makeBuffer' added.
*/

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

abstract class Picture {

   /* Convenience methods for constructing pictures. */

   /** Return a Picture consisting of a square
       with sides of length 1 filled with Paint p.
       The bottom-left corner of the rectangle is at (0,0).
   */
   public static Picture box(Paint p) {
      Shape s = new Rectangle2D.Double(0,0,1,1);
      return new BasicShape(s,p);
   }

   /** Return a Picture consisting of a circle
       with radius 1 filled with Paint p.
       The centre of the circle is at (0,0).
   */
   public static Picture circle(Paint p) {
      Shape s = new Ellipse2D.Double(-1, -1, 2, 2);
      return new BasicShape(s,p);
   }

   /** Return a Picture consisting of a triangle
       with vertices at (x0,y0), (x1,y1), (x2,y2),
       and filled with Paint p.
   */
   public static Picture triangle(double x0, double y0,
                                  double x1, double y1,
                                  double x2, double y2, Paint p) {
      GeneralPath path = new GeneralPath();
      path.moveTo((float)x0, (float)y0);
      path.lineTo((float)x1, (float)y1);
      path.lineTo((float)x2, (float)y2);
      path.closePath();
      return new BasicShape(path,p);
   }

   /** Return the picture produced by taking this picture
       and moving it distance tx in the x-direction
       and ty in the y-direction.
   */
   public Picture at(double tx, double ty) {
      return new Transform
         (this, AffineTransform.getTranslateInstance(tx,ty));
   }

   /** Return the picture produced by taking this picture
       and rotating it by deg degrees anti-clockwise.
   */
   public Picture turn(double deg) {
      double rad = deg * Math.PI / 180;
      return new Transform
         (this, AffineTransform.getRotateInstance(rad));
   }

   /** Return the Picture produced by taking this picture
       and applying a uniform scaling about the origin
       by a factor s.
       If s>1 the Picture will grow.
       If 0<s<1 the Picture will shrink.
   */
   public Picture size(double s) {
      return new Transform
         (this, AffineTransform.getScaleInstance(s,s));
   }

   /** Return the Picture produced by taking this picture
       and scaling by amount sx in the x-direction,
       and sy in the y-direction.
   */
   public Picture scale(double sx, double sy) {
      return new Transform
         (this, AffineTransform.getScaleInstance(sx,sy));
   }

   /** Return the Picture produced by taking this picture
       and applying a shear by amounts shx in the x-direction,
       and shy in the y-direction.
   */
   public Picture shear(double shx, double shy) {
      return new Transform
         (this, AffineTransform.getShearInstance(shx,shy));
   }

   /** Return the Picture that consists of this Picture
       on top of the picture p.
   */
   public Picture over(Picture p) {
      return new Overlay(this,p);
   }

   /** Return the Picture that consists of this Picture
       with the Picture p on top of it.
   */
   public Picture under(Picture p) {
      return new Overlay(p,this);
   }


   /* Methods for displaying a picture. */

   /** Create a JFrame that displays this picture.
       (x,y) = picture coordinates of bottom-left corner
               of area of picture to be displayed.
       (w,h) = width and height of area to be displayed.
       s = number of pixels per unit distance in picture.
       b = background colour.
   */
   public void show
      (double x, double y, double w, double h,
       double s, Color b) {
      AffineTransform t = new AffineTransform();
      t.scale(s,s);
      t.translate(0,h);
      t.scale(1,-1);
      t.translate(-x,-y);
      JPanel panel = new DisplayPanel(this,t);
         // (DisplayPanel is defined below.)
      int width = (int) Math.round(w*s);
      int height = (int) Math.round(h*s);
      panel.setPreferredSize(new Dimension(width, height));
      panel.setBackground(b);

      JFrame frame = new JFrame();
      frame.setLocation(100,100);
      frame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
      frame.getContentPane().add(panel);
      frame.pack();
      frame.setVisible(true);
   }

   /*********************************************************/

   /* This class defines a JPanel that will display
      the picture passed to it when it is created.
   */
   private class DisplayPanel extends JPanel {

      private Picture picture;
         // The picture to be displayed.

      private AffineTransform convert;
         // Transformation that converts to user coordinates.

      private DisplayPanel (Picture p, AffineTransform t) {
         picture = p;
         convert = t;
      }

      public void paintComponent(Graphics g) {
         final AffineTransform ident = new AffineTransform();
         super.paintComponent(g);
         Graphics2D g2 = (Graphics2D) g;
         g2.transform(convert);
         picture.transDisplay(ident,g2);
      }
   }

   /************************************************************/

   /** Create a BufferedImage that contains this picture.
       (x,y) = picture coordinates of bottom-left corner
               of area of picture to be stored in buffer.
       (w,h) = width and height of area to be stored.
       s = number of pixels per unit distance in picture.
       bg = background colour.
   */
   public BufferedImage makeBuffer
      (double x, double y, double w, double h,
       double s, Color bg) {
      int width = (int) Math.round(w*s);
      int height = (int) Math.round(h*s);
      int bufferType = BufferedImage.TYPE_INT_RGB;
      BufferedImage buffer =
         new BufferedImage(width, height, bufferType);
      Graphics2D g2 = buffer.createGraphics();
      g2.setColor(bg);
      g2.fillRect(0,0,width,height);
      AffineTransform t = new AffineTransform();
      t.scale(s,s);
      t.translate(0,h);
      t.scale(1,-1);
      t.translate(-x,-y);
      g2.transform(t);
      AffineTransform ident = new AffineTransform();
      this.transDisplay(ident,g2);
      g2.dispose();
      return buffer;
   }

   /************************************************************/

   /** Apply transformation t to this Picture,
       and display the result using Graphics2D g2.
       (This method is defined separately for each different
        subclass of Picture: BasicShape, Empty, Overlay
        and Transform.)
   */
   public abstract void
      transDisplay(AffineTransform t, Graphics2D g2);
}

⌨️ 快捷键说明

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