简体   繁体   中英

Fields of java.awt.geom.Rectangle2D?

Not a Java person here, I'm just trying this code in Processing.org :

//import java.awt.Rectangle; // int only
import java.awt.geom.Rectangle2D; 

Rectangle2D testr;

void setup() {
  testr = new Rectangle2D.Float(1.0, 1.0, 30.0, 30.0);
  println(testr);
  //println(testr.x);
}

It prints out:

java.awt.geom.Rectangle2D$Float[x=1.0,y=1.0,w=30.0,h=30.0]

... which sort of implies where are x and y fields accessible? Even Rectangle2D.Float (Java Platform SE 7 ) says:

Field Summary
...
float x
The X coordinate of this Rectangle2D.

And yet, if I uncomment println(testr.x); , compilation fails with:

testr.x cannot be resolved or not a field.

Where am I going wrong - and why cannot I access a field, which the documentation clearly says exists?

Btw, this is what I find in /tmp as the full .java source when the processing .pde manages to compile:

import processing.core.*; 
import processing.data.*; 
import processing.event.*; 
import processing.opengl.*; 

import java.awt.geom.Rectangle2D; 

import java.util.HashMap; 
import java.util.ArrayList; 
import java.io.File; 
import java.io.BufferedReader; 
import java.io.PrintWriter; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.IOException; 

public class sketch_140204c extends PApplet {


//import java.awt.Rectangle; // int only


Rectangle2D testr;

public void setup() {
  testr = new Rectangle2D.Float(1.0f, 1.0f, 30.0f, 30.0f);
  println(testr);
}

  static public void main(String[] passedArgs) {
    String[] appletArgs = new String[] { "sketch_140204c" };
    if (passedArgs != null) {
      PApplet.main(concat(appletArgs, passedArgs));
    } else {
      PApplet.main(appletArgs);
    }
  }
}

These fields are declared on Rectangle2D.Float but testr is just a Rectangle2D . Changing its declaration will let you access them since they are public:

Rectangle2D.Float testr;

It's a good suggestion to use getters and setters but there aren't individual setters for these classes. Only setters to set the entire rectangle (namely setRect ).

These fields are declared on Rectangle2D.Float you can print to control.

Ellipse2D.Float test= new Ellipse2D.Float(getPosX()-40,getPosY()-40,30.0f, 30.0f);
        System.out.println(test.getCenterX());
        System.out.println(test.getCenterY());
        System.out.println(test.getMinX());
        System.out.println(test.getMinY());
        System.out.println(test.getMaxX());
        System.out.println(test.getMaxY());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM