Results 1 to 2 of 2
  1. #1
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

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

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

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

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

Similar Threads

  1. Draw rectangle in a frame
    By Mothrat in forum Java 2D
    Replies: 0
    Last Post: 12-13-2010, 10:52 AM
  2. Draw rectangle by dragging mouse
    By Tyler in forum SWT / JFace
    Replies: 11
    Last Post: 08-09-2010, 07:50 PM
  3. Draw String in Rectangle
    By DavidG24 in forum AWT / Swing
    Replies: 3
    Last Post: 05-20-2009, 07:05 AM
  4. How to Draw Ellipse in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 11:13 PM
  5. How to Draw a Rectangle in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:09 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
  •