Nim in a gui. Marbles do not draw correctly

This is my first real GUI application

Page 1 of 1

0 Replies - 2005 Views - Last Post: 13 March 2008 - 12:29 PM Rate Topic: -----

#1 brawnyman713  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 139
  • Joined: 21-October 07

Nim in a gui. Marbles do not draw correctly

Posted 13 March 2008 - 12:29 PM

So I am designing Nim in a gui. Nim works like this: You have a pile of marbles. When it is your turn, you must take a number of marbles between 1 and half of the pile. The object of the game is to have the computer take the last marble. I know it's ugly and all. My problem is that I can't get the marbles to draw within the confines of the panel. They draw themselves all over the frame. What do I need to do to get my marbles to draw correctly?

NimGUI.java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class NimGUI
{
	public static void main(String[] args)
	{
		Nim game = new Nim();
		JFrame frame = new JFrame();
		JPanel panel = new JPanel();
		JLabel status = new JLabel("Welcome to nim");
		MarblePanel marblePanel = new MarblePanel(200, 150, game.getMarbles());
		InputPanel input = new InputPanel("Enter the number of marbles", 4);

		panel.add(status);
		panel.add(input);
		frame.setLayout(new GridLayout());
		frame.add(panel);
		frame.add(marblePanel);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(500, 400);
		frame.setVisible(true);
	}
}



MarblePanel.java:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class MarblePanel extends JPanel
{
	private int numMarbles;
	public static final double MARBLE_SIZE = 10.0;
	private Ellipse2D ellipse;
	private Color[] colors = {Color.RED, Color.GREEN, Color.BLUE, Color.GRAY, Color.MAGENTA, Color.BLACK};
	
	public MarblePanel(int width, int height, int numMarbles)
	{
		setSize(width, height);
		this.numMarbles = numMarbles;
	}
	
	public MarblePanel(int width, int height)
	{
		setSize(width, height);
		numMarbles = (int) (Math.random() * 91) + 10;
	}
	
	public void paintComponent(Graphics g)
	{
		Graphics2D g2 = (Graphics2D) g;
		int width = getWidth();
		int height = getHeight();
		int marblesLeft = numMarbles;
		
		for(int x = 0; x + MARBLE_SIZE < width; x += 13)
		{
			for(int y = 0; y + MARBLE_SIZE < height && marblesLeft > 0; y += 13)
			{
				g2.setColor(colors[(int) (Math.random() * colors.length)]);
				ellipse = new Ellipse2D.Double(x, y, MARBLE_SIZE, MARBLE_SIZE);
				g2.fill(ellipse);
				marblesLeft--;
			}
		}
		
		marblesLeft = numMarbles;
		g2.drawString("" + numMarbles, 200, 200);
	}
	
	public void setMarbles(int numMarbles)
	{
		this.numMarbles = numMarbles;
	}
	
	public int getMarbles()
	{
		return numMarbles;
	}
}


Nim.java:
public class Nim
{
	private int numMarbles;
	private int winner;
	
	public Nim(int amount)
	{
		numMarbles = amount;
		winner = -1;
	}
	
	public Nim()
	{
		numMarbles = (int) (Math.random() * 91) + 10;
		winner = -1;
	}
	
	public void takeMarbles(int numMarbles, boolean isHuman)
	{
		// No one has won just yet
		if(winner < 0)
		{
			if(numMarbles > this.numMarbles / 2)
				numMarbles = this.numMarbles / 2;
			if(numMarbles < 1)
				numMarbles = 1;
	
			this.numMarbles -= numMarbles;
			
			if (this.numMarbles < 1)
			{
				if(isHuman)
					winner = 0;
				else
					winner = 1;
			}	
			
		}
		
	}
	
	
	public void setMarbles(int marbles)
	{
		numMarbles = marbles;
	}
	
	public int getMarbles()
	{
		return numMarbles;
	}
	
	public int getWinner()
	{
		return winner;
	}
	public boolean isValid(int amount)
	{
		if (amount < 1)
			return false;
		if (amount > numMarbles / 2 && numMarbles / 2 > 0)
			return false;
		
		return true;
	}
	
	public int getMax()
	{
		if(numMarbles > 1)
			return numMarbles / 2;
		else
			return 1;
	}
	
	// Returns the amount for the computer to take from the pile
	public int calcAmount(boolean isSmart)
	{
		int amount = 0, power = 0;
		
		if(isSmart)
		{
			// Determines the highest power of two below the pile amount
			for(int x = 1; (int) Math.pow(2, x) <= numMarbles; x++)
				power = x;
				
			amount = (numMarbles - (int) Math.pow(2, power)) + 1;
		}
		else
			amount = (int) (Math.random() * (numMarbles / 2)) + 1;
		
		if(amount > numMarbles / 2)
			amount = numMarbles / 2;
			
		if(amount < 1)
			amount = 1;
			
		return amount;
	}
	public String toString()
	{
		String result = "There are currently " + numMarbles + " in the pile";
		
		return result;
	}
}


InputPanel.java:
import javax.swing.*;

public class InputPanel extends JPanel
{
	public InputPanel(String inputText, int fieldWidth)
	{
		add(new JLabel(inputText));
		add(new JTextField(fieldWidth));
	}
}



This is my first real GUI application, and I know it's some elemental error. What is my problem?

Is This A Good Question/Topic? 0
  • +

Page 1 of 1