Java Help

ComproxComprox *chortle*Canada Join Date: 2002-01-23 Member: 7Members, Super Administrators, Forum Admins, NS1 Playtester, NS2 Developer, Constellation, NS2 Playtester, Reinforced - Shadow, WC 2013 - Silver, Subnautica Developer, Subnautica Playtester, Pistachionauts
edited October 2004 in Off-Topic
Well, I'm totally stuck. I found out at 11pm today this program is due at 8am the next morning. My teacher decided to assign an extra lab this week, and not tell us. Thankfully someone noticed on the site the due date for it. What I need to do is get a nice little ball to bounce around the screen, hitting all 4 sides, in a pretty basic way.

I don't need any sin, cos or angle stuff. All I want to do is get it going at an angle, like x+5 and y-3, and when it reaches a wall, it just inverts x or y from positive to negative or vise versa so it continues along its merry way. And I have a few requirements:

<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->Create handle for Thread:

  Thread timer;

Tell JAVA want to have a Thread:
Runnable interface.

class MyApplet extends JApplet implements Runnable {
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->public void start () {
if (timer == null) {
timer = new Thread (this);
timer.start();
running = true;
}
}
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->public void stop () {
  running = false;
  }
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->public void run () {
do {
repaint(); // redraws the screen
try {
timer.sleep (100);
} catch (InterruptedException e) {running = false;}

// Sprite movement code goes here

} while (running);

timer = null; // Destroy the timer Thread
}
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

Now, if I wasn't working on no sleep, and the fact that I have a good 2 hours more of homework to go before I can even think of sleeping, I might be able to do this, but I've been banging away at it for over 3 hours, and I'm getting absolutely no where. I've tried google, but 90% of the stuff is too complicated for my needs to learn from, and the only version I got to even compile, didn't display anything. I think I have the movement code done basically right, I just don't have it drawing anything, anywhere. The whole getting to interface with paint() is totally eluding me right now. Any help would be greatly appreciated, my head hurts <!--emo&:(--><img src='http://www.unknownworlds.com/forums/html//emoticons/sad-fix.gif' border='0' style='vertical-align:middle' alt='sad-fix.gif' /><!--endemo--> Here is what my animation code in run() looks like so far:

<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->do {
           repaint(); // redraws the screen
           try {
               timer.sleep (100);
           } catch (InterruptedException e) {stop();}
           if (!xReverse)
           {
               if ((++xCoord + ballWidth) >= (dimensions.width - appletInsets.left))
                   xReverse = true;
           }
           else
           {
               if (--xCoord <= appletInsets.right)
                   xReverse = false;
           }

           if (!yReverse)
           {
               if ((++yCoord + ballHeight) >= (dimensions.height - appletInsets.bottom))
                   yReverse = true;
           }
           else
           {
               if (--yCoord <= appletInsets.top)
                   yReverse = false;
           }
       } while (timer!=null);<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

Comments

  • SkulkBaitSkulkBait Join Date: 2003-02-11 Member: 13423Members
    edited October 2004
    Man, if it weren't so late... and if it had been less than 2 years ago that I last did any java... and I could stand to read One True Brace style... then I might be able to help. But my brain has already gone to sleep.

    I could so do it in C++/SDL though... with my eyes closed...
  • CronosCronos Join Date: 2002-10-18 Member: 1542Members
    Whats your problem? Can I look at your code in full?

    If your using swing components you might be forgetting to tell it to actually display [setvisible(true);].
  • ComproxComprox *chortle* Canada Join Date: 2002-01-23 Member: 7Members, Super Administrators, Forum Admins, NS1 Playtester, NS2 Developer, Constellation, NS2 Playtester, Reinforced - Shadow, WC 2013 - Silver, Subnautica Developer, Subnautica Playtester, Pistachionauts
    It's basic, Im totally missing any form of drawOval right now, since everywhich way I put it in has not worked. Here's a zip of the work.
  • CronosCronos Join Date: 2002-10-18 Member: 1542Members
    I thinky my school java page had a few code samples on how to get Draw Oval working. I may be able to find you some sample code but I'll need to do some digging.
  • CronosCronos Join Date: 2002-10-18 Member: 1542Members
    void drawOval(int x, int y, int width, int height)

    You'd want fillOval instead I'd wager since it fills the oval with the current foreground colour.

    The x and y integers specify where on the applet window the oval will be drawn, whilst the width and height components specify how wide and how high the oval itself will be.

    I need to take a little more time looking at your code to see how it may be implemented within the program though.
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    Hehehe... I've <i>just</i> had to hand in two assignments, one of which was a ball bouncing in a rotating box, the other of which involved threading.

    I can't help, though, since my graphics assignment was in C++ using Glut. I haven't done any graphics work (or much other work) with Java.
  • CronosCronos Join Date: 2002-10-18 Member: 1542Members
    Something I've noticed, I've got the MainBounce java and class file from the zip but I have a class file called Bouncer that I dont seem to have. Was there anything important in that file that might help?

    Also, it's important to remeber that swing applets usually are invisible unless you set their visibility parameter to true. Check that you've done this.
  • ComproxComprox *chortle* Canada Join Date: 2002-01-23 Member: 7Members, Super Administrators, Forum Admins, NS1 Playtester, NS2 Developer, Constellation, NS2 Playtester, Reinforced - Shadow, WC 2013 - Silver, Subnautica Developer, Subnautica Playtester, Pistachionauts
    Oooh, Bouncer was me putting stuff in a seperate class, I deleted it, but it seems it left the other files associated with it. It's useless now.
  • InjuryInjury Mahou Shoujo Join Date: 2002-11-10 Member: 7992Banned
    <!--QuoteBegin--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->[00:05] * Comprox is now known as CompJavaSucks
    [00:09] * joev is now known as CompSucksJava
    [00:09] * CompSucksJava is now known as joev<!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->

    I don't think anything else has to be said. ;)
  • CronosCronos Join Date: 2002-10-18 Member: 1542Members
    I modified the html file and managed to get the applet showing. The html file now reads as

    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->


    <html>
    <!-- This file automatically generated by BlueJ Java Development  -->
    <!-- Environment.  It is regenerated automatically each time the  -->
    <!-- applet is run.  Any manual changes made to file will be lost -->
    <!-- when the applet is next run inside BlueJ.  Save into a       -->
    <!-- directory outside of the package directory if you want to    -->
    <!-- preserve this file. -->
       <head>
           <title>MainBounce Applet</title>
       </head>
       <body>
           <h1>MainBounce Applet</h1>
           <hr>
           <applet code="MainBounce.class" width=500 height=500>      
           </applet>
           <hr>
       </body>
    </html>

    <!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

    It says that the applet has crashed though.

    I noticed you havent defined the yCoord integer on line 119. That may be causing problems.

    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->int xCoord = 20, yCoord, ballWidth = 30, ballHeight = 30;<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

    On second thoughts though, after defining yCoord in the program it did nothing important.

    One other thing, your getSize() and getInsets() methods are unfamiliar to me and only appear once in the program. Could they be the root of the matter?
  • ScytheScythe Join Date: 2002-01-25 Member: 46NS1 Playtester, Forum Moderators, Constellation, Reinforced - Silver
    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->/*
    Lab assignment 3: BounceCircle

    Description: This program makes a red circle bounce back and forth accross the screen.
    */

    import genesis.*; // For general commands.
    import java.awt.*; // For "Color.*" statement.

    public class BounceCircle { // Declares the BounceCircle class.

     public static void main (String [ ] args) { // Starts the main function.
       CircleFigure.create(); // Creating the circle
       CircleFigure.setRadius(25); // Setting the circle's radius
       CircleFigure.moveTo(200,130); // Centering the circle in the window
       CircleFigure.setColour(Color.red); // Sets the colour of the square to red.
       for (;;) { // Starting an infinite loop
        for (int i = CircleFigure.getXCentre(); i >=25; i--) { //Move the circle left untill it hits the left side
         CircleFigure.moveLeft(1);
         Delay.milliseconds(20);
        } // Close move left for loop.
        for (int i = 25; i <=367; i++) { // Move the circle right untill it hits the right side.
         CircleFigure.moveRight(1);
         Delay.milliseconds(20);
       } // Close move right for loop.
       } // End infinite loop.
     } // Close main function
    } // Close class BounceCircle<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

    This might help, I dunno.
  • ComproxComprox *chortle* Canada Join Date: 2002-01-23 Member: 7Members, Super Administrators, Forum Admins, NS1 Playtester, NS2 Developer, Constellation, NS2 Playtester, Reinforced - Shadow, WC 2013 - Silver, Subnautica Developer, Subnautica Playtester, Pistachionauts
    Getsize and GetInsets were 2 functions I found on... ugh, I don't even remember what size. GetSize returns the dimenions of the applet window, insets does... um, I totally forget now.. wow. They are used later on down for determining if the ball is at the edge of the screen, allowing for scability of the window.
  • ComproxComprox *chortle* Canada Join Date: 2002-01-23 Member: 7Members, Super Administrators, Forum Admins, NS1 Playtester, NS2 Developer, Constellation, NS2 Playtester, Reinforced - Shadow, WC 2013 - Silver, Subnautica Developer, Subnautica Playtester, Pistachionauts
    The Answer! Well, it works at least. God that was so basic! Huge thanks to cronos for slogging it out with me. Im heading to bed here soon (finally).
  • CronosCronos Join Date: 2002-10-18 Member: 1542Members
    No worries, always glad to help, but most of that stuff was beyond me. Just goes to show how advanced Australian computer science courses are D:
Sign In or Register to comment.