Results 1 to 11 of 11
5Likes Thread: Java is not painting anything
- 02-17-2013, 04:31 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 7
- Rep Power
- 0
Java is not painting anything
Im am trying to create a simple program that makes stars randomly apear on the screen.
When you use your mousewheel they have to change size and color. i dont see any errors left in the code but it just wont paint anything.
i hope some of you can help me with this, its important that i know at least the basics of java befor next week.gif)
Java Code:import java.awt.*; import javax.swing.JFrame; public class Main extends JFrame { public Main() { setLayout(new FlowLayout()); setSize( 500, 500 ); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); setTitle( "Starry sky" ); setLocation( 200, 100 ); setBackground(Color.BLUE); setVisible( true ); } public static void main(String[] args) { new Main(); } }Java Code:import java.awt.*; import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; import java.util.Random; import javax.swing.JPanel; public class StarrySky extends JPanel implements MouseWheelListener { private Star[] stars = new Star[100]; Random location = new Random(); private int size = 30; private Color colour; Star s; public StarrySky() { addMouseWheelListener( this ); for (int index = 0; index < 100; index++) { int x = location.nextInt(600); int y = location.nextInt(600); if(index < 50) { stars[index] = new RedStar(x,y,size,colour); } else { stars[index] = new Star(x,y,size,colour); } } } public void paintcomponent(Graphics g) { super.paintComponent(g); super.setBackground(Color.blue); for(Star s : stars) { s.teken(g); } } public void mouseWheelMoved(MouseWheelEvent e) { s.Evolve(); repaint(); } }Java Code:import java.awt.*; public class Star { protected int x, y, size; protected Color colour; public Star(int x,int y,int size,Color colour) { this.x = x; this.y = y; this.size = size; this.colour = Color.YELLOW; } public void teken(Graphics g) { g.setColor(colour); g.fillRect(x, y, size, size); } public void Evolve() { if (size <= 100) { size = size + 2; } } }Java Code:import java.awt.*; public class RedStar extends Star { public RedStar(int x,int y,int size,Color colour) { super(x, y, size, colour); } public void teken(Graphics g) { g.setColor(colour); g.fillRect(x, y, size, size); } private Color colour(int r, int g, int b) { return colour ; } public void Evolve() { int r, g, b; r = 245; g = 0; b = 0; if( r < 255) { r +=2; colour = colour(r,g,b); } } }
- 02-17-2013, 04:45 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Java is not painting anything
You're not adding a StarrySky object to the content pane of your Main object as far as I can see.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-17-2013, 04:54 PM #3
Member
- Join Date
- Jan 2013
- Posts
- 7
- Rep Power
- 0
Re: Java is not painting anything
thanks for the reply,
I've had a feeling that the reason had to be something like that, but, could you please tell me how to add StarrySky to the Main class?
I'm trying a lot of diffrent things but it just doesn't work
- 02-17-2013, 05:05 PM #4
Re: Java is not painting anything
I guess you need to start here: Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-17-2013, 08:49 PM #5
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 682
- Rep Power
- 1
Re: Java is not painting anything
In your Main constructor, just do
There may be other issues though.Java Code:StarrySky sky = new StarrySky(); add(sky);
Regards,
Jim
- 02-19-2013, 12:39 PM #6
Member
- Join Date
- Jan 2013
- Posts
- 7
- Rep Power
- 0
Re: Java is not painting anything
thanks for the help everyone.
I've added Jim's code to my main class but it still doesn't do anything.
something else i find strange is that i've set the background colour to blue but it's still gray.
Am i missing the code to paint anything?
EDIT:
i just noticed a typo in the paintComponent in StarrySky.
it is paintcomponent while it should be paintComponent.
now when i run my program i see a small blue square, it's not nearly finished but i can finally try moreLast edited by Hagelslag; 02-19-2013 at 12:57 PM.
- 02-19-2013, 01:46 PM #7
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 682
- Rep Power
- 1
Re: Java is not painting anything
Hi, that last error can be a common problem. It is advisable to put the @Override annotation before any method which is to be overridden. That way, if you get the signature wrong or mistype the method name, the compiler will flag it as an error. Otherwise, it just thinks you are creating a new method. If you are not familiar with annotations, try The Java™ Tutorials.
Regards,
JimLast edited by jim829; 02-19-2013 at 01:49 PM.
The Java™ Tutorial
YAT -- Yet Another Typo
- 02-19-2013, 01:52 PM #8
Member
- Join Date
- Jan 2013
- Posts
- 7
- Rep Power
- 0
Re: Java is not painting anything
- 02-19-2013, 02:26 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Java is not painting anything
I'm a bit rusty on this, but I don't see anything there that gives Swing any way of calculating how big the JPanel should be.
Looks to me like it's just gone for a default size (ie small).
Can I also suggest setting the background colour in the constructor?
Seems a bit pointless setting it everytime the paintComponent is called.Please do not ask for code as refusal often offends.
- 02-19-2013, 02:33 PM #10
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 682
- Rep Power
- 1
Re: Java is not painting anything
Ok, I did two things.
First, I gave StarrySky a size:
Then I added pack() at the and of the main constructor.Java Code:setPreferredSize(new Dimension(500, 500));
Second, in the mouseWheelMoved method, you were referencing a local variable s that was not initialized. So I put a loop around it as shown.
So now it shows movement but since I haven't tried to figure out what you want to do, that is all. One final note. It's subtle but it is documented in the Window API. If you do aJava Code:@Override public void mouseWheelMoved(MouseWheelEvent e) { System.out.println(e.getClickCount()); for (Star s : stars) { s.Evolve(); } repaint(); }in the Main constructor it will center your frame on the screen.Java Code:setLocationRelativeTo(null);
Regards,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 02-19-2013, 03:19 PM #11
Member
- Join Date
- Jan 2013
- Posts
- 7
- Rep Power
- 0
Re: Java is not painting anything
thanks, this is great!
.gif)
.gif)
just a few tweaks and its prefect..gif)
I'm currently reading those oracle java tutorials so that i wont have to bother all of you with my problems anymore.gif)
the @Override is a great tip too, i'll try to keep using that from now on..gif)
Thank you all for helping me out!
Similar Threads
-
Re-painting
By tnrh1 in forum New To JavaReplies: 36Last Post: 11-09-2012, 05:31 PM -
Painting in SWT
By jionnet in forum SWT / JFaceReplies: 9Last Post: 09-24-2010, 06:52 AM -
Painting
By xael in forum New To JavaReplies: 6Last Post: 09-06-2010, 05:10 AM -
First Java game: Why is screen painting so slow?
By CoderMan in forum Java GamingReplies: 5Last Post: 05-20-2010, 02:55 AM -
Java applet strange painting behaviour
By sirdori in forum New To JavaReplies: 1Last Post: 01-07-2009, 01:14 PM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks