Results 1 to 2 of 2
Thread: Rectangle/Ellipse won't draw
- 03-07-2012, 03:50 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
Rectangle/Ellipse won't draw
So I'm trying to create a boulderdash game and i got as far as drawing the grid. I can also successfully draw walls (by changing background color of jpanel to black).... but when I'm trying to add a ROCK at the positions in the jpanel, i can't draw anything. What am I doing wrong??? I know rocks exist in array as when I move my code to draw a rock in the switch statment where the wall is, it doesn't draw a rock there either. Help Please
Java Code:import javax.swing.*; import java.awt.*; import java.awt.geom.Ellipse2D; import java.awt.geom.Rectangle2D; public class BoulderDash extends JPanel { int levels; static int WIDTH = 40; static int HEIGHT = 22; static BDTile[][] currentLevel = new BDTile[WIDTH][HEIGHT]; BDLevelReader bd; JPanel[][] cell = new JPanel[WIDTH][HEIGHT]; Rectangle2D.Double rec = new Rectangle2D.Double(40,40,30,30); Ellipse2D.Double rock = new Ellipse2D.Double(); public BoulderDash() throws Exception { bd = new BDLevelReader(); levels = bd.readLevels("levels.xml"); bd.setCurrentLevel(levels); initLevel(); this.setPreferredSize(new Dimension(800,700)); this.setLayout(new GridLayout(WIDTH, HEIGHT)); for (int i = 0; i < WIDTH; i++) { for (int j = 0; j < HEIGHT; j++){ cell[i][j] = new JPanel(); cell[i][j].setBorder(BorderFactory.createLineBorder(Color.GREEN)); this.add(cell[i][j]); } } } public void initLevel() { //sets the currentLevel 2D array for each level for (int i = 0; i < WIDTH; i++) { for (int j = 0; j < HEIGHT; j++){ currentLevel[i][j] = bd.getTile(i, j); } } } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; setBorder(BorderFactory.createRaisedBevelBorder()); for(int i = 0; i < WIDTH; i++) { for (int j = 0; j < HEIGHT; j++) { switch(currentLevel[i][j]) { case WALL: cell[i][j].setBackground(Color.BLACK); break; case ROCK: rock.setFrame(cell[i][j].getX(), cell[i][j].getY(), cell[i][j].getWidth() -2, cell[i][j].getHeight() - 2); g2.setColor(Color.RED); g2.fill(rock); g2.draw(rock); break; default: System.out.println("Not Wall"); } } } } public static void main(String[] args) throws Exception { BoulderDash b = new BoulderDash(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); f.add(b); f.pack(); f.setLayout(new FlowLayout()); f.setVisible(true); } }
- 03-07-2012, 07:51 AM #2
Re: Rectangle/Ellipse won't draw
Moved from New to Java
First off, never call setBackground(...) or make any change of state in the GUI components form a painting method override.
Scond, we have no idea what your BDTile class looks like. To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem. Not all your code.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Draw rectangle in a frame
By Mothrat in forum Java 2DReplies: 0Last Post: 12-13-2010, 10:52 AM -
Draw rectangle by dragging mouse
By Tyler in forum SWT / JFaceReplies: 11Last Post: 08-09-2010, 07:50 PM -
Draw String in Rectangle
By DavidG24 in forum AWT / SwingReplies: 3Last Post: 05-20-2009, 07:05 AM -
How to Draw Ellipse in Java
By Java Tip in forum java.awtReplies: 0Last Post: 06-23-2008, 11:13 PM -
How to Draw a Rectangle in Java
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:09 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks