How to repaint a specific area (e.g. circle) in java? - java

I am trying to make a simple game where you basically have to hit the circles to get a point. But i faced a tiny problem which i couldn't really solve on my own so here is my question how do I repaint a round surface.I used the repaint(Rectangle r) method but it doesn't workout.
public void objectHit(MouseEvent e) {
int distance = 0, deltaX = 0, deltaY = 0, RadiusSqaured = 0;
for (int i = 0; i < obj.length; i++) {
deltaX = e.getX() - obj[i].getPoint().x;
deltaY = e.getY() - obj[i].getPoint().y;
distance = deltaX * deltaX + deltaY * deltaY;
RadiusSqaured = obj[i].getRadius() * obj[i].getRadius();
if (distance <= RadiusSqaured) {
repaint(obj[i].repaintRect());
x = ThreadLocalRandom.current().nextInt(50 + radius / 2, 850 - radius / 2);
y = ThreadLocalRandom.current().nextInt(60 + radius / 2, 750 - radius / 2);
repaint(obj[i].repaintRect());
}
}
}

In JComponent, there is a method for repainting based on a box area. Does this satisfy your requirements?
https://docs.oracle.com/javase/8/docs/api/javax/swing/JComponent.html#repaint-long-int-int-int-int-

Related

Glitching when running program in parallel

I have a problem in regards to parallelism in javafx. More specifically, dividing the canvas of my program to run with multiple threads. When compiled, the program glitches out and doesn't work as supposed to. The code in the thread class is supposed to print out the Mandelbrot set, and the code in the application class is dividing the canvas into different parts that each should run in parallel. Here is the code:
public void MandelbrotSet(int n) {
int widthh = (int)canvas.getWidth();
int portion = widthh / n;
for (int i = 0; i < n; i++) {
int startX = i * portion;
int endX = startX + portion;
myMandelbrotParallelTEST2 thread = new myMandelbrotParallelTEST2(startX, endX, image, width, height, maximumIterations, canvas, zoom, xPos, yPos, hue, brightness, saturation, R, G, B);
thread.start();
//would add thread.join() here with try and catch
}
canvas.getGraphicsContext2D().drawImage(image, 0, 0);
}
this is how the code in the Thread class looks:
#Override
public void run(){
double centerY = canvas.getWidth() / 2.0;
double centerX = canvas.getHeight() / 2.0;
for (int x = start; x < end; x++) {
for (int y = 0; y < canvas.getHeight(); y++) {
double cr = xPos / width + (x - centerY) / zoom;
double ci = yPos / height + (y - centerX) / zoom; //getting position of the points on the canvas
int iterationsOfZ = 0;
double zr = 0.0;
double zi = 0.0;
while (iterationsOfZ < maximumIterations && (zr * zr) + (zi * zi) < 4) {
double oldZr = zr;
zr = (zr * zr) - (zi * zi) + cr;
zi = 2 * (oldZr * zi) + ci;
iterationsOfZ++;
}
int iterations = iterationsOfZ;
if (iterations == maximumIterations) { //inside the set
imagee.getPixelWriter().setColor(x, y, Color.rgb(R, G, B));
} else if (brightness == 0.9) { //white background
imagee.getPixelWriter().setColor(x, y, Color.hsb(hue, iterations / maximumIterations, brightness));
} else if (hue == 300) { //colorful background
imagee.getPixelWriter().setColor(x, y, Color.hsb(hue * iterations / maximumIterations, saturation, brightness));
} else if (hue == 0 && saturation == 0 && brightness == 1) {
imagee.getPixelWriter().setColor(x, y, Color.hsb(hue, saturation, brightness));
} else { //black background
imagee.getPixelWriter().setColor(x, y, Color.hsb(hue, saturation, iterations / brightness));
}
}
}
//canvas.getGraphicsContext2D().drawImage(imagee, 0, 0);
}
[this is how the program looks, and while panning around it glitches even more, but the execution time is really fast, which means that it is ran concurrently.]enter code here(https://i.stack.imgur.com/IZUfY.png)
I found that adding thread.join() fixes the problem, but then the program is ran on a single thread. Could anyone help me out resolve this issue?
What you are trying to do cannot be done with a Canvas because of the fx application thread restiction which has already been mentioned.
But it can be done via the WritableImage of JavaFX 13+ with support for Buffers. Writing into the buffer is not restricted in the above way and can thus be done in parallel with high performance.
I have compiled some examples (not any parallel ones) of how to use WritableImages with Buffers here: https://github.com/mipastgt/JFXToolsAndDemos

Montecarlo method and JFrame (math and java problem)

I made a little program to approximate Pi and I would like to represent it. I started but I thought it would look better if the points outside the circle were red and the ones inside were green. I can't do it and I don't understand why the problem is there. I don't know if it is in pure math or in dev.
for(int i = 0; i<1000; i++) {
double x = Math.random() * (500-250) + 250;
double y = Math.random() * 250;
double applyformula = (x - 250)*(x - 250) + (y - 250) * (y - 250);
if(applyformula == y || (y*y) - 500 * y < applyformula ) {
g.fillOval((int) x,(int) y, 5, 5);
g.setColor(Color.GREEN);
} else {
g.fillOval((int) x,(int) y, 5, 5);
g.setColor(Color.RED);
}
}
If someone could help me, that will be perfect.
It could be done in a simpler way.
for (int i = 0; i < 1000; i++) {
double x = Math.random();
double y = Math.random();
double applyformula = (x * x) + (y * y);
if (applyformula <= 1) {
g.setColor(Color.GREEN);
} else {
g.setColor(Color.RED);
}
// Set the actual coordinates in a 250 pixels wide square.
x *= 250;
y *= 250;
g.fillOval((int) x, (int) y, 5, 5);
}

Rotate player towards mouse

I want my player rotate towards mouse. Here's the code calculating the angle:
float angle = (float) Math.atan2(MouseInput.getMousePos().y - transform.position.y + transform.size.y / 2,
MouseInput.getMousePos().x - transform.position.x + transform.size.x / 2);
angle = (float) (angle * (180 / Math.PI));
if (angle < 0) {
angle = 360 + angle;
}
transform.rotation = 180 + angle;
And getMousePos() method (it just returns mouse pos relative to window):
public static Vector2 getMousePos() {
Point p = MouseInfo.getPointerInfo().getLocation();
return new Vector2(p.x - Game.w.getAccessToWindow(Acces.WINDOW_JFRAME_ACCES).getLocation().x,
p.y - Game.w.getAccessToWindow(Acces.WINDOW_JFRAME_ACCES).getLocation().y);
}
Can you tell me what's wrong with this code? Player isn't rotating properly.
I tried following this article: https://gamefromscratch.com/gamedev-math-recipes-rotating-to-face-a-point/
Update:
I found this post: Java 2d rotation in direction mouse point
Now I've updated my code to this:
int centerX = (int) (transform.size.x / 2);
int centerY = (int) (transform.size.x / 2);
int mouseX = (int) MouseInput.getMousePos().x;
int mouseY = (int) MouseInput.getMousePos().y;
double angle = Math.atan2(centerY - mouseY, centerX - mouseX) - Math.PI / 2;
transform.rotation = angle;
But still something is off. Try this code for yourself. Maybe I did something wrong somewhere else.

Bounding Joystick to a Circle

Here is a snippet of code that I have. I am trying to bound my object in a circle rather than a square, but there are things wrong with my code that I can't figure out. I have both the rectangle bounds which work and the circle bounds which don't work.
// get the pos
_touchingPoint.x = (int)event.getX();
_touchingPoint.y = (int)event.getY();
angle = Math.atan2(_touchingPoint.y , _touchingPoint.x);
// bound to a box
if( _touchingPoint.x < 75){
_touchingPoint.x = 75;
}
if ( _touchingPoint.x > 225){
_touchingPoint.x = 225;
}
if (_touchingPoint.y < 300){
_touchingPoint.y = 300;
}
if ( _touchingPoint.y > 450 ){
_touchingPoint.y = 450;
}
//bound to a circle
if( _touchingPoint.x < 75 * Math.cos(angle))
{
_touchingPoint.x = (int) (75 * Math.cos(angle));
}
if ( _touchingPoint.x > 225 * Math.cos(angle))
{
_touchingPoint.x = (int) (225 * Math.cos(angle));
}
if ( _touchingPoint.y < 300 * Math.sin(angle))
{
_touchingPoint.y = (int) (300 * Math.sin(angle));
}
if ( _touchingPoint.y > 450 * Math.sin(angle))
{
_touchingPoint.y = (int) (450 * Math.sin(angle));
}
_touchingPoint.x and _touchingPoint.y represent the object (Joystick) that I am trying to constrain. I figured I could try simple trig to constrain the Joystick to a circle, but I guess I was wrong.
What would be the best way to set up a circle constraint?
You seem to be trying to constrain the position to a circular disk, not a circle which is its boundary. I'd accomplish this by rescaling the offset vector from the center:
double dx = event.getX() - 150;
double dy = event.getY() - 375;
double len = Math.hypot(dx, dy);
if (len > 75) {
dx = dx*75/len;
dy = dy*75/len;
}
_touchingPoint.x = (int)dx + 150;
_touchingPoint.y = (int)dy + 375;
The code appears to have calculated the angle of your point from (0,0) then bounding it based on comparing its x with some function of that angle. For example, 75 * Math.cos(angle) varies between -75 and +75, whereas you want to be comparing the point with centre of a circle.
Set up variables for the centre of the circle (cx and cy) and the difference in the x and y ordinates (dx and dy).
You then want to test whether the distance from the centre exceeds your desired radius (usually this is done with the square of the distance as it avoids a sqrt operation - dx*dx+dy*dy>r*r).
If the distance is greater than the radius, project back to the circle, either using trig or by scaling the difference :
double dx = event.getX() - cx;
double dy = event.getY() - cy;
double d2 = dx*dx+dy*dy;
if(d2>r*r) {
double scale = r / Math.sqrt(d2);
dx*=scale;
dy*=scale;
}
then ( dx + cx, dy + cy ) will lie within the circle defined by cx,cy and r.
Unless I'm missing something, it would be along these lines:
===EDIT=== ONE THING I was missing, as pointed out by Pete, was how angle should be computed:
angle = atan2(y - 375, x - 150);
=== end of edit ===
a = _touchingPoint.x;
b = _touchingPoint.y;
if((a-150)*(a-150) + (b-375)*(b-375) > 75*75){
_touchingPoint.x = 150 + 75*cos(angle);
_touchingPoint.y = 375 + 75*sin(angle);
}

Collision detection for circles

I have a simple Java applet that has two user-controlled balls, drawn using java.awt. I need a way to detect a collision with between them. I have an algorithm for detecting collision with the walls:
if (xPosition > (300 - radius)){
xSpeed = -xSpeed;
}
else if (xPosition < radius){
xSpeed = -xSpeed;
}
else if (yPosition > (300 - radius)) {
ySpeed = -ySpeed;
}
else if (yPosition < radius){
ySpeed = -ySpeed;
}
xPosition += xSpeed;
yPosition += ySpeed;
and for the second ball:
if (xPosition2 > (300 - radius)){
xSpeed2 = -xSpeed2;
}
else if (xPosition2 < radius){
xSpeed2 = -xSpeed2;
}
else if (yPosition2 > (300 - radius)) {
ySpeed2 = -ySpeed2;
}
else if (yPosition2 < radius){
ySpeed2 = -ySpeed2;
}
xPosition2 += xSpeed2;
yPosition2 += ySpeed2;
The applet is 300 pixels by 300 pixels.
radius stores the radius of the circles.
xPosition and xPosition2 store the x coordinates for the two balls.
yPosition and yPosition store the y coordinates for the two balls,
xSpeed and xSpeed2 store the x velocities for the two balls.
ySpeed and ySpeed2 store the y velocities for the two balls.
Use http://java.sun.com/j2se/1.5.0/docs/api/java/awt/geom/Point2D.html, there's a distance method there, if it's less than the radius they're colliding.
EDIT:
Err, less than the radius * 2 , sorry
There's Point2D in Java or you can do it yourself, it is trivially easy for circle/circle collisions or sphere/sphere collisions.
int distXX = (xPosition1 - xPosition2) * (xPosition1 - xPosition2);
int distYY = (yPosition1 - yPosition2) * (yPosition1 - yPosition2);
if ( radius*radius > distXX * distYY ) {
... // There's a collision
}
public boolean colliding(Ball anotherBall) {
double xDelta = (this.x + this.ballSize/2 + this.dx) - (anotherBall.x + anotherBall.ballSize/2 + anotherBall.dx);
double YDelta = (this.y + this.ballSize/2 + this.dy) - (anotherBall.y + anotherBall.ballSize/2 + anotherBall.dy);
double distance = Math.sqrt(Math.pow(xDelta, 2) + Math.pow(YDelta, 2));
return (distance <= this.ballSize/2 + anotherBall.ballSize/2);
}
This Link is pretty useful!
Circle-Circle Collisions
It's very detailed and didatic
At the bottom of that page there are another links, to even more detailed stuff!
I used the Distance Between Centers method ---
Circles
By measuring the distance between each center you can say if they are colliding.
The distance should never be more then the sum of the 2 radius.
Here's what I did:
private boolean checkDrawContains(ShapeDrawable newHole)
{
long newCenterX = newHole.getBounds().left + (newHole.getBounds().width()/2); //Get the center of my shapes
long newCenterY = newHole.getBounds().top + (newHole.getBounds().height()/2);
for(ShapeDrawable hole: mHoles) // I was storing the circles in an ArrayList
{
long centerX = hole.getBounds().left + (hole.getBounds().width()/2); //Get the center of my shapes
long centerY = hole.getBounds().top + (hole.getBounds().height()/2);
long x = centerX - newCenterX;
long y = centerY - newCenterY;
long aux = (long) ((Math.pow(Math.abs(x),2)) + (Math.pow(Math.abs(y),2))); //Pythagoras the hard way :P
long distance = (long) Math.sqrt(aux);
long sRads = (newHole.getBounds().width()/2) + (hole.getBounds().width()/2);
if(distance <= sRads ) {
return true; //Is Colliding!
}
}
return false; // Is not Colliding!
}

Categories