Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

To display twinkling stars in applet  RSS feed

 
Annie John
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to write a program in java applet to display twinkling stars?

the color of the stars should change every time they twinkle.

thanks in advance
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds like a homework problem. We aren't a code mill. We don't provide complete solutions. We will help you though, if you show some effort. What have you done so far? Show us your code and where you're having problems. Keep it concise.
 
Annie John
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry.
 
Annie John
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


he problem is that i want to display a star shaped object..but dont know how to do so...so used fillRect() instead..can you suggest a way out?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you want a full blow twinkling star or will an asterisk (*) do?
 
Annie John
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you could help me with both.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmmm.

Let us get to basics first then there are some more pointers regarding AWT-Swing which we can tackle later.

You are drawing a 5x5 rect. Check out the API for Graphics class for a method to draw a string (since Graphics are used to paint, everything is drawing lingo, else you might expect a method to write a string. "*" is a string. Use this instead of the 5x5 rect.

You have managed to generate random colors. However you are using those colors to set the foreground, where as what you really want to do is set the color of the stars. Again check out the API for Graphics class for a method to set the color.

After that, you need to define what you mean by twinkling. I ran your code and it looks more like raindrops on a windshield than twinkling stars
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And while you're about that, you might as well switch to Swing (JApplet, not Applet) -- AWT is soooo dated.

A couple of comments:
-- If you design your UI with its custom painting in a JPanel, it's easy to add that to a JApplet / JFrame / JDialog and even add it alongside other components. Designing a custom painting in a top level container has drawbacks.
-- If you change to Swing, the painting method to override is paintComponent(...), not paint(...) More details here:
http://java.sun.com/docs/books/tutorial/uiswing/painting/index.html
-- Last and most important, do not change the state of a component in a painting override. At least, not unless you completely understand the effect of doing so. setBack/ForeGround both give rise to a repaint(), and only the coalescing of pending painting events saves you from a near-total hang. Even so, without testing I can say that your code must be chewing up processor cycles like nobody's business.

Another useful tutorial trail for learning about 2D graphics is
http://java.sun.com/docs/books/tutorial/2d/index.html
 
Stanislav Lapitsky
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java-sl.com/shapes.html
Use this to get Star Polygons to be rendered.

Regards,
Stas
 
Annie John
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!