RR_1990
RR_1990

Reputation: 193

Let an image fly in a circle in Java

I need to let an image fly in a cirle, i'm now only stuck on one part. For calculating the points it needs to go im using pythagoras to calculate the height (point B).

Now when using the sqrt function I the the error that I can't convert a double to an int. Here's my code :

package vogel;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class vogel extends Component {
    private int x;
    private int r;
    private int b;


    BufferedImage img;

    public vogel() {
       try {
           img = ImageIO.read(new File("F:/JAVA/workspace/School/src/vogel/vogel.png"));
       } catch (IOException e) {
       } 
       r = 6;
    }


    @Override
    public void paint(Graphics g) {
        for(int i = -r; i <= r; i++) {
            x = i;      

            b = Math.sqrt(r^2 - x^2);
            g.drawImage(img, x, b, this);
        }        
    }


    public static void main(String[] args) { 
        JFrame f = new JFrame("Boot");   
        f.setSize(1000,1000);
        f.add(new vogel());        
        f.setVisible(true);

        for (int number = 1; number <= 1500000; number++) {
            f.repaint();

            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {}
        }
    }
}

Hope one of you guys can help me out

Upvotes: 0

Views: 1043

Answers (5)

Bob
Bob

Reputation: 918

Pre-calculating the path co-ordinates would speed up the redraw loops, the quickest way to do get every pixel co-ordinate is with the Bresenham method (ref. Hachi), here is the Java code

private void drawCircle(final int centerX, final int centerY, final int radius) {
    int d = 3 - (2 * radius);
    int x = 0;
    int y = radius;
    Color circleColor = Color.white;

    do {
        image.setPixel(centerX + x, centerY + y, circleColor);
        image.setPixel(centerX + x, centerY - y, circleColor);
        image.setPixel(centerX - x, centerY + y, circleColor);
        image.setPixel(centerX - x, centerY - y, circleColor);
        image.setPixel(centerX + y, centerY + x, circleColor);
        image.setPixel(centerX + y, centerY - x, circleColor);
        image.setPixel(centerX - y, centerY + x, circleColor);
        image.setPixel(centerX - y, centerY - x, circleColor);
        if (d < 0) {
            d = d + (4 * x) + 6;
        } else {
            d = d + 4 * (x - y) + 10;
            y--;
        }
        x++;
    } while (x <= y);
}

You will need to adjust slightly for your own implementation as this example uses the data storage type defined by Rosetta. http://rosettacode.org/wiki/Basic_bitmap_storage#Java

Note: because it generates a 1/8 arc and mirrors it, it won't create the co-ordinates in the correct order to move your image - you will need to load them into an array and sort them.

The complete class can be found at Rosetta here; http://rosettacode.org/wiki/Bitmap/Midpoint_circle_algorithm#Java

more information about Bresehnam's equations can be found here http://free.pages.at/easyfilter/bresenham.html

Upvotes: 0

Michael Berry
Michael Berry

Reputation: 72294

This line:

 b = Math.sqrt(r^2 - x^2);

...Isn't doing what you think in a number of ways. To start with ^ means XOR, it's not an exponent operator - and it returns a double where as b is an int.

Dealing with the power problem, we can use Math.pow instead (which actually gives you a power) to get:

 b = Math.sqrt(Math.pow(r, 2), Math.pow(x, 2));

Of course, I'm assuming here you did mean power and didn't mean to XOR the two numbers together instead!

You could just cast the result to an int:

 b = (int)Math.sqrt(Math.pow(r, 2), Math.pow(x, 2));

But you probably want to change b so it's a double and you can keep the added accuracy.

Upvotes: 0

Hachi
Hachi

Reputation: 3289

convert it by casting

b = (int)Math.sqrt(..);

although using the algorithm of Bresenham is more efficient than calculating over roots

Upvotes: 1

Andrew Thompson
Andrew Thompson

Reputation: 168825

Cast the value. E.G.

b = (int)Math.sqrt(r^2 - x^2);

Upvotes: 3

Prince John Wesley
Prince John Wesley

Reputation: 63698

b = (int)Math.sqrt(r^2 - x^2); 

Upvotes: 0

Related Questions