AWT Rectangle2D 類



簡介

Rectangle2D 類表示由位置 (x,y) 和尺寸 (w x h) 定義的矩形。

類宣告

以下是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 上方的位掩碼。

類建構函式

序號建構函式和說明
1

protected Rectangle2D()

這是一個抽象類,不能直接例項化。

類方法

序號方法和說明
1

void add(double newx, double newy)

將由雙精度引數 newx 和 newy 指定的點新增到此 Rectangle2D。

2

void add(Point2D pt)

將 Point2D 物件 pt 新增到此 Rectangle2D。

3

void add(Rectangle2D r)

將 Rectangle2D 物件新增到此 Rectangle2D。

4

boolean contains(double x, double y)

測試指定的座標是否在 Shape 的邊界內。

5

boolean contains(double x, double y, double w, double h)

測試 Shape 的內部是否完全包含指定的矩形區域。

6

abstract Rectangle2D createIntersection(Rectangle2D r)

返回一個新的 Rectangle2D 物件,表示此 Rectangle2D 與指定的 Rectangle2D 的交集。

7

abstract Rectangle2D createUnion(Rectangle2D r)

返回一個新的 Rectangle2D 物件,表示此 Rectangle2D 與指定的 Rectangle2D 的並集。

8

boolean equals(Object obj)

確定指定的物件是否等於此 Rectangle2D。

9

Rectangle2D getBounds2D()

返回 Shape 的高精度和更精確的邊界框,比 getBounds 方法更精確。

10

PathIterator getPathIterator(AffineTransform at)

返回一個迭代器物件,該物件定義此 Rectangle2D 的邊界。

11

PathIterator getPathIterator(AffineTransform at, double flatness)

返回一個迭代器物件,該物件定義扁平化 Rectangle2D 的邊界。

12

int hashCode()

返回此 Rectangle2D 的雜湊碼。

13

static void intersect(Rectangle2D src1, Rectangle2D src2, Rectangle2D dest)

將一對指定的源 Rectangle2D 物件相交,並將結果放入指定的目的地 Rectangle2D 物件中。

14

boolean intersects(double x, double y, double w, double h)

測試 Shape 的內部是否與指定的矩形區域的內部相交。

15

boolean intersectsLine(double x1, double y1, double x2, double y2)

測試指定的線段是否與此 Rectangle2D 的內部相交。

16

boolean intersectsLine(Line2D l)

測試指定的線段是否與此 Rectangle2D 的內部相交。

17

abstract int outcode(double x, double y)

確定指定的座標相對於此 Rectangle2D 的位置。

18

int outcode(Point2D p)

確定指定的 Point2D 相對於此 Rectangle2D 的位置。

19

void setFrame(double x, double y, double w, double h)

將此 Rectangle2D 的外部邊界的位 置和大小設定為指定的矩形值。

20

abstract void setRect(double x, double y, double w, double h)

將此 Rectangle2D 的位置和大小設定為指定的雙精度值。

21

void setRect(Rectangle2D r)

將此 Rectangle2D 設定為與指定的 Rectangle2D 相同。

22

static void union(Rectangle2D src1, Rectangle2D src2, Rectangle2D dest)

將一對源 Rectangle2D 物件合併,並將結果放入指定的目的地 Rectangle2D 物件中。

繼承的方法

此類繼承自以下類的方法

  • java.awt.geom.RectangularShape

  • java.lang.Object

Rectangle2D 示例

使用您選擇的任何編輯器建立以下 Java 程式,例如在D:/ > AWT > com > tutorialspoint > gui >中。

AWTGraphicsDemo.java
package com.tutorialspoint.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 com\tutorialspoint\gui\AWTGraphicsDemo.java

如果沒有錯誤,則表示編譯成功。使用以下命令執行程式。

D:\AWT>java com.tutorialspoint.gui.AWTGraphicsDemo

驗證以下輸出

AWT Rectangle2D
awt_graphics.htm
廣告
© . All rights reserved.