Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Grid of clickable elements

I'm planning on making an editor for my current project and will need some java swing for it.

Basically I need a grid within some sort of frame; the single elements of the grid should be able to be selected via click and with a dropdown/selector element you should be able to change the color of the grid element

Can anyone tell me what parts of swing I'll need for that? Any help would be really appreciated ;)

Edit: let's go a bit into detail

This editor is planned to generate maps for an android strategy game I develope with some friends of mine Let's say we have a square field of 16x16 fields which are all by default green. By selecting the single field I want to be able to change the color of this field to something else.

In addition, every field should be able to return it's value (i.e. the color)

like image 493
deemel Avatar asked Nov 05 '11 21:11

deemel


1 Answers

Your question is a little short on details, but perhaps you want a JPanel that uses GridLayout and holds an array of JLabels whose opaque property is true. You could add a MouseListener to the JLabels that shows a JPopupMenu that shows possible colors, and then depending on the selection use it to set the JLabel's background color (which shows since it has been made opaque).

For example:

Main.java

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Main {
   private static void createAndShowGui() {
      int rows = 20;
      int cols = 40;
      int cellWidth = 20;
      ColorGrid mainPanel = new ColorGrid(rows, cols, cellWidth);

      JFrame frame = new JFrame("Color Grid Example");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

ColorGrid.java

import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.*;

@SuppressWarnings("serial")
public class ColorGrid extends JPanel {
   private MyColor[][] myColors;
   private JLabel[][] myLabels;

   public ColorGrid(int rows, int cols, int cellWidth) {
      myColors = new MyColor[rows][cols];
      myLabels = new JLabel[rows][cols];

      MyMouseListener myListener = new MyMouseListener(this);
      Dimension labelPrefSize = new Dimension(cellWidth, cellWidth);
      setLayout(new GridLayout(rows, cols));
      for (int row = 0; row < myLabels.length; row++) {
         for (int col = 0; col < myLabels[row].length; col++) {
            JLabel myLabel = new JLabel();
            myLabel = new JLabel();
            myLabel.setOpaque(true);
            MyColor myColor = MyColor.GREEN;
            myColors[row][col] = myColor;
            myLabel.setBackground(myColor.getColor());
            myLabel.addMouseListener(myListener);
            myLabel.setPreferredSize(labelPrefSize);
            add(myLabel);
            myLabels[row][col] = myLabel;
         }
      }
   }

   public MyColor[][] getMyColors() {
      return myColors;
   }

   public void labelPressed(JLabel label) {
      for (int row = 0; row < myLabels.length; row++) {
         for (int col = 0; col < myLabels[row].length; col++) {
            if (label == myLabels[row][col]) {
               MyColor myColor = myColors[row][col].next();
               myColors[row][col] = myColor;
               myLabels[row][col].setBackground(myColor.getColor());
            }
         }
      }
   }
}

MyColor.java

import java.awt.Color;

public enum MyColor {
   GREEN(Color.green, "Green", "g"), RED(Color.red, "Red", "r"), 
   BLUE(Color.blue, "Blue", "b"), YELLOW(Color.yellow, "Yellow", "y");
   private Color color;
   private String name;
   private String shortName;

   private MyColor(Color color, String name, String shortName) {
      this.color = color;
      this.name = name;
      this.shortName = shortName;
   }

   public MyColor next() {
      int index = 0;
      for (int i = 0; i < MyColor.values().length; i++) {
         if (MyColor.values()[i] == this) {
            index = (i + 1) % MyColor.values().length;
         }
      }
      return MyColor.values()[index];
   }

   public Color getColor() {
      return color;
   }

   public String getName() {
      return name;
   }

   public String getShortName() {
      return shortName;
   }

   @Override
   public String toString() {
      return shortName;
   }

}

MyMouseListener.java

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;

public class MyMouseListener extends MouseAdapter {
   private ColorGrid colorGrid;

   public MyMouseListener(ColorGrid colorGrid) {
      this.colorGrid = colorGrid;
   }

   @Override
   public void mousePressed(MouseEvent e) {
      if (e.getButton() == MouseEvent.BUTTON1) {
         colorGrid.labelPressed((JLabel)e.getSource());
      } else if (e.getButton() == MouseEvent.BUTTON3) {
         MyColor[][] myColors = colorGrid.getMyColors();
         for (int row = 0; row < myColors.length; row++) {
            for (int col = 0; col < myColors[row].length; col++) {
               System.out.print(myColors[row][col] + " ");
            }
            System.out.println();
         }
         System.out.println();
      }
   }
}
like image 153
Hovercraft Full Of Eels Avatar answered Nov 01 '22 07:11

Hovercraft Full Of Eels