1 Replies - 564 Views - Last Post: 05 December 2012 - 09:12 PM Rate Topic: -----

#1 nvrBungeeJumped  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-December 12

Simple GUI; tool bar in layered pane plus images for tiles.

Posted 05 December 2012 - 06:06 PM

Just working on a simple Hnefatafl game. Having a bit of trouble adding a menu bar to the window. I think my problem is with the line
layeredPane.add(muBar, JLayeredPane.TOP_ALIGNMENT);

I believe TOP_ALIGNMENT is the bit that is wrong, and that it should be something like TOP_SOMETHINGTODOWITHALAYERNAME
Or maybe
layeredPane.add(muBar, JLayeredPane.DEFAULT_LAYER);

I've give that a try.

Other question is whith making the tiles. I am using square.setBackground and setting it to a colour. Anyway that it can be set to an image?

Full code below, thanks in advance


import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
 
public class kBoard extends JFrame implements MouseListener, MouseMotionListener {
	JLayeredPane layeredPane;
	JPanel kBoard;
	JLabel kPiece;
	int xAdjustment;
	int yAdjustment;
 
  public void splash(){
	   
  }
	
  public kBoard(){
	  Dimension boardSize = new Dimension(640, 640);
	 
	  //  Use a Layered Pane for this this application
	  layeredPane = new JLayeredPane();
	  getContentPane().add(layeredPane);
	  layeredPane.setPreferredSize(boardSize);
	  layeredPane.addMouseListener(this);
	  layeredPane.addMouseMotionListener(this);
	
	  //Add a tool bar to the Layered Pane
	  
	  //creates the menu bar object
	  JMenuBar muBar = new JMenuBar();
	  //creates the buttons for the menu bar
	  JMenu file = new JMenu("File");
	  muBar.add(file);
	  JMenu edit = new JMenu("Edit");
	  muBar.add(edit);
	  JMenuItem exit = new JMenuItem("Exit");
	  file.add(exit);
	  JMenuItem options = new JMenuItem("Options");
	  edit.add(options);
	  //sets the menu bar to the window with all the stuff we created on it
	  layeredPane.add(muBar, JLayeredPane.TOP_ALIGNMENT);
	  
	  //Add a chess board to the Layered Pane 
	 
	  kBoard = new JPanel();
	  layeredPane.add(kBoard, JLayeredPane.DEFAULT_LAYER);
	  kBoard.setLayout( new GridLayout(9, 9) );
	  kBoard.setPreferredSize( boardSize );
	  kBoard.setBounds(0, 0, boardSize.width, boardSize.height);
	 
	  for (int i = 0; i < 81; i++) {
	  JPanel square = new JPanel( new BorderLayout() );
	  kBoard.add( square );
	 
	  int row = (i / 9) % 1;
	  if (row == 0)
	  square.setBackground( i % 2 == 0 ? Color.black : Color.white );
	  else
	  square.setBackground( i % 2 == 0 ? Color.white : Color.black );
	  }
	 
	  //Add a few pieces to the board
	 
	  JLabel piece = new JLabel( new ImageIcon("king.png") );
	  JPanel panel = (JPanel)kBoard.getComponent(40);
	  panel.add(piece);
	  piece = new JLabel(new ImageIcon("defender.png"));
	  panel = (JPanel)kBoard.getComponent(38);
	  panel.add(piece);
	  piece = new JLabel(new ImageIcon("defender.png"));
	  panel = (JPanel)kBoard.getComponent(39);
	  panel.add(piece);  
	  piece = new JLabel(new ImageIcon("defender.png"));
	  panel = (JPanel)kBoard.getComponent(41);
	  panel.add(piece);
	  piece = new JLabel(new ImageIcon("defender.png"));
	  panel = (JPanel)kBoard.getComponent(42);
	  panel.add(piece);
	  piece = new JLabel(new ImageIcon("attacker.png"));
	  panel = (JPanel)kBoard.getComponent(3);
	  panel.add(piece);
	  piece = new JLabel(new ImageIcon("attacker.png"));
	  panel = (JPanel)kBoard.getComponent(4);
	  panel.add(piece); 
	  piece = new JLabel(new ImageIcon("attacker.png"));
	  panel = (JPanel)kBoard.getComponent(5);
	  panel.add(piece); 
	  piece = new JLabel(new ImageIcon("attacker.png"));
	  panel = (JPanel)kBoard.getComponent(13);
	  panel.add(piece);

  }
 
  public void mousePressed(MouseEvent e){
	kPiece = null;
	Component c =  kBoard.findComponentAt(e.getX(), e.getY());
		 
	if (c instanceof JPanel) 
	return;

	Point parentLocation = c.getParent().getLocation();
	xAdjustment = parentLocation.x - e.getX();
	yAdjustment = parentLocation.y - e.getY();
	kPiece = (JLabel)c;
	kPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
	kPiece.setSize(kPiece.getWidth(), kPiece.getHeight());
	layeredPane.add(kPiece, JLayeredPane.DRAG_LAYER);
  }
 
  //Moves the game piece around
  
  public void mouseDragged(MouseEvent me) {
	  if (kPiece == null) return;
	  kPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
 }
 
  //Drop the chess piece back onto the chess board
 
  public void mouseReleased(MouseEvent e) {
  if(kPiece == null) return;
 
  kPiece.setVisible(false);
  Component c =  kBoard.findComponentAt(e.getX(), e.getY());
 
  if (c instanceof JLabel){
  Container parent = c.getParent();
  parent.remove(0);
  parent.add( kPiece );
  }
  else {
  Container parent = (Container)c;
  parent.add( kPiece );
  }
 
  kPiece.setVisible(true);
  }
 
  //these are listeners for mouse clicked, moved, entered and exited events
  //we don't need any of them
  //delete in future
  //may need mouse exited to drop the piece when you try move a piece out of the frame
  //that might not be the best way to go about that
  public void mouseClicked(MouseEvent e) {
  
  }
  public void mouseMoved(MouseEvent e) {
 }
  public void mouseEntered(MouseEvent e){
  
  }
  public void mouseExited(MouseEvent e) {
  
  }
 
  public static void main(String[] args) {
	  JFrame gameFrame = new kBoard();
	  gameFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE );
	  gameFrame.pack();
	  gameFrame.setResizable(true);
	  gameFrame.setLocationRelativeTo( null );
	  gameFrame.setVisible(true);
 }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Simple GUI; tool bar in layered pane plus images for tiles.

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,885
  • Joined: 06-March 08

Re: Simple GUI; tool bar in layered pane plus images for tiles.

Posted 05 December 2012 - 09:12 PM

The JMenu goes to the JFrame not to the JLayeredPane inside it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1