Java: Interactive Elements Grid

I plan to make an editor for my current project, and for this you will need java swing.

Basically, I need a grid in some frame; individual grid elements should be selected by clicking and with the help of the drop-down / selector element you should be able to change the color of the grid element.

Can someone tell me which parts of the swing I will need? Any help would be really appreciated;)

Edit: get a little distracted

This editor is planning to create maps for an Android game strategy that I am developing with some of my friends. Let them say that we have a square field of 16x16 fields, all green by default. Having selected one field, I want to change the color of this field to something else.

In addition, each field should be able to return its value (i.e. color)

+4
source share
1 answer

Your question is a bit short in detail, but maybe you need a JPanel that uses a GridLayout and contains an array of JLabels whose opaque property is true. You can add MouseListener to JLabels, which shows JPopupMenu, which shows the possible colors, and then depending on your choice, use it to set the background color of JLabel (which shows since it was opaque).

For instance:

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(); } } } 
+9
source

Source: https://habr.com/ru/post/1379818/


All Articles