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

    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");
    		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();
    	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) {
            Graphics2D g2 = (Graphics2D)g;
    		for(int i = 0; i < WIDTH; i++) {
    			for (int j = 0; j < HEIGHT; j++) {
    				switch(currentLevel[i][j]) {
    				case WALL:	
    				case ROCK:	
    					rock.setFrame(cell[i][j].getX(), cell[i][j].getY(), cell[i][j].getWidth() -2, cell[i][j].getHeight() - 2);
    				default:	System.out.println("Not Wall");
    	public static void main(String[] args) throws Exception {
    		BoulderDash b = new BoulderDash();
    		JFrame f = new JFrame();
    	    f.setLayout(new FlowLayout());

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    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.

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

Posting Permissions

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