位置:首页 > 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