Results 1 to 3 of 3
  1. #1
    ruben381 is offline Member
    Join Date
    Jan 2012
    Posts
    13
    Rep Power
    0

    Default Rectangles not showing up on a panel.

    It's probably something really herp derp flurpy, like it always is with me, but I just can't see it.

    Any help here is appreciated.

    Main.java

    Java Code:
    package source;
    
    import javax.swing.*;
    
    public class Main
    {
    	
    	public static Main0 f;
    	public static int width = 800;
    	public static int height = 600;
    
    	
    	public static void main(String[] args) 
    	{
    		f = new Main0();
    		f.setVisible(true);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setResizable(false);
    		f.setSize(width, height);
    		f.setTitle("Platformer");
    		f.setLocationRelativeTo(null);
    	}
    	
    
    
    }

    Main0.java

    Java Code:
    package source;
    
    import java.awt.*;
    
    import javax.swing.*;
    
    public class Main0 extends JFrame{
    	
    	public Main1 panel;
    	
    	public Main0()
    	{
    		panel = new Main1();
    		setLayout(new GridLayout(1, 1, 0, 0));
    		add(panel);
    	}
    
    }
    Main1.java

    Java Code:
    package source;
    
    import java.awt.*;
    import java.awt.event.KeyAdapter;
    
    import javax.swing.*;
    
    import org.lwjgl.input.Keyboard;
    
    public class Main1 extends JPanel implements Runnable{
    	
    	public Rectangle character;
    	public Rectangle ground;
    	
    	
    	public int characterWidth = 20;
    	public int characterHeight = 20;
    	public boolean falling = false;
    	public int fps = 1000;
    	public int fallingSpeed = 10;
    	public int fallingFrame = 0;
    	public int floorHeight = 80;
    	
    	public int moveStrength = 1;
    	public int moveFrame = 0;
    	public int fallinginairStrength = 10;
    	public int resetMovement = 0;
    	
    	
    	public boolean objectsDefined = false;
    
    	
    	public Thread game;
    	public boolean running = true;
    	
    	public boolean right = false;
    	public boolean left = false;
    	
    	public Main1() 
    	{
    		setBackground(Color.black);
    		
    		defineObjects();
    		
    		game = new Thread(this);
    		game.start();
    		
    		if(Keyboard.isKeyDown(Keyboard.KEY_D))
    		{
    			right = true;
    		}
    		if(Keyboard.isKeyDown(Keyboard.KEY_A))
    		{
    			right = true;
    		}
    				
    		}
    	
    	public void defineObjects()
    	{
    		
    		character = new Rectangle((Main.width / 2 ) - (characterWidth / 2), (Main.height/2) - (characterHeight/2), characterWidth, characterHeight);
    		ground = new Rectangle(-10, Main.height-floorHeight, Main.width+10, floorHeight);
    		
    		
    		objectsDefined= true;
    		
    		repaint();
    	}
    	
    	public void paintComponents(Graphics g)
    	{
    		super.paintComponents(g);
    		
    		if(objectsDefined)
    		{
    			g.setColor(Color.WHITE);
    			g.fillRect(character.x, character.y, character.width, character.height);
    			g.fillRect(ground.x, ground.y, ground.width, ground.height);
    		}
    	}
    	
    	public void run() 
    	{
    		while(running)
    		{
    			Point pt1 = new Point(character.x, character.y + character.height);
    			Point pt2 = new Point(character.x, character.y + character.width);
    			character.y += fallingSpeed;
    			
    			if(fallingFrame >= fallingSpeed)
    			{
    				if(ground.contains(pt1) || ground.contains(pt2)) 
    				
    				{
    					falling = false;
    				} 
    				else
    				{
    				character.y += 1;
    				falling = true;
    				}
    				
    				if(falling)
    				{
    					character.y += 1;
    				}
    				
    				
    				fallingFrame = 0;
    			}
    			else
    			{
    				fallingFrame += 1;
    			}
    			
    			if(falling)
    			{
    				moveStrength = fallinginairStrength;
    			}
    			else
    			{
    				moveStrength = resetMovement;
    			}
    			
    			// L/R MOVING
    			if(moveFrame >= moveStrength)
    					{
    						if(right)
    						{
    							character.x -= 1;
    							
    							moveFrame = 0;
    						}
    						if(left)
    						{
    							character.x += 1;
    							
    							moveFrame = 0;
    						}
    					}
    			else
    			{
    				moveFrame  += 1;
    			}
    			setFrames();
    			
    			repaint();
    		}
    		
    	}
    	
    	public void setFrames()
    	{
    		try
    		{
    			game.sleep(fps/1000);
    		}catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    	}
    
    }


    Thanks,

    -Adam

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Rectangles not showing up on a panel.

    Is the code that draws the shapes being executed? Add a println that prints out the location and size of the shape being drawn to see.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Rectangles not showing up on a panel.

    1. You shouldn't be overriding paintComponents(...). Since the panel doesn't have any components, that method won't be executed.
    2. Thread.sleep(...) is a static method and shouldn't be invoked on an instance of Thread, as that is misleading.
    3. Learn to use a javax.swing.Timer instead. That's what the class is for.

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

Similar Threads

  1. Why only one of the two rectangles is painted?
    By JOHNINALBANY in forum Java 2D
    Replies: 5
    Last Post: 07-07-2012, 10:54 PM
  2. Intersection of two Rectangles
    By Dex in forum New To Java
    Replies: 4
    Last Post: 03-25-2012, 02:28 PM
  3. Collision between 2 rectangles
    By CNew in forum New To Java
    Replies: 1
    Last Post: 12-05-2010, 04:18 AM
  4. Tooltip text showing through panel
    By Glovergg in forum AWT / Swing
    Replies: 2
    Last Post: 06-11-2010, 07:25 AM
  5. Rectangles method
    By bdario1 in forum New To Java
    Replies: 31
    Last Post: 03-31-2010, 09:32 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
  •