0

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

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
RR_1990
  • 193
  • 1
  • 3
  • 15

5 Answers5

3

Cast the value. E.G.

b = (int)Math.sqrt(r^2 - x^2);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    I had a closer look at that code and it has lots of problems yet to be sorted out. 1) Don't swallow exceptions. 2) Don't mix Swing & AWT. 3) When doing custom painting in Swing, override `paintComponent()` rather than `paint()`. 4) Don't call `Thread.sleep()` in the EDT. 5) The loop in the current paint method will most likely not work as you expect. (Though I'm not sure how you expect it to work). 6) The formula shown moves the image in a straight line from left to right.. – Andrew Thompson Oct 13 '11 at 10:01
  • 1
    Also, using pythagoras won't move the object smoothly in a circle. It will speed up and slow down at opposite sides. If you want it to move at a fixed rate, you need to give it a heading and velocity and work out the next co-ordinate point using Trigonometry instead. – Bob Oct 13 '11 at 10:43
  • 2
    There's an example using @Bob's approach [here](http://stackoverflow.com/questions/3256269/jtextfields-on-top-of-active-drawing-on-jpanel-threading-problems/3256941#3256941). – trashgod Oct 13 '11 at 14:09
1

convert it by casting

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

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

Hachi
  • 3,237
  • 1
  • 21
  • 29
0
b = (int)Math.sqrt(r^2 - x^2); 
Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
0

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.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
0

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

Bob
  • 898
  • 8
  • 11