位置:首頁 > Java技術 > AWT > AWT Rectangle2D類

AWT Rectangle2D類

Rectangle2D類規定的位置(X,Y)和尺寸(寬x高)定義的矩形。

類的聲明

以下是聲明類java.awt.geom.Rectangle2D:

public abstract class Rectangle2D
   extends RectangularShape

字段域

以下是java.awt.geom.Arc2D類字段:

  • static int OUT_BOTTOM -- 位掩碼,表示一個點在於低於此Rectangle2D。

  • static int OUT_LEFT -- 位掩碼,表示一個點位於此Rectangle2D的左側。

  • static int OUT_RIGHT -- 位掩碼,表示一個點位於此Rectangle2D的右側。

  • static int OUT_TOP -- 位掩碼,表示此Rectangle2D一個點之上。

類的構造函數

S.N. 構造函數與說明
1 protected Rectangle2D() 
這是一個抽象類,不能直接實例化。

類方法

S.N. 方法和說明
1 void add(double newx, double newy) 
Adds a point, specified by the double precision arguments newx and newy, to this Rectangle2D.
2 void add(Point2D pt) 
Adds the Point2D object pt to this Rectangle2D.
3 void add(Rectangle2D r) 
Adds a Rectangle2D object to this Rectangle2D.
4 boolean contains(double x, double y) 
Tests if the specified coordinates are inside the boundary of the Shape.
5 boolean contains(double x, double y, double w, double h) 
Tests if the interior of the Shape entirely contains the specified rectangular area.
6 abstract Rectangle2D createIntersection(Rectangle2D r) 
Returns a new Rectangle2D object representing the intersection of this Rectangle2D with the specified Rectangle2D.
7 abstract Rectangle2D createUnion(Rectangle2D r) 
Returns a new Rectangle2D object representing the union of this Rectangle2D with the specified Rectangle2D.
8 boolean equals(Object obj) 
Determines whether or not the specified Object is equal to this Rectangle2D.
9 Rectangle2D getBounds2D() 
Returns a high precision and more accurate bounding box of the Shape than the getBounds method.
10 PathIterator getPathIterator(AffineTransform at) 
Returns an iteration object that defines the boundary of this Rectangle2D.
11 PathIterator getPathIterator(AffineTransform at, double flatness) 
Returns an iteration object that defines the boundary of the flattened Rectangle2D.
12 int hashCode() 
Returns the hashcode for this Rectangle2D.
13 static void intersect(Rectangle2D src1, Rectangle2D src2, Rectangle2D dest) 
Intersects the pair of specified source Rectangle2D objects and puts the result into the specified destination Rectangle2D object.
14 boolean intersects(double x, double y, double w, double h) 
Tests if the interior of the Shape intersects the interior of a specified rectangular area.
15 boolean intersectsLine(double x1, double y1, double x2, double y2) 
Tests if the specified line segment intersects the interior of this Rectangle2D.
16 boolean intersectsLine(Line2D l) 
Tests if the specified line segment intersects the interior of this Rectangle2D.
17 abstract int outcode(double x, double y) 
Determines where the specified coordinates lie with respect to this Rectangle2D.
18 int outcode(Point2D p) 
Determines where the specified Point2D lies with respect to this Rectangle2D.
19 void setFrame(double x, double y, double w, double h) 
Sets the location and size of the outer bounds of this Rectangle2D to the specified rectangular values.
20 abstract void setRect(double x, double y, double w, double h) 
Sets the location and size of this Rectangle2D to the specified double values.
21 void setRect(Rectangle2D r) 
Sets this Rectangle2D to be the same as the specified Rectangle2D.
22 static void union(Rectangle2D src1, Rectangle2D src2, Rectangle2D dest) 
Unions the pair of source Rectangle2D objects and puts the result into the specified destination Rectangle2D object.

繼承的方法

這個類繼承的方法從以下類:

  • java.awt.geom.RectangularShape

  • java.lang.Object

Ellipse2D 實例

選擇使用任何編輯器創建以下java程序D:/ > AWT > com > yiibai > gui >

AWTGraphicsDemo.java
package com.yiibai.gui;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class AWTGraphicsDemo extends Frame {
       
   public AWTGraphicsDemo(){
      super("Java AWT Examples");
      prepareGUI();
   }

   public static void main(String[] args){
      AWTGraphicsDemo  awtGraphicsDemo = new AWTGraphicsDemo();  
      awtGraphicsDemo.setVisible(true);
   }

   private void prepareGUI(){
      setSize(400,400);
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      }); 
   }    

   @Override
   public void paint(Graphics g) {
      Rectangle2D shape = new Rectangle2D.Float();
      shape.setFrame(100, 150, 200,100);
      Graphics2D g2 = (Graphics2D) g; 
      g2.draw (shape);
      Font font = new Font("Serif", Font.PLAIN, 24);
      g2.setFont(font);
      g.drawString("Welcome to TutorialsPoint", 50, 70);
      g2.drawString("Rectangle2D.Rectangle", 100, 120);
   }
}

編譯程序,使用命令提示符。進入到D:/> AWT,然後鍵入以下命令。

D:AWT>javac comyiibaiguiAWTGraphicsDemo.java

如果冇有錯誤出現,這意味著編譯成功。使用下麵的命令來運行程序。

D:AWT>java com.yiibai.gui.AWTGraphicsDemo

驗證下麵的輸出

AWT Rectangle2D