Hi everyone.
I want to program an animated applet. Five rects are drawn that grow and shrink. The fith rect is placed in the middle and is “overlapped” by the others - it doesn’t seem the be animated. The animation is controlled by a “border”-Value that determins the size of the other rects.
It compiles without any errors on java2 1.4.2 and 1.5 but then the runtime outputs the following error:
Parameter error - java.lang.NumberFormatException: null
x: 0 | y: 0 | r: 26 | h: 100 | b: 200
java.awt.Color[r=0,g=0,b=255]
Exception in thread "Timer-0" java.lang.NullPointerException
at Rechteck$Tasks.malen(Rechteck.java:79)
at Rechteck$Tasks.run(Rechteck.java:63)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)
Here is my code:
import java.awt.*;
import java.applet.*;
import java.util.Timer;
import java.util.TimerTask;
public class Rechteck extends Applet
{
Graphics graphics;
Timer timer;
private int x, y, b, h, r;
private boolean grow = false; //Rect shrinks at program start
public void init()
{
getParams();
timer = new Timer();
timer.schedule(new Tasks(), 1000);
}
public void getParams()
{
try
{
x = Integer.parseInt(getParameter("x"));
y = Integer.parseInt(getParameter("y"));
b = Integer.parseInt(getParameter("b"));
h = Integer.parseInt(getParameter("h"));
r = Integer.parseInt(getParameter("r"));
if (r>=b/2-1)
r=b/2-1;
if (r>=h/2-1)
r=b/2-1;
}
catch(NumberFormatException ex)
{
x = 0;
y = 0;
b = 200;
h = 100;
r = 25;
System.out.println("Parameter error - " + ex);
}
}
class Tasks extends TimerTask
{
public void run()
{
// Prevent the graphic grows larger than the applet or tries to shrinc smaler than one pixel.
if (isMaxSize())
grow = true;
else if (r == 1)
grow = false;
// Calculate border dimension for the next frame.
if (grow == true)
r--;
else
r++;
// Paint the new image after all calculations are done.
malen();
}
private boolean isMaxSize()
{
if (r >= b/2-1 || r >= h/2-1)
return true;
else
return false;
}
private void malen()
{
//x, y, breite, höhe
System.out.println("x: " + x + " | y: " + y + " | r: " + r + " | h: " + h + " | b: " + b);
System.out.println(Color.blue);
graphics.setColor(Color.blue);
graphics.fillRect(x,y,r,h-r);
graphics.setColor(Color.red);
graphics.fillRect(x+r,y,b-r,r);
graphics.setColor(Color.black);
graphics.fillRect(x+r,y+r,b-2*r,h-2*r);
graphics.setColor(Color.yellow);
graphics.fillRect(x,y+h-r,b-r,r);
graphics.setColor(Color.green);
graphics.fillRect(x+b-r,y+r,r,h-r);
}
}
}
