Results 1 to 11 of 11
Like Tree5Likes
  • 1 Post By JosAH
  • 1 Post By jim829
  • 1 Post By jim829
  • 1 Post By Tolls
  • 1 Post By jim829

Thread: Java is not painting anything

  1. #1
    Hagelslag is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Default 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

    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);
                    
             } 
    	}
    	
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    Hagelslag likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Hagelslag is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Default 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

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Java is not painting anything

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: Java is not painting anything

    In your Main constructor, just do

    Java Code:
    StarrySky sky = new StarrySky();
    add(sky);
    There may be other issues though.

    Regards,
    Jim
    Hagelslag likes this.

  6. #6
    Hagelslag is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Default 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 more
    Last edited by Hagelslag; 02-19-2013 at 12:57 PM.

  7. #7
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default 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,
    Jim
    Last edited by jim829; 02-19-2013 at 01:49 PM.
    Hagelslag likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    Hagelslag is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Default Re: Java is not painting anything

    it seems like the little blue square has something to do with the super.setBackground in the StarrySky class.
    sometimes i see a little bit of yellow inside the square. everything except the mousewheellistener seems to work.
    does someone know how to fix this problem?Java is not painting anything-naamloos.pngJava is not painting anything-naamloos2.png

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default 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.
    Hagelslag likes this.
    Please do not ask for code as refusal often offends.

  10. #10
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: Java is not painting anything

    Ok, I did two things.

    First, I gave StarrySky a size:
    Java Code:
          setPreferredSize(new Dimension(500, 500));
    Then I added pack() at the and of the main constructor.

    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.
    Java Code:
    @Override
       public void mouseWheelMoved(MouseWheelEvent e) {
          System.out.println(e.getClickCount());
          for (Star s : stars) {
             s.Evolve();
          }
          repaint();
       }
    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 a
    Java Code:
    setLocationRelativeTo(null);
    in the Main constructor it will center your frame on the screen.

    Regards,
    Jim
    Hagelslag likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    Hagelslag is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Default Re: Java is not painting anything

    thanks, this is great!
    just a few tweaks and its prefect.
    I'm currently reading those oracle java tutorials so that i wont have to bother all of you with my problems anymore

    the @Override is a great tip too, i'll try to keep using that from now on.

    Thank you all for helping me out!

Similar Threads

  1. Re-painting
    By tnrh1 in forum New To Java
    Replies: 36
    Last Post: 11-09-2012, 05:31 PM
  2. Painting in SWT
    By jionnet in forum SWT / JFace
    Replies: 9
    Last Post: 09-24-2010, 06:52 AM
  3. Painting
    By xael in forum New To Java
    Replies: 6
    Last Post: 09-06-2010, 05:10 AM
  4. First Java game: Why is screen painting so slow?
    By CoderMan in forum Java Gaming
    Replies: 5
    Last Post: 05-20-2010, 02:55 AM
  5. Java applet strange painting behaviour
    By sirdori in forum New To Java
    Replies: 1
    Last Post: 01-07-2009, 01:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •