Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Running an baisic Snowman applet

 
Julie Anne Moore
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading Java Software Solutions 7th edition and just starting working with applets.

One of the first examples in the book is not running properly for me.
It will compile but when I ask it to run it does not stop running in the terminal window until I press control-C; is that how applets work.
Also when I ask it to run I do not get the output the book says it is suppose to give. It is all correct except the background color is missing.



Filename: Untitled.tiff
Description:
File size: 20 Kbytes
[Download Untitled.tiff] Download Attachment
 
Tony Docherty
Bartender
Posts: 3054
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With Swing components such as JApplet you shouldn't be overriding the paint() method, if you need to do custom painting you should be adding a component such as a JPanel to the content pane and drawing on that. Having said that you can get your code working if you set the background colour of the content pane ie:

But as I've said you really should use another component to do the drawing and you should set the background in the init() method and not in paint().
 
Julie Anne Moore
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:With Swing components such as JApplet you shouldn't be overriding the paint() method, if you need to do custom painting you should be adding a component such as a JPanel to the content pane and drawing on that. Having said that you can get your code working if you set the background colour of the content pane ie:

But as I've said you really should use another component to do the drawing and you should set the background in the init() method and not in paint().


Thanks for the advice. JPanel is taught in the next chapter.

The code is copy directly from the textbook. Is it a change in Java that is causing the problem, that you need to include getContentPane()?

By adding I get a cyan screen till I adjust the size then the snowman shows up.
If I can the screen smaller then bigger the the the cyan background is only on that was the smaller part of the screen.
 
Tony Docherty
Bartender
Posts: 3054
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No there has been no change in Java that would cause this issue. The code posted looks very much like it is written for AWT and not Swing ie your class should extend Applet and not JApplet.

Sorry I forgot to tell you about one other inclusion you need that is at the top of your paint method (but after setting the background colour if you have done it in the paint method) add:


Alternatively run the code as it originally was but change JApplet to Applet.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic