I took a course in C and now im into java. I never created anywith graphics in C. I need to finish a program in C that makes a red ball appear on a blue background and then move down and up the screen.

Any information of how to at least draw an oval or make it move would help...

I know that I need to make the call appear in the center then paint over it and make it appear slightly lower and loop the process, but I do not know any of the code...

This is what I have so far:

import java.awt.*;
import java.applet.Applet;
 
public class BouncingBall extends Applet {
    
    //Dimensions of Applet:
    public final int width = 500;
    public final int height = 500;
    //Size and speed of ball:
    public final int diameter = 20;
    public final int speed = 5;
    //Milliseconds to pause between frames:
    public final int pause = 20;
    
    public void paint (Graphics page) {
        setBackground(Color.blue);
    
        
        //Loop forever;
        while (true) {
            
            //Pause between frames:
            try { Thread.sleep(pause) ; } catch (Exception e) { };
        }
    }
}

Thanks

Recommended Answers

All 5 Replies

You'd have to use repaint() I think, and keep re-drawing the oval just 1 pixel lower. I think theres a method called drawOval or something but dont remember what class it's in

drawOval(int x, int y, int width, int height)

Just use threads to increment the x and y value position at certain intervals.

I found the method

in java.awt.Graphics


drawOval(int x, int y, int width, int height)
Draws the outline of an oval.


while(true){
drawOval(50,y,50,50);
repaint();
y++;
}

if you want it to bounce, figure out the size of the screen you are on, find the lowest pixel, and if y = that value start using y-- instead. 2 if statements would work for it

Thanks, but my IDE NetBeans is giving me an error regarding drawOval here is the code I have now...I forgot to put that it has to start in the center of the page of 500 by 500 pixels and it should bounce back up and then bounce back down forever...I have been trying to get this right all day...

//This applet draws an animated bouncing ball with uniform speed.
//The ball starts at the center of the graphics page and bounces off
//the top and bottom

import java.awt.*;
import java.applet.Applet;
        
public class BouncingBall extends Applet {
    
    //Dimensions of Applet:
    public final int width = 500;
    public final int height = 500;
    //Size and speed of ball:
    public final int diameter = 20;
    public final int speed = 5;
    //Milliseconds to pause between frames:
    public final int pause = 20;
    
    public void paint (Graphics page) {
        setBackground(Color.blue);
        page.drawOval(250, int y, 20, 20);
        page.setColor(Color.red);
        //Loop forever;
        while (true) {
            drawOval(50,y,50,50);
            repaint();
            y++;
            //Pause between frames:
            try { Thread.sleep(pause) ; } catch (Exception e) { };
        }
    }
}

//End-of-File

Thanks again

Hi, if your getting an error with drawOval then I would try drawRect();
In my opinion the while(true) and repaint(); should be in the run(); method since you should create a Thread and implement the Runnable interface.
When you call repaint in Java it automatically calls another method called update(); You should implement your own update method in your code like

public void update()
{
paint(page);
}

To reduce flicker. I hope this helps you some. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.