Java Help
Comprox
*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
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-->
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
I could so do it in C++/SDL though... with my eyes closed...
If your using swing components you might be forgetting to tell it to actually display [setvisible(true);].
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.
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.
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.
[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. ;)
<!--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?
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.