Results 1 to 3 of 3
- 05-17-2012, 02:15 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 9
- Rep Power
- 0
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
Main1.javaJava 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); } }
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
- 05-17-2012, 03:36 PM #2
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.
- 05-17-2012, 03:56 PM #3
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Why only one of the two rectangles is painted?
By JOHNINALBANY in forum Java 2DReplies: 5Last Post: 07-07-2012, 10:54 PM -
Intersection of two Rectangles
By Dex in forum New To JavaReplies: 4Last Post: 03-25-2012, 02:28 PM -
Collision between 2 rectangles
By CNew in forum New To JavaReplies: 1Last Post: 12-05-2010, 04:18 AM -
Tooltip text showing through panel
By Glovergg in forum AWT / SwingReplies: 2Last Post: 06-11-2010, 07:25 AM -
Rectangles method
By bdario1 in forum New To JavaReplies: 31Last Post: 03-31-2010, 09:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks