Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Animated applet produces runtime errors  (Read 775 times)
0 Members and 1 Guest are viewing this topic.
Offline Nark

Senior Newbie





« Posted 2004-12-16 18:54:26 »

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:
1  
2  
3  
4  
5  
6  
7  
8  
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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
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);
        }
    }      
}

Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #1 - Posted 2004-12-16 19:38:47 »

1. You create a "Graphics" variable called "graphics", then you never set it to anything. Ergo, the NullPointerException. That's okay, though, because the next thing you should do is...

2. Move the "malen()" method to your outer class. Change its name and signature to:

1  
public void paint(Graphics g)


3. Add the following line at the end of the new "paint(Graphics g)" method:

1  
repaint();





Now, I'm sure you're wondering about the instructions above. The problem you're having is probably based on a misunderstanding of how painting works. You see, the applet only repaints itself from the method "paint(Graphics g)". That method is called by the operating system whenever it feels like it needs to update the applet. However, you may occasionally want to update it manually, so the "repaint()" call tells the OS to call "paint(Graphics g)".

Let me know if you have any questions. :-)

Java Game Console Project
Last Journal Entry: 12/17/04
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

ctomni231 (34 views)
2014-07-18 06:55:21

Zero Volt (30 views)
2014-07-17 23:47:54

danieldean (25 views)
2014-07-17 23:41:23

MustardPeter (27 views)
2014-07-16 23:30:00

Cero (42 views)
2014-07-16 00:42:17

Riven (44 views)
2014-07-14 18:02:53

OpenGLShaders (32 views)
2014-07-14 16:23:47

Riven (33 views)
2014-07-14 11:51:35

quew8 (30 views)
2014-07-13 13:57:52

SHC (66 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!