Issue
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);
}
}
}
Solution
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).
Answered By - Radiodef
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.