Hello, I am a noob in Java, and I am stumped with these 2 exercises. These are probably extremely simple for you all, so any kind of help with writing the program would be well-appreciated.

1. Write a program which simulates inflation of a balloon (2-dimensional circular shape starting from the centre of your output console as a one-dimensional point). The balloon should collapse again to a one-dimensional point after hitting the console's boundary. This process should repeat itself a certain number of times. (Top: you can get a flashing effect by randomly changing the value of colour using a random value generator with values between 0 and 225).

2. Write a program based on a number of conditional while-loops which animates a movement of a square with the edge length in pixels entered as a prompted input. The square moves back and forth along a 45-degrees slope (straight line) from the bottom-left corner across the output console.

Note: In both assignments you should be able to control the speed of animation either by changing the delay time or by changing the value of increments/decrements.

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Graphics.html

This class^ pretty much has all the methods you need. drawRect and drawOval or fillRect and fillOval are probably the ones you will use. What you need to do is shown here: http://java.sun.com/docs/books/tutorial/2d/geometry/primitives.html . I can never remember when you're supposed to override the paintComponent method instead of the paint method (anyone?), but it says to override the paint method so do that. Their example class extends JApplet but you can extend JFrame and it should work the same way. And you can basically have variables in your class that are the parameters of fillRect etc, and update those variables while calling repaint in a loop. (Inside the paint method, you'll need to follow the example, and also pass your variables into the fillRect method or whatever you use)

This article has been dead for over six months. Start a new discussion instead.