I have some logic which I am using to construct a series of clusters. So far, to denote the cluster to which each point on the graph belongs to, I am using a series of colours, where points belonging to the same cluster are of the same colour.
Besides that, I would also like to display the centre of each cluster since this will help me see how my cluster building algorithm performs. To do this at the moment, I am writing some text on the graph through the use of the XPointerAnnotation class. The problem with this is that I think that having text on top of points can lead to a messy plot (considering that it is highly likely that there will be hundreds of points).
I thought of drawing lines going outwards, from the centre point to each of the members of its cluster. The problem I am facing is that I can't quite seem to find the correct method or methods which does that.
I have managed to find the source of XYLineAndShapeRenderer and have tried to use it as a guide, but I still get no custom lines drawn on the plot. I have tried to override the drawPrimaryLine, drawPrimaryLineAsPath and drawSecondaryPass methods, but to no avail.
The code I am using to render the lines is as follows:
int x1 = (int) dataset.getXValue(series, 0);
int y1 = (int) dataset.getYValue(series, 0);
int x2 = (int) dataset.getXValue(series, item);
int y2 = (int) dataset.getYValue(series, item);
g2.drawLine(x1, y1, x2, y2);
System.out.println(String.format("Drawing %d %d %d %d %s", x1, y1, x2, y2, g2.getColor()));
State s = (State) state;
if (item == s.getLastItemIndex()) {
// draw path
drawFirstPassShape(g2, pass, series, item, s.seriesPath);
}
The print statement prints the right coordinates and the right colours, so it just seems that the graphics that I am adding is not being rendered. I have tried calling super, both before and after my code is executed but to no avail either.
Any directions would be appreciated.
Thanks.
Looking more closely at the code posted, the xy value obtained from the dataset represents a point in data coordinates. Before such a point can be rendered, it must be transformed into graphics coordinates, relative to the dataArea. As an example, drawPrimaryLineAsPath() uses the corresponding axis method, valueToJava2D(), to convert a data value to a graphics coordinate.
double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation);
double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation);
Addendum: The drawPrimaryLineAsPath() method is invoked from drawItem() only when drawSeriesLineAsPath is true, e.g. setDrawSeriesLineAsPath(true).
Related
I am making a 3D Java game but i have got problems when rotating a hitbox. I, upon this point, only used a method which detects if a Vector3f is in a box.
But in my game I want to rotate houses, for example, so that method won't work. I could use circulair hitboxes but that wouldn't work for every instance of objects.
So far i have used this simple calculation to detect if a location is in a hitbox.
public boolean isinbox(Vector3f pos) {
Vector3f entPos = ent.getPosition();
float x1 = entPos.x + xOffset;
float z1 = entPos.z + zOffset;
float y1 = entPos.y + yOffset;
float x2 = entPos.x - xOffset;
float z2 = entPos.z - zOffset;
float y2 = entPos.y;
return pos.x < x1 && pos.x > x2 && pos.z < z1 && pos.z > z2 && pos.y > y2 && pos.y < y1;
}
This works in many ways, but I can't figure out how to rotate them and still be able to detect it. The xOffset is the ofset if side a to the center and negative side b to the center.
How would I be able to rotate a hitbox and detect if an Vector is in it?
There are a couple of ways of getting around this issue and one (or more) ways of solving this issue:
Solving It
SAT Collision
SAT Stands for Separating Axis Theorem.
TutsPlus and MetaSoftware are great websites to learn how it works and how to implement it.
The Separating Axis Theorem (SAT for short) essentially states if you are able to draw a line to separate two polygons, then they do not collide. It's that simple. (gamedevelopment.tutsplus.com)
Here is the basic idea:
It can also be used for more complex shapes:
Getting Around It
AABB Collision
This is done by assuming that collisions will only ever happen on the X/Y axis (never on an axis defined by an arbitrary line).
Here is an example of what this looks like:
Therefore, you must define this axis-aligned hitbox by using the minimum X-and-Y values and the maximum X-and-Y values of the original box.
Circle Collision
This is a much simpler collision check which only detects when two objects are within a certain distance of each other.
Here is an example of what this looks like:
The way this works is that if the distance between the two objects is less than the sum of each circle's radius, then the objects are colliding.
Using an External Library
Bullet Physics
Box2D
So I just started learning Java yesterday coming from a different language, and I am reading through my textbook and finding it to be pretty nice so far. However I did an exercise that basically required me to create a new Object use Rectangle and find the area. Below is the working code I came up with.
Now coming from other programming languages I was just toying around with this and did int area,width,height;and then it gave me an error saying that I had to use double in order to utilize .getWidth();, .getHeight(). I couldn't find anything in my book telling me why I had to make this a double and I started looking online and found this link
Now I found some documentation online where It told me to use double as well, but I'm not really sure why would I need to set these as doubles. Is it because the people who made Java, knew that precision is needed when we are working with coordinates and doing math with widths, heights and coordinates? My book says that it takes more memory to make a double variable rather than an int ( I come from doing lots of javascript and PHP, so reading on what a float and double does was something good for me).
I.E. Why do I need to make my area,height,width variable doubles in order to use .getWidth,.getHeight
package keepo;
import java.awt.Rectangle;
public class tuna{
public static void main(String [] args){
Rectangle rect = new Rectangle(10,20,50,40);
double area,width,height;
width = rect.getWidth();
height = rect.getHeight();
area = width * height;
System.out.println("Width is : " + width + "Height is : " + height);
System.out.println("Area is : " + area);
}
}
It is because this is how these methods have been defined in the java api. As you can see under the modifier and type column that the methods getWidth(), getHeight() all return value of type double.
Because in this case, you should not use those methods. The AWT class Rectangle does store coordinates as ints. You can easily read them back as ints if that's what you want to do, by accessing the fields instead of calling the getter methods:
int area, width, height;
width = rect.width; // not getWidth()
height = rect.height; // not getHeight()
area = width * height;
The getWidth() and getHeight() methods serve zero purpose here, as they will always return the same value as the fields, except as a different type (and you can already assign any int value to a double anyway, when a double is what you want to use).
So why do those two methods (along with getX() and getY()) exist at all? Because in Java 1.2 the geometry stuff in the API was expanded. People wanted to be able to work with floating-point coordinates, which Rectangle cannot do. And the Java maintainers couldn't change the fields of Rectangle from int to double because that would break backwards compatibility with how old code was already using it. So two new classes, Rectangle2D.Float and Rectangle2D.Double were added, which store coordinates as floats and doubles respectively.
But what if you want to work generically with any rectangle, without writing separate code for all the rectangle flavors? A new abstract class, Rectangle2D was also added, as the superclass of the three rectangle classes. This class is abstract (meaning it cannot be created on its own, as it is incomplete) and it does not store any coordinates itself. It does however, specify a contract that its subclasses follow (meaning that any Rectangle2D method is available in all three of its implementations). That includes the getWidth() and getHeight() methods that return doubles, regardless of the actual storage type of the particular rectangle.
Taking the abstraction an extra, perhaps superfluous, level, they also added RectangularShape as the superclass of several shapes with rectangular bounds: Rectangle2D, RoundRectangle2D, Ellipse2D and Arc2D. That is the class that actually declares the getWidth() and getHeight() methods, which all RectangularShape subclasses must provide:
// What's this shape? A rectangle? An ellipse? Does it use ints? floats? doubles?
RectangularShape something = ......;
// We don't care!
System.out.println("The shape (whatever it is) occupies an area of:");
System.out.println(something.getWidth() + " × " + something.getHeight());
So you can call those getter methods on any rectangle (or "rectangular shape") to get its coordinates, but if you know you have a particular shape class, you can/should access its fields directly, as that is simpler, and it gives you the values without converting them to a different type.
P.S. It is a similar story with Point, which uses int coordinates, but provides double getX() and double getY() methods, because of the later-added classes Point2D.Float, and Point2D.Double, and the abstract superclass Point2D.
P.P.S. There is actually a small advantage to using double (or long) for your rectangle's area, even if your rectangle coordinates are ints. Large multiplications could overflow the 32-bit range of an int, producing the wrong result. If you convert at least one of the values to a larger type, it will cause the multiplication to be done in that larger type, which you can then safely store without overflow:
Rectangle big = new Rectangle(0, 0, 1000000, 1000000);
int area = big.width * big.height;
long bigArea = (long)big.width * big.height;
System.out.println(area); // -727379968 (uh oh!)
System.out.println(bigArea); // 1000000000000
Imran Ali is right.
This is java documentations for getHeight() and for getWidth() it's same.
java.awt.Rectangle
public double getHeight()
Returns the height of the bounding Rectangle in double precisionReturns:
the height of the bounding Rectangle.
But if you want/need to use int instead of double, use following codes for height and repeat them for width too:
using getSize() method which returns rectangle dimension then use it's fields (width and height)
int height = rect.getSize().height;
using data type casting
int height = (int) rect.getHeight();
int height = (int) rect.getSize().getHeight();
The Rectangle.getWidth() and Rectangle.getHeight()methods both return their values with double precision, as stated by others. It is easier if you just keep using them, in order to prevent the Rectangle's values from being changed on accident, by simply casting the value to an int:
int width = (int)rect.getWidth()
and int height = (int)rect.getHeight()
I am drawing a series of Body objects on a Slick2D screen from a dyn4j World (World.getBodies()) and getting a very low frame rate, i.e. 6-10 FPS. Here is the part of the render loop in Slick2D that draws the dyn4j bodies:
for (int i = 0; i < space.getBodies().size(); i++) { // Iterate through World bodies
if (space.getBodies().get(i) instanceof lander.ShipFragmentPool.Fragment) {
lander.ShipFragmentPool.Fragment fragment = (lander.ShipFragmentPool.Fragment) space.getBodies().get(i);
float x = (float) (fragment.getWorldCenter().x * scale); // Extract fragementco-ordinates
float y = (float) (fragment.getWorldCenter().y * scale);
float tempX = x % bWIDTH;
if (tempX > bufferX // Only draw fragments that are on screen
&& tempX < bufferX + sWIDTH) {
float radius = (float) (fragment.getRadius() * scale); // Get radius
float diameter = (float) (radius * 2); // And diameter
float drawX = (tempX - bufferX) - radius; // Co-ordinates to draw on screen
float drawY = ((bHEIGHT - bufferY) - y) - radius;
g.setColor(Color.white);
g.drawOval(drawX, drawY, diameter, diameter);
}
}
}
I am guessing that the main issue that I am iterating over 36 bodies in the World object and having to test for object type in each case. The returned Body objects are of different types and so I have to test each body to see if it an instance of the type I want to render (ship fragments after an explosion). Is there a better (i.e. faster) way to construct this loop? Or is there another issue that I am missing?
Note:
in the example above bufferY is fixed (the screen doesn't move when the explosion is happening)
bWIDTH/bHEIGHT are the width and height of the background image, sWIDTH is the width of the screen
I am iterating over a set of Body objects in dyn4J because I want to the individual explosion fragments to interact with the landscape, e.g. bounce, slide, etc.
the Body objects have a single Fixture and this is a Circle
Thanks
Turns out it isn't the graphics drawing routine... it is the dyn4j iteration of the fragments during the World.update() method. Changing the fragment Fixtures to sensors (Fixture.setSensor(true)) so they detect collisions, but don't respond to them, resolves the performance issues and I get about 130FPS if I run the game unchecked. Shame, given that it's only 36 objects. I didn't think that would be too many for dyn4j to handle... I suspect I am missing some setting somewhere to make this work effectively. :/
ps. the for (Object spaceBody : spaceBodies) in my previous comment does work. Not sure what happened there, but a clean and build sorted it out.
Im trying to get a rectangle to move from a fixed location, to a click point in my game. I have it working to the point that im getting good coordinates from the e.getX and e.getY methods in the mouse listener class. I also have the rectangle moving in straight lines to the side or up and down by increasing and decreasing the positionX and Y values but i need a way to move it to any point that i click on, basically i need to set the change in x and y. The way i was thinking is to compute the rise and run of the line from the click points and the start point and then set the x and y values to the rise and run. If i do it this way, i need a way to decrease the rise and run values down to their lowest terms so it moves fluidly. Or if there is a way im not thinking of that you can offer any guidence that would be helpfull.
How are you drawing it at the moment? Below is a method I used to shoot bullets in a little game that I made/really should finish sometime.
This is by no means all the code you will need, and in some places there are better ways to do things (which I'm sure people will start to tell me in the comments section after :) ) but it should give you a basis to work from.
The one thing missing from this is regulating the speed at which it repaints (fps), sorry but I can't remember the simple way to do this right now (need to get back to my actual job!) Hope it helps.
The barebones of it will be something like this:
//these set out the current position of your rectangle
double recX, reYy, recH, recW;
//this gives something to manage speed
int speed;
public void paintComponent(Graphics g) {
g.drawRectangle(recX,rexY,recH,recW);
}
//here we work out the movement
public void clickedScreen(double x, double y){
double newX = x;
double newY = y;
//calculate the speed to move at
vX = newX - recX;
vY = newY - recY;
//get the distance
length = Math.sqrt((v1*v1)+(v2*v2));
//make it a unit vector
v1 = v1/length;
v2 = v2/length;
}
public moveRec(){
recX = recX+(v1*speed);
recY = recY+(v2*speed);
}
while(true) {
moveRec();
repaint();
}
My assignment is to implement an algorithm to color a closed shape starting from a given (x,y) coordinate and "spread" via recursive calls untill it reaches the borders of the shape. So far this is what I've come up with:
private void color(int x, int y) {
g2d.draw(new Line2D.Double(x, y, x, y));
if (!robot.getPixelColor(x - 1, y).equals(Color.BLACK) &&
!robot.getPixelColor(x - 1, y).equals(Color.RED)) {
color(x - 1, y);
} else if (!robot.getPixelColor(x + 1, y).equals(Color.BLACK) &&
!robot.getPixelColor(x - 1, y).equals(Color.RED)) {
color(x + 1, y);
} else if (!robot.getPixelColor(x, y - 1).equals(Color.BLACK) &&
!robot.getPixelColor(x - 1, y).equals(Color.RED)) {
color(x, y - 1);
} else if (!robot.getPixelColor(x, y + 1).equals(Color.BLACK) &&
!robot.getPixelColor(x - 1, y).equals(Color.RED)) {
color(x, y + 1);
}
}
The Robot class' getPixelColor is the only way I found to get the color of a given pixel (as far as I know another would be getRGB, but that only works on Image objects). To my understanding this should work, as the outer lines of the shape are definitely black, and the initial x and y values come from a MouseListener, so they are inside the shape, however I get the following error:
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at sun.java2d.pipe.BufferedContext.validateContext(BufferedContext.java:110)
at sun.java2d.d3d.D3DRenderer.validateContextAA(D3DRenderer.java:42)
at sun.java2d.pipe.BufferedRenderPipe$AAParallelogramPipe.fillParallelogram(BufferedRenderPipe.java:445)
at sun.java2d.pipe.PixelToParallelogramConverter.drawGeneralLine(PixelToParallelogramConverter.java:264)
at sun.java2d.pipe.PixelToParallelogramConverter.draw(PixelToParallelogramConverter.java:121)
at sun.java2d.SunGraphics2D.draw(SunGraphics2D.java:2336)
at dline.DrawingSpace.color(DrawingSpace.java:87)
at dline.DrawingSpace.color(DrawingSpace.java:93)
at dline.DrawingSpace.color(DrawingSpace.java:90)
at dline.DrawingSpace.color(DrawingSpace.java:93)
at dline.DrawingSpace.color(DrawingSpace.java:90)
(drawingSpace is a sub-class of JPanel)
The teacher did tell us that this is memory consuming, however it's supposed to be a working algorithm, so I'm doing something wrong, obviously. Any help would be much appriciated, thank you.
You can try to increase the Stack size: How to increase the Java stack size?
Probably you have a bug in your algorithm, or the shape is too big. What helps if you 'draw' your algorithm on a piece of graph paper. That way you can check your algorithm.
I'm guessing that you're backtracking onto previously visited pixels. The pixel you just drew probably won't be visible to robot until after you return from color, so it will not appear red from the previous painting.
Do you have a reference to the java.awt.Shape? A much simpler way than using the robot would be to use Shape.contains(Point) to see whether it's in the shape you're supposed to draw.
The basic algorithm either way is depth-first traveral. To do a DFS when there are possible cycles, you can record the pixels you've already drawn.
//java.awt.Point
Set<Point> paintedPixels = new HashSet<Point>();
private void color(int x, int y) {
if ( paintedPixels.contains(new Point(x, y)) ) {
//already painted
return;
}
paintedPixels.add(new Point(x, y));
//...
}
Now, this could still result in a very deep search. You might consider instead using a non-recursive breadth-first traveral. See the Wikipedia article on Flood Fill.
The problem with implementing this as a recursive algorithm is that it has (for bigger images) a very high recursion depth.
In Java (and most other imperative programming languages, too) the maximal recursion depth is limited by the amount of stack space for each thread, since it must keep a stack frame for each method invocation there.
You may try smaller images first, and try to increase the stack size with the -xss parameter.
Edit: As pointed out by Mark, the Robot will not get any pixels until your drawing is complete, since often your drawing is double-buffered (i.e. the Swing engine lets you paint first on an image, and draws then the complete image to the screen).
Also, you are not converting between device (screen) and user (component) coordinates for the lookup.
You wrote:
The Robot class' getPixelColor is the only way I found to get the color of a given pixel (as far as I know another would be getRGB, but that only works on Image objects).
So, why don't you use an Image object? Fill your shape while drawing on the Image, and then draw the whole image at once to the screen.
And your method can be made much more readable if you transfer the "is already painted" test inside the recursive call:
private void color(int x, int y) {
// getPixel invokes something in the image - or replace it here.
Color org = getPixel(x,y);
if (org.equals(Color.BLACK)) {
// reached the border
return;
}
if (org.equals(Color.RED)) {
// already painted before
return;
}
g2d.draw(new Line2D.Double(x, y, x, y));
color(x-1, y);
color(x+1, y);
color(x, y-1);
color(x, y-1);
}