I currently have a multi-class program that uses the processing library set up in Eclipse. I was wondering if there if there is a Text object in a 3rd party library somewhere that I can use to create text objects on the screen, and crucially, move these text objects around without having to redraw them to the screen. Are there any such classes out there?
Eg. a class called Text init as Text textObject = new Text("String", x, y)
with a method similar to textObject.move(dx, dy)
If you don't find a library to do it, here's a minimal implementation. If you like, I could wrap it up in a library so that the objects draw themselves. If you're using eclipse, you'll probably need to add some parent.s before the PApplet calls.
TextObject to;
void setup(){
size(400,400);
to = new TextObject("test", 0, height/2);
}
void draw(){
background(0);
if(frameCount % 300 == 0) to.set(0, height/2);
to.move(1,0);
to.display();
}
class TextObject
{
float x, y;
String text;
PFont f;
TextObject(String s, float x, float y){
this.x = x;
this.y = y;
this.text = s;
f = createFont("Georgia", 32);
}
TextObject(String s, float x, float y, PFont f){
this.x = x;
this.y = y;
this.text = s;
this.f = f;
}
void set(float x, float y){
this.x = x;
this.y = y;
}
void move(float dx, float dy){
x += dx;
y += dy;
}
void display(){
textFont(f);
text(text, x, y);
}
}
Related
I have been really stuck on this for the past day or so and I could really use somebody's input as to where I'm going wrong
So I have a 'Play Game' image, I'm loading it into my game as follows:
playGame = new SimpleControl(250.0f, 225.0f, 140.0f, 30.0f, "PlayBtn", this);
The SimpleControl object looks like this:
public SimpleControl(float x, float y, float width, float height,
String bitmapName, GameScreen gameScreen) {
super(x, y, width, height, gameScreen.getGame().getAssetManager()
.getBitmap(bitmapName), gameScreen);
public boolean isActivated() {
// Consider any touch events occurring in this update
Input input = mGameScreen.getGame().getInput();
// Check if any of the touch events were on this control
BoundingBox bound = getBound();
for (int idx = 0; idx < TouchHandler.MAX_TOUCHPOINTS; idx++) {
if (input.existsTouch(idx)) {
if (bound.contains(input.getTouchX(idx), input.getTouchY(idx))) {
return true; }}}
return false; }
I have an update method in my Game class which goes like this:
Input input = mGame.getInput();
List<TouchEvent> touchEvents = input.getTouchEvents();
if (touchEvents.size() > 0) {
TouchEvent touchevent = touchEvents.get(0);
{
if (playGame.isActivated()) {
mGame.getScreenManager().removeScreen(this.getName());
AboutScreen aboutScreen = new AboutScreen(mGame);
mGame.getScreenManager().addScreen(aboutScreen);
}
Unfortunately when I'm running this, the button does not pick up any touch event but there's a little section of the screen that does:
http://i.imgur.com/R4nVIRP.png
BoundingBox.java:
public BoundingBox(float x, float y, float halfWidth, float halfHeight) {
this.x = x;
this.y = y;
this.halfWidth = halfWidth;
this.halfHeight = halfHeight;
}
public boolean contains(float x, float y) {
return (this.x - this.halfWidth < x && this.x + this.halfWidth > x
&& this.y - this.halfHeight < y && this.y + this.halfHeight > y);
}
Thank you in advance :)
Your contains(float x, float y) looks incorrect.
this.x - this.halfwidth < x means it checks for x at a location before the start of the bounding box.
For example your x should be -
(x>=this.x && x<=(this.x + 2*this.halfwidth))
And y should be
(y>=this.y && y<=(this.y + 2.this.halfHeight))
(Assuming halfWidth and halfHeight are what they say)
(also, can't open the imgur link since its blocked for us)
I have created a custom Path2D class to draw an H-shaped "calliper" on screen, for a project I am doing. I want to drag and eventually resize the calliper on screen. I have managed to get the Path2D set up so I can draw the calliper, and the code looks like this:
Declaration and Constructor:
public class Calliper extends Path2D.Double
{
// X and Y coordinates of all six points on Calliper
double cX1, cX2, cX3, cX4, cX5, cX6;
double cY1, cY2, cY3, cY4, cY5, cY6;
// Width and Height
double cWidth;
double cHeight;
public Calliper(double x, double y, double w, double h)
{
cWidth = w;
cHeight = h;
cX1 = x;
cY1 = y;
cX2 = x;
cY2 = y + (h/2);
cX3 = x;
cY3 = y + h;
cX4 = x + w;
cY4 = y;
cX5 = cX4;
cY5 = cY4 + (h /2);
cX6 = cX4;
cY6 = cY4 + h;
build();
}
build() method (used to draw the path) and setCalliper() method, used to redefine the coordinates, or width, height:
private void build()
{
// Draw the path for the calliper
moveTo(cX1, cY1);
lineTo(cX2, cY2);
lineTo(cX3, cY3);
moveTo(cX2, cY2);
lineTo(cX5, cY5);
moveTo(cX4, cY4);
lineTo(cX6, cY6);
}
public void setCalliper(double x, double y, double w, double h)
{
// Rebuild the calliper using different x,y coordinates, or
// different width/height
cWidth = w;
cHeight = h;
cX1 = x;
cY1 = y;
cX2 = x;
cY2 = y + (h/2);
cX3 = x;
cY3 = y + h;
cX4 = x + w;
cY4 = y;
cX5 = cX4;
cY5 = cY4 + (h /2);
cX6 = cX4;
cY6 = cY4 + h;
build();
}
I have created a class to draw this calliper on the screen, which it will do, however if I try to drag the calliper around the screen, it doesn't erase the original shape as I drag, so I get a long trail of shapes left behind. I thought I had omitted super.paintComponent(g) from my paintComponent(Graphics g) method, but even with it in there the code still does not work.
My drag method looks like this:
#Override
public void mouseDragged(MouseEvent ev)
{
double mx = ev.getX();
double my = ev.getY();
if (dragging)
{
calX = mx - offsetX;
calY = my - offsetY;
cal = setCalliper(calX, calY, calW, calH);
repaint();
}
}
If I change the line cal = setCalliper(calX, calY, calW, calH); above to read cal = new Calliper(calX, calY, calW, calH); then it works, but I have been told I shouldn't do it this way.
Any ideas why it doesn't work as expected?
The setCalliper() directly calls the build method, a method which appends new points to all the previous points added to the Path2D - so each time mouseDragged is called more points are added to the Path. Try calling reset() before calling build() (or call reset in the build method before the moveTo/lineTo calls).
I am working with a Point object that has an x and y component, Point(double x, double y). I want to write a function that changes the values of the x and y component without having a
new Point p = ...
For Example, this is my current version:
public class Point{
private double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public Point movePoint(double dx, double dy) {
return new Point(this.x + dx, this.y + dy);
}
}
Is it possible to do something like movePoint() without making a new Point?
Thanks in advance.
Certainly. Just change your code to return a reference to this as such:
public Point movePoint(double dx, double dy) {
this.x += dx;
this.y += dy;
return this;
}
Also note that Java has a built in class for storing double precision points in its java.awt.geom.Point2D.Double class.
Sure, try this:
public class Point{
private double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public Point movePoint(double dx, double dy) {
this.x += dx;
this.y += dy;
return this;
}
}
I have a Point class.
The point can be both in 2-D and 3-D. I am deciding this based on the length of the coordinate array passed to the constructor.
double x, y, z;
int dimension;
Point(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
dimension = 3;
}
Point(double x, double y) {
this.x = x;
this.y = y;
this.z = 0;
dimension = 2;
}
Point(double[] p)
{
if(p.length == 2)
this(p[0], p[1]);
else if(p.length == 3)
this(p[0], p[1], p[2]);
}
The last constructor gives error because constructor call must be the first statement in a constructor.
Is there a way to achieve what I'm doing?
May be you can do something like
double x, y, z;
int dimension;
Test(double x, double y, double z) {
initDim(x, y, z);
}
Test(double x, double y) {
initDim(x, y);
}
Test(double[] p)
{
if(p.length == 2)
initDim(p[0], p[1]);
else if(p.length == 3)
initDim(p[0], p[1], p[2]);
}
private void initDim(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
dimension = 3;
}
private void initDim(double x, double y) {
initDim(x, y, 0);
dimension = 2;
}
Addition to those i would like suggest a solution like this. Less constructors and easily adaptable. Bit similar to what you do to create a singleton.
public class Point {
double x, y, z;
int dimension;
private Point(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
dimension = 3;
}
private Point(double x, double y) {
this.x = x;
this.y = y;
this.z = 0;
dimension = 2;
}
public Point getInstance(double x, double y, double z) {
return new Point(x, y, z);
}
public Point getInstance(double x, double y) {
return new Point(x, y);
}
public Point getInstance(double[] p) {
if (p.length == 2)
return new Point(p[0], p[1]);
else (p.length == 3)
return new Point(p[0], p[1], p[2]);
}
}
You can create your instance like this.
Point point = Point.getInstance(0, 0);
It's generally not good to have too many constructors. It gives you a good opportunity to express how it should be used.
public static Point Create(double... p)
{
if(p.length == 2)
return Point(p[0], p[1]);
else if(p.length == 3)
return Point(p[0], p[1], p[2]);
// default case or throw error
}
Alternatively you can create an initialize method, which you call from the constructors.
I'm having trouble formatting when using coordinates.
public class Coordinate {
public int x;
public int y;
public Coordinate( int x, int y) {
this.x = x;
this.y = y;
}
}
So, later, when I'm trying to find the location of my rabbit, I use:
Coordinate (x, y) = rabbit.get(i);
And that doesn't work, but this does:
Coordinate z = rabbit.get(i);
I want to find the x and y values so I'm confused as to how to do that and why the Coordinate (x, y) doesn't work. Thanks for your help!
As your attributes x,y of Coordinate are public:
Coordinate z = rabbit.get(i);
int xCor = z.x; //this is your x coordinate
int yCor = z.y; //this is your y coordinate
Normaly these attriubtes are private and you access them with a getter/setter-Method:
public class Coordinate {
private int x;
private int y;
public Coordinate( int x, int y) {
this.x = x;
this.y = y;
}
public int getX(){
return this.x;
}
public void setX(int newX){
this.x = newX;
}
//same for Y
}
//in the main program.
Coordinate z = rabbit.get(i);
int yourX = z.getX() //this is your x coordinate
int yourY = z.getY() //this is your y coordinate
I assume you use Java, so I added the Tag, this enables highlighting. This works with other languages in the same way.