Is there any easy way to draw a circle onto a JPanel? - java

I'm having trouble using the drawOval(x,y,width,height) method, which assumes that the x and y values represent the coordinates of the "upper left corner of the oval to be drawn" (javadoc)
I want the x and y values to represent the center-point of a circle. How do I do this? Thanks

A simple solution, if you have the width/height declared in advance, would be to use the drawOval method as follows:
drawOval( x - (width/2), y - (height/2), width, height);
This will ensure that (x, y) is at the center of the oval.
Why?
Let's say (x, y) is (10, 10) and you want to draw an oval of (height, width) = (10, 10).
drawOval(x, y, height width);
would then draw the top-right of the oval at (10, 10), and the bottom-left would be at (10 + 10, 10 + 10) = (20, 20).
On the other hand, if you use
drawOval( x - (width/2), y - (height/2), height, width);
the top-right of the oval would be drawn at ( 10 - (10/2), 10 - (10/2) ) = (5, 5) and the bottom would be drawn at (5 + 10, 5 + 10) = (15, 15). The center would then be (10, 10) :)

Related

Mouse coordinate issues in Slick2D

For development purposes of my game, I want to display the coordinates of the mouse in my window. I am having a couple of issues with this. First, the axis appears to start in the bottom left. I would like it to start in the upper left because that is what I am used to. I am currently compensating for this by subtracting the Y coordinate from the height of the window. I would prefer to not have to do this. The other issue is that my Y coordinate appears to be offset by 141 pixels and the X coordinate always stays at -1. How can I fix these issues?
int posX = Mouse.getX();
int posY = HEIGHT - Mouse.getY();
g.setColor(Color.black);
g.fillRect(0, HEIGHT - 50, 125, 30);
g.setColor(Color.white);
g.drawString(posX + "," + posY, 10, HEIGHT - 45);

Java / box2DLights - Wrong light position

I'm using Libgdx for a project and more precisely Box2DLights.
My problem is the following one : When I want to put a new "PointLight" it's always on the center of the screen. And if I change the coordinates, it doesn't work.
Inside my "show()" method :
Box2D.init();
world = new World(new Vector2(0, 0), true);
rh = new RayHandler(world);
rh.setAmbientLight(1.2f, 0.2f, 0.2f, 0.1f);
pl = new PointLight(rh, 100, new Color(1,1,1,1),(float) 0.5,0,0);
Inside my "render()" method :
Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
world.step(delta, 8, 3);
renderer.begin(ShapeType.Filled);
for (SolarSystem ss : solarSystemList)
{
if(ss.getColor() <= 15) colorSS = Color.YELLOW;
else if(ss.getColor() > 15 && ss.getColor() < 31) colorSS = Color.ORANGE;
else if(ss.getColor() > 30 && ss.getColor() < 46) colorSS = Color.RED;
else if(ss.getColor() > 45) colorSS = Color.CYAN;
renderer.setColor(colorSS);
renderer.circle(ss.getMapX(), ss.getMapY(), ss.getSize() - 3);
}
renderer.end();
rh.updateAndRender();
Result :
Now if I try to change coordinates :
pl = new PointLight(rh, 100, new Color(1,1,1,1),(float) 0.5, 50, 50);
... no light anymore
Do you know how it's possible to put the light where I want ?
EDIT : My screen size : width - 860px / height - 645px
if the (1,1) is the top right and the (0,0) is bottom left and the (0.5,0.5) is the middle of the screen, then i propose to do this :
insert the value that you want and divide it by the width and height of of your screen for example
( xPosition/Gdx.graphics.width, yPosition/Gdx.graphics.height )
Update :
sorry i didn't see that (0,0) was the center so i propse to you to use this instead :
width = Gdx.graphics.width;
height = Gdx.graphics.height;
((xPosition - width/2)/ width/2 , (yPosition - height/2)/ height/2)
Update 2 :
i think you are doing little arithmetic mistake assume that your
width = 860 and your height = 645 as you said
this is the equation :
x= ((xPosition - width/2)/ width/2)
y= (yPosition - height/2)/ height/2)
x = (50 - 860/2) / (860/2)
y = (50 - 645/2) / (645/2)
x = (50 - 430) / (430)
y = (50 - 322.5) / (322.5)
x = (50 - 430) / (430) = (-380) / (430)
y = (50 - 322.5) / (322.5) = (-272.5) / (322.5)
x = -0.88
y = -0.84
which is closer to (-1,-1) aka : the left bottom corner
hope it was helpful :)
If you take a distance of 0.5 and your light shines above half of the screen I just assume that a position of 50, 50 will not fit into this screen. Just try to change your position to a smaller value. Maybe your coordinates do not represent pixels but other units as it is recommended for box2d.
Edit: As I don't know your entire libgdx app I just recommend to have a deeper look into Camera, ViewPort and such. For example the box2dlights RayHandler can get your camera via setCombinedMatrix. You may also want to synchronize lights with bodies and the box2d world with your sprites.

how to set x and y coorinates android

i have created a 10x10 grid in the center of the screen in android, now i would like to give each square in the grid a coordinate. for example top left square in the grid would be 0 then 1, 2,3 and so on. But i dont know how to do this. i am trying to do this in a draw class which extends view. my code of what i am trying is below
public int coordinates(int posX, int posY){
int startX = (screenWidth / 2) - (rectSide / 2);
int startY = (screenHeight / 2) - (rectSide / 2);
//for(int i=0; i<=10000; i+=100){
xCoord = (startX + (posX*100));
yCoord = (startY + (posY*100));
}
You know you start at point 0,0 top left. So assuming you have equally spaces squares you can just do the screen height / 10 to get how far apart each square should be in the y direction. And then do the same for the x direction. Say your screen was 1000 pixels tall.
Then your grid at position (0,1) would be at (0,100) pixels. (0,2) would be (0,200) you are just multiplying the y coordinate by the height of each square in the grid.

two points and then finds the smallest circle and the smallest rectangle containing the points

Write a Java program that reads two points and then finds the smallest circle and the smallest rectangle containing the points. Note that a circle is represented by its center and radius, and a rectangle by the two diagonal points – top-left and bottom-right corners. For example, p1 = (0, 0) and p2 = (4, 3) are entered as input, your program will print C = ((2, 1.5), 2.5) and R = ((0, 3), (4, 0)). No if statement is allowed, but you may use built-in methods such as sqrt, pow, abs, max, and min.
Scanner in = new Scanner ( System.in);
double cx, cy, cyx,c;
double p1x,p1y,p2x,p2y;
System.out.print("Enter point 1, x ");//0
p1x=in.nextDouble();
System.out.print("Enter point 1, y ");//0
p1y=in.nextDouble();
System.out.print("Enter point 2, x ");//4
p2x=in.nextDouble();
System.out.print("Enter point 2, y ");//3
p2y=in.nextDouble();
cx= (p2x-p1x)/2;// (2,)
cy=(p2y-p1y)/2;// (,1.5)
cyx= (p2x-p2y)+cy;// ((,),2.5)
System.out.println((cx+","+cy)+","+cyx);
as far as radius, i am unsure. As well as unsure if the code would work, or i am over complicating things, or far off at all.
The smallest circle containing two points will have each point along the circle's circumference, with the center of the circle directly between those two points, meaning the circle's radius is half the distance between the points.
With the simplest definition, we could use ((x2 + x1) / 2, (y2 + y1) / 2) as the centerpoint, and sqrt((x2 - x1)^2 + (y2 - y1)^2) / 2 as the radius. However, if we use java.awt.geom.Ellipse2D to represent the circle, you need to have the top-left corner of the square containing the circle and the diameter of the circle.
The diameter is easy: twice the radius, or sqrt((x2 - x1)^2 + (y2 - y1)^2).
To get the top-left corner of the containing square, subtract the radius from the x- and y-coordinate of the centerpoint:
double diameter = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
Point2D.Double center = new Point2D.Double((x2 + x1) / 2, (y2 + y1) / 2);
Point2D.Double tlCorner = new Point2D.Double(
center.x - diameter / 2,
center.y - diameter / 2
);
Ellipse2D.Double circle = new Ellipse2D.Double(
tlCorner.x,
tlCorner.y,
diameter,
diameter
);
The smallest rectangle containing two points uses those two points as opposite corners. Again, with the simplest definition we could just use the two input points as two corners of a rectangle. However, Java's rectangle classes expect the top-left corner, the width, and the height, not two points.
Rectangle2D.Double rect = new Rectangle2D.Double(
Math.min(x1, x2),
Math.min(y1, y2),
Math.abs(x2 - x1),
Math.abs(y2 - y1)
);

How to draw a circle within a circle?

I'm trying to get a bunch of small circles that have varying shades of green to be drawn within a big circle to get a "bush" look, but I can't figure out how to get all the small circles within the shape of a big circle. I can only figure out how to get it within a rectangle.
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for(int i = 0; i < 1000; i++){
int redV = (int) ((Math.random() * 100) + 27);
g.setColor(new Color(red, red + 31, red - 15));
int x = (int) ((Math.random() * 400) + 150);
int y = (int) ((Math.random() * 500) + 200);
g.fillOval(x, y, 50, 50);
}
}
I guess you have to do some geometry here, and verify whether the x and y coordinates generated randomly are within your circle. As you said, within a rectangle is easy (because you just check that x > left, x+50 < right, y > top, y+50 < bottom), however for a circle you have to use the equation of a circle and check that (x,y) and (x+50,y+50) are within it before actually doing the fillOval().
I think you have a simple way out by using the Java 2D Shape.contains(), which is implemented by Ellipse2D. So essentially you create an instance of Ellipse2D.Double or Ellipse2D.Float for the greater circle, and then just call contains() each time you generate the coordinates to check they are within it before drawing them.
I think you can just change the Color slightly, and increment/decrement x, y, width, and height slightly to get them to be within the older circle. The new oval should be painted over the old one.
Choose the point that should be the center of the big circle, and draw the big circle relative to that (e.g. using java.awt.geom.Ellipse2D).
You can then use the center of the big circle and its radius to position the other smaller circles relative to that also, inside the circumference.

Categories

Resources