Skip to main content

Making black boxes move.

1 reply [Last post]
ryanreese
Offline
Joined: 2008-11-20

I have this

/*<br />
Ryan Reese<br />
ASCIILAND Game Board<br />
Pretty sexy stuff here<br />
*/</p>
<p>//Importing crap to make this work<br />
import javax.swing.*;<br />
import java.util.Random;<br />
import java.awt.*;<br />
import java.awt.geom.*;<br />
import java.awt.event.*;<br />
import java.awt.image.*;<br />
import java.io.*;<br />
import javax.imageio.*;<br />
import java.lang.*;</p>
<p>//Start the public class, aka the main class<br />
public class ASCIILAND<br />
{<br />
	//Declare all variables that won't change<br />
	public static final String p1="John";<br />
	public static final String p2="Bobby";<br />
	public static final String p3="Joseph";<br />
	public static boolean player1=true;<br />
	public static boolean player2=false;<br />
	public static boolean player3=false;<br />
	public static int playerSet;</p>
<p>	public static void main(String args[])<br />
	{<br />
		//Declare all the constructors so that I can access crap off of it ^_^<br />
      final Random generator=new Random();<br />
      JFrame frame=new JFrame();</p>
<p>      int[] cells= new int[29];<br />
      //Oh yea, now it loops to fill up all the variables<br />
      //29 variables total here<br />
      for (int i=0; i < 29; i++)<br />
      {<br />
          cells[i] = i+1;<br />
      }<br />
      //Gotta manually declare these variables because they need seperate names D;<br />
      int START;<br />
      int END;</p>
<p>      //Set crap for frame<br />
      frame.setSize(780, 550);<br />
      frame.setTitle("ASCIILAND Game Board");<br />
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);<br />
		addComponentsToPane(frame.getContentPane(), playerSet);<br />
      GridBagLayout gridbag = new GridBagLayout();<br />
      GridBagConstraints c = new GridBagConstraints();</p>
<p>      frame.setLayout(gridbag);</p>
<p>      JPanel board = new JPanel();<br />
      board.setSize(550,650);<br />
      board.setMinimumSize(new Dimension(550, 650));<br />
      board.setPreferredSize(new Dimension(550, 650));<br />
		//Loading the image...<br />
		LoadImageApp image=new LoadImageApp();<br />
		//Loading the  game board...<br />
		board.add(image);</p>
<p>		gridbag.setConstraints(board, c);<br />
		frame.add(board);</p>
<p>		//Player button<br />
		final JButton buttonPane1=new JButton("");<br />
		if(player1==true)<br />
		{<br />
			 buttonPane1.setText("Player 1 Roll");<br />
		}<br />
		if(player2==true)<br />
		{<br />
			 buttonPane1.setText("Player 2 Roll");<br />
		}<br />
		if(player3==true)<br />
		{<br />
			 buttonPane1.setText("Player 3 Roll");<br />
		}</p>
<p>		//Inside the brackets it is the code to be executed</p>
<p>		buttonPane1.addActionListener(<br />
		new ActionListener()<br />
		{<br />
      	public void actionPerformed(ActionEvent e)<br />
			{<br />
				final int roll1=generator.nextInt(8)+1;<br />
				buttonPane1.setEnabled(false);<br />
				playerSet=1;<br />
				if(player1==true)<br />
				{</p>
<p>					buttonPane1.setText("Player "+playerSet+" rolled a "+roll1+". Moving now...");<br />
					player1=false;<br />
					player2=true;<br />
				}<br />
      	}<br />
    	});<br />
		//Jbutton crap for players.<br />
		JPanel buttonPane = new JPanel();<br />
      buttonPane.setSize(100, 100);<br />
      buttonPane.setMinimumSize(new Dimension(100, 100));<br />
      buttonPane.setPreferredSize(new Dimension(100, 100));<br />
      buttonPane.add(buttonPane1);</p>
<p>		//Player constraints<br />
      gridbag.setConstraints(buttonPane, c);<br />
      frame.add(buttonPane1);<br />
      //Make it so you can see the frame</p>
<p>      frame.setVisible(true);<br />
	}</p>
<p>	public static void addComponentsToPane(Container pane, int playerSet)<br />
	{<br />
		//Sets to null so absolute positioning can take place<br />
		pane.setLayout(null);</p>
<p>		//Black box for player 1<br />
		if(player1==true)<br />
		{<br />
			playerSet=1;<br />
			JPanel black1=new JPanel();<br />
			Container box1=new blackBox();<br />
			JLabel label1=new JLabel("Player "+playerSet);<br />
			label1.setForeground(Color.WHITE);<br />
			box1.setBounds(25, 5, 50,  50);<br />
			box1.add(label1);<br />
			black1.add(box1);<br />
			pane.add(box1);<br />
		}<br />
	}</p>
<p>	public int boardMovement(int roll)<br />
	{<br />
		//This will do the board movement<br />
		return roll;<br />
	}<br />
}<br />
//Class to make the image<br />
class LoadImageApp extends Component<br />
{<br />
	BufferedImage img;</p>
<p>	//"Draws" the image<br />
	public void paint(Graphics g)<br />
	{<br />
		g.drawImage(img, 0, 0, null);<br />
	}<br />
	//Loads the image with a try/catch<br />
	public LoadImageApp()<br />
	{<br />
	 	//Need a try/catch becuase ImageIO.read. It requires one to be caught/thrown...not baseball though ;D.<br />
		try<br />
		{<br />
			img = ImageIO.read(new File("ASCIILAND.jpg"));<br />
		}<br />
		catch (IOException e)<br />
		{<br />
			e.printStackTrace();<br />
		}<br />
	}<br />
	 //Sets the dimensions of the image<br />
	 public Dimension getPreferredSize()<br />
    {<br />
        if (img == null)<br />
        {<br />
            return new Dimension(100,100);<br />
        }<br />
        else<br />
        {<br />
            return new Dimension(img.getWidth(null), img.getHeight(null));<br />
        }<br />
    }<br />
}<br />
class blackBox extends JPanel<br />
{<br />
	protected void paintComponent(Graphics g)<br />
	{<br />
		Graphics2D graphic = (Graphics2D) g;<br />
		graphic.setColor(Color.BLACK);<br />
		Rectangle2D.Double box = new Rectangle2D.Double(0, 0, 400, 400);<br />
		graphic.fill(box);<br />
	}<br />
}

This short example can be used for all intents and purposes.

<br />
import javax.swing.*;<br />
import java.awt.*;<br />
import java.awt.event.*;</p>
<p>//Start the public class, aka the main class<br />
public class ASCIILAND2<br />
{	</p>
<p>	public static void main(String args[])<br />
	{<br />
      JFrame frame=new JFrame();<br />
      frame.setSize(780, 550);<br />
      frame.setTitle("ASCIILAND Game Board");<br />
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);<br />
		addComponentsToPane(frame.getContentPane());<br />
      GridBagLayout gridbag = new GridBagLayout();<br />
      GridBagConstraints c = new GridBagConstraints();</p>
<p>      frame.setLayout(gridbag);</p>
<p>      frame.setVisible(true);<br />
	}</p>
<p>	public static void addComponentsToPane(Container pane)<br />
	{<br />
		//Sets to null so absolute positioning can take place<br />
		pane.setLayout(null);</p>
<p>		//Black box for player 1<br />
		JPanel black1=new JPanel();<br />
		Container box1=new blackBox();<br />
		JLabel label1=new JLabel("Player 1");<br />
		label1.setForeground(Color.WHITE);<br />
		box1.setBounds(25, 5, 50,  50);<br />
		box1.add(label1);<br />
		black1.add(box1);<br />
		pane.add(box1);</p>
<p>	}<br />
}

How do I move the black boxes?

Message was edited by: ryanreese

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ryanreese
Offline
Joined: 2008-11-20

Bump