|
-
February 8th, 2011, 06:11 PM
#1
Jpanel + Grid location using MouseListener
Hi,
I can't seem to think up a way or can't seem to find an appropriate method in the APIs to help with this.... the problem is as follows;
I have a grid of JPanels that contain JLabels with randomly assigned background colours, I have MouseListener successfully implemented so far. I have it set up to tell me that if a user clicks anywhere on the Grid it states where it clicks... for example ("Clicked at 534, 60"). However, I was wondering if there is any way to in fact to take the information I gain from the getX() and getY() methods and use it to figure which Panel and Label in the grid I am clicking so I can then run a getBackground() method and compare its background to neighbouring panels.
Will basically work like a bubble breaker game.
If my explanation above seems a bit all over the place, just let me know and I'll try clarifying further.
Thanks for any help offered and if you like, my code thus far is given below:
first class: GameInterface.java
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.JComponent.*;
public class GameInterface extends JFrame implements ActionListener{
GridForGame gridForGame = new GridForGame();
public GameInterface(){
super("Bubble Breaker");
buildGUI();
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
pack();
setVisible(true);
setResizable(false);
}
private void buildGUI(){
JPanel main = new JPanel(new BorderLayout());
add(main, BorderLayout.CENTER);
//JLabel statusbar;
JPanel menu = new JPanel();
menu.setLayout(new BoxLayout(menu, BoxLayout.Y_AXIS));
menu.setPreferredSize(new Dimension(150,300));
Dimension buttonSize = new Dimension(120,40);
addButton("8 x 8 Grid", "small", buttonSize, menu);
addButton("12 x 12 Grid", "medium", buttonSize, menu);
addButton("16 x 16 Grid", "large", buttonSize, menu);
addButton("How To Play", "howToPlay", buttonSize, menu);
addButton("Options", "options", buttonSize, menu);
addButton("Quit", "quitGame", buttonSize, menu);
menu.setBorder(new TitledBorder(new EtchedBorder(4, Color.blue, Color.black), "Menu"));
JPanel buttons = new JPanel(new GridLayout(1,1));
buttons.add(menu);
main.add(buttons, BorderLayout.EAST);
main.add(gridForGame, BorderLayout.CENTER);
//statusbar = new JLabel("default");
//add(statusbar, BorderLayout.EAST);
}
public void addButton(String label, String command, Dimension size, JPanel panel){
JButton button = new JButton(label);
button.setPreferredSize(size);
button.setMaximumSize(size);
Dimension seperatorSize = new Dimension(0,5);
button.setAlignmentX(CENTER_ALIGNMENT);
button.addActionListener(this);
button.setActionCommand(command);
panel.add(Box.createRigidArea(seperatorSize));
panel.add(button);
}
public void actionPerformed(ActionEvent ae){
String command = ae.getActionCommand();
if (command == "small"){
gridForGame.small();
}
if (command == "medium"){
gridForGame.medium();
}
if (command == "large"){
gridForGame.large();
}
if(command.equals("howToPlay")){
// check source code for URL that goes here
}
if(command.equals("options")){
// check source image for code that goes here
}
if(command == "quitGame"){
int selectedValue = JOptionPane.showConfirmDialog(null, "If Your Quit The Game All Data Will Be Lost" +
"\n" + "Are You Sure You Want To Quit?", "Attention", JOptionPane.YES_NO_OPTION);
// Might be issues above due to splitting brackets onto lines
if(selectedValue == JOptionPane.NO_OPTION)
return;
else System.exit(0);
}
}
public static void main( String args[]){
GameInterface i = new GameInterface();
i.addWindowListener(new WindowAltering());
}
static class WindowAltering extends WindowAdapter{
public void windowClosing(WindowEvent e ){
int selectedValue = JOptionPane.showConfirmDialog(null, "Closing This Window Will End the Game." +
"\n" + "Click Yes To Exit", "Exit Bubble Breaker Game?", JOptionPane.YES_NO_OPTION);
if(selectedValue == JOptionPane.NO_OPTION)
return;
else System.exit(0);
}
}
}
second/final class : GridForGame.java
Code:
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.util.Random;
import java.awt.Color.*;
import java.awt.Cursor.*;
import javax.swing.border.*;
import java.awt.event.*;
public class GridForGame extends JPanel {
JTextField myNumberArray[] = new JTextField[16];
MouseHandling handler = new MouseHandling();
//JLabel statusbar;
JLabel statusbar = new JLabel("default");
public GridForGame(){
setLayout(new BorderLayout());
setBackground(Color.gray);
setBorder(new EtchedBorder(4, Color.blue, Color.blue));
setPreferredSize(new Dimension(600,600));
}
public void small(){
JPanel squareContainer = new JPanel();
squareContainer.setLayout(new GridLayout(8,8));
squareContainer.setPreferredSize(new Dimension(500,500));
squareContainer.setMaximumSize(new Dimension(500,500));
squareContainer.addMouseListener(handler);
statusbar.setText("default");
add(statusbar, BorderLayout.NORTH);
for(int i=0; i<8; i++){
JPanel squares = new JPanel();
squares.setLayout(new GridLayout(1,1));
for(int j=0; j<8; j++){
JLabel number = new JLabel();
number.setEnabled(true);
number.setOpaque(true);
//number.setEditable(false);
number.setBorder(new MatteBorder(1, 1, 1, 1, Color.black));
squares.add(number);
for(int k=0; k<8; k++){
int n = new Random().nextInt(7);
switch(n){
case 0: number.setBackground(Color.red); break;
case 1: number.setBackground(Color.cyan); break;
case 2: number.setBackground(Color.blue); break;
case 3: number.setBackground(Color.green); break;
case 4: number.setBackground(Color.yellow); break;
case 5: number.setBackground(Color.magenta); break;
default:
}
}
}
squareContainer.add(squares);
}
add(squareContainer);
revalidate();
}
public void medium(){
JPanel squareContainer = new JPanel();
squareContainer.setLayout(new GridLayout(12,12));
squareContainer.setPreferredSize(new Dimension(500,500));
squareContainer.setMaximumSize(new Dimension(500,500));
squareContainer.addMouseListener(handler);
statusbar.setText("default");
add(statusbar, BorderLayout.NORTH);
for(int i=0; i<12; i++){
JPanel squares = new JPanel();
squares.setLayout(new GridLayout(1,1));
for(int j=0; j<12; j++){
JLabel number = new JLabel();
number.setEnabled(true);
number.setOpaque(true);
number.setBorder(new MatteBorder(1, 1, 1, 1, Color.black));
squares.add(number);
for(int k=0; k<12; k++){
int n = new Random().nextInt(7);
switch(n){
case 0: number.setBackground(Color.red); break;
case 1: number.setBackground(Color.cyan); break;
case 2: number.setBackground(Color.blue); break;
case 3: number.setBackground(Color.green); break;
case 4: number.setBackground(Color.yellow); break;
case 5: number.setBackground(Color.magenta); break;
default:
}
}
}
squareContainer.add(squares);
}
add(squareContainer);
revalidate();
}
public void large(){
JPanel squareContainer = new JPanel();
squareContainer.setLayout(new GridLayout(16,16));
squareContainer.setPreferredSize(new Dimension(500,500));
squareContainer.setMaximumSize(new Dimension(500,500));
squareContainer.addMouseListener(handler);
statusbar.setText("default");
add(statusbar, BorderLayout.NORTH);
for(int i=0; i<16; i++){
JPanel squares = new JPanel();
squares.setLayout(new GridLayout(1,1));
for(int j=0; j<16; j++){
JLabel number = new JLabel();
number.setEnabled(true);
number.setOpaque(true);
number.setBorder(new MatteBorder(1, 1, 1, 1, Color.black));
squares.add(number);
for(int k=0; k<16; k++){
int n = new Random().nextInt(7);
switch(n){
case 0: number.setBackground(Color.red); break;
case 1: number.setBackground(Color.cyan); break;
case 2: number.setBackground(Color.blue); break;
case 3: number.setBackground(Color.green); break;
case 4: number.setBackground(Color.yellow); break;
case 5: number.setBackground(Color.magenta); break;
default:
}
}
}
squareContainer.add(squares);
}
add(squareContainer);
revalidate();
}
private class MouseHandling implements MouseListener{
public void mouseClicked(MouseEvent event){
//do stuff here
statusbar.setText(String.format("Clicked at %d,%d", event.getX(), event.getY()));
}
public void mousePressed(MouseEvent event){
//do stuff here
statusbar.setText("anything");
}
public void mouseReleased(MouseEvent event){
//do stuff here
statusbar.setText(String.format("Released at %d,%d", event.getX(), event.getY()));
}
public void mouseEntered(MouseEvent event){
//do stuff here
statusbar.setText("Entered");
}
public void mouseExited(MouseEvent event){
//do stuff here
statusbar.setText("Exited");
}
}
}
Thanks again for any help that could be offered!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|