• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setBackground method problem?

 
dan kom
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, upon executing the following Applet in Applet viewer in BlueJ , the background color turns Cyan only when i resize the window of Applet viewer, is the setBackground method works as intended?


 
Bod McLeon
Ranch Hand
Posts: 62
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

This could not be the best way to do it, but I am doing the exact same as you are and the code I use is:

All of these are customisable, so if you can find the colour code for cyan (which as far as I know is 0,255,255) and change the first line it should work.


 
Bod McLeon
Ranch Hand
Posts: 62
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am doing this with Panel so as I said it might not work.
 
Bod McLeon
Ranch Hand
Posts: 62
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JPanel*
 
Piet Souris
Rancher
Posts: 1522
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a JApplet,  you should put these commands in the 'init ()' method, not  in the 'paint ()' method. See the api.
 
Rob Camick
Ranch Hand
Posts: 2673
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To follow up on Piet comments here is a simple example to get you started:


Note you need to change the background of the "content pane" of the applet.
 
Tony Docherty
Bartender
Posts: 3054
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Piet has said you should not be putting such code in the paint method. Also if you do want to put some code in the paint method you should generally call super.paint() as the first line of the method.

BTW why are you working with applets, they are a dead technology?
 
dan kom
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:To follow up on Piet comments here is a simple example to get you started


I've started reading the book JSS by pearson eighth edition, and i'm just at the end of chapter 2, there is an exercise there and i pretty much copied the whole JApplet code. So i'm a newbie.

I wondered why the background works only when i resize the window, or is the book wrong?? i can post a a picture from the book showing the source code.
 
Rob Camick
Ranch Hand
Posts: 2673
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know why the code in the text book doesn't work. All I know is that it is wrong.

I showed you the proper way to create the applet and solve your problem.

I suggest you can learn Swing basics from the Swing tutorial.. There are plenty of working examples that are better structured then the code from the book.

As already mentioned you are wasting time learning how to use applets. Applets are not supported in many browsers any more because of security risks.
 
Campbell Ritchie
Sheriff
Posts: 51349
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please provide more details about the book; I cannot find anything under JSS
 
dan kom
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:I don't know why the code in the text book doesn't work. All I know is that it is wrong.

I showed you the proper way to create the applet and solve your problem.

I suggest you can learn Swing basics from the Swing tutorial.. There are plenty of working examples that are better structured then the code from the book.

As already mentioned you are wasting time learning how to use applets. Applets are not supported in many browsers any more because of security risks.


thanks for your help, but i'm taking Java Course at the uni and the uni decided to choose that book for the course, and they insist doing everything 'by the book'

Cambell ritchie wrote:
Welcome to the Ranch 

Please provide more details about the book; I cannot find anything under JSS


thanks, the book called Java Software Solutions, i will upload an image of the code.
 
dan kom
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://imgur.com/a/ig8eW
 
Campbell Ritchie
Sheriff
Posts: 51349
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this the book? I am surprised that a book printed in 2014 still teaches applets, since nobody uses them any more.
 
dan kom
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Is this the book? I am surprised that a book printed in 2014 still teaches applets, since nobody uses them any more.




 
dan kom
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Is this the book? I am surprised that a book printed in 2014 still teaches applets.


Yes, it's from 2004.
 
Campbell Ritchie
Sheriff
Posts: 51349
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Applets were still in common use in 2004; that appears to be an older edition of the book I found.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic