Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Java Doubles for drawing and conversion help
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Shadow456732




PostPosted: Thu Feb 14, 2013 11:31 am   Post subject: Java Doubles for drawing and conversion help

I'm trying to create a program that uses a rate to move a ball across the screen rather than with delay's. The problem im having is that it seems that c.fillOval does not support rounded double values. And i need to somehow convert the double into an integer for use with graphics functions. Here's my code.

Java:
// The "Bouncyball" class.
import java.awt.*;
import hsa.Console;

public class Bouncyball
{
    static Console c;           // The output console

    public static void main (String[] args)
    {
        c = new Console ();
        int width = c.getWidth ();
        int height = c.getHeight ();
        c.println ("What delay do you want to use?");
        int delay = c.readInt ();
        double x = Math.round (width / 2);
        double y = Math.round (height / 2);
        double[] rate = new double [2];
        rate [0] = 0.25;
        rate [1] = 0.25;
        while (true)
        {
            x = x + rate [0];
            y = y + rate [1];
            c.setColor (Color.green);
            c.fillOval (Math.round (x), Math.round (y), 30, 30);
            c.clear ();
        }
    }
}


Mod Edit:

Please wrap you code in either of the following in order to preserve whitespace (indentation) and to highlight the syntax.
code:

[syntax="java"] ... code ... [/syntax]

[code] ... code ... [/code ]
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Thu Feb 14, 2013 11:45 am   Post subject: RE:Java Doubles for drawing and conversion help

You are passing doubles to Math.round. According to the documentation, there are 2 Math.round functions: one takes a float, the other a double. Math.round(float) returns an int, while Math.round(double) returns a long. Since Console.fillOval takes integers, Math.round(double) will not work.

Either cast it to an int by type c.fillOval ((int)Math.round(x), (int)Math.round(y), 30,30) or by changing your doubles to floats.
Shadow456732




PostPosted: Fri Feb 15, 2013 9:45 am   Post subject: RE:Java Doubles for drawing and conversion help

Thanks! Worked perfectly! I turned the variable types to float's. I had originally used doubles since i made a mistake in the syntax and called them Float not float. I switched to doubles and i didn't make the same mistake. Also, coming from turing, i want to know if there is a similar view.set("offscreenonly")/view.update() kind of thing but for java? Thanks a lot for the help!


EDIT: Is there also a reasonably short delay function in java? I would use the help thing in ready to program, but it's a bit more complicated than the one in turing lol. Thanks again Very Happy
evildaddy911




PostPosted: Fri Feb 15, 2013 3:40 pm   Post subject: RE:Java Doubles for drawing and conversion help

try {
Thread.sleep(int milliseconds); }
catch (InterruptedException ex) { }

you can replace Thread with your own Thread object if you are multithreading, or use Thread.currentThread()
Insectoid




PostPosted: Fri Feb 15, 2013 6:16 pm   Post subject: RE:Java Doubles for drawing and conversion help

Quote:
i want to know if there is a similar view.set("offscreenonly")/view.update() kind of thing


Yes and no. No, there is no simple command to do it. Yes, it's possible. You need to set up a 2nd buffer object to draw to. See here.

Quote:
Is there also a reasonably short delay function in java?


No, and you don't need one. I don't know how you're supposed to regulate framerate, but I did it by putting Graphics.repaint() on a timer and somewhere in my paint function I called all the animation functions.

So, you don't pause the game to regulate the framerate, you just don't update the animations until you need to while letting the rest of the code run. But there's probably a better way to do this. I've only written one Java game, and it's a colossal mess that I don't know how it works.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: