Hi, can someone help me understand what I need to do to reduce my code size so I won't need 56 separate methods to add all of my actionListeners for a JButton array? I'll just include smaller chunks of my code since the full set is about 800 lines. Any help is much appreciated & this is not for any homework assignment. I genuinely enjoy writing code.
Here's what the code does: It's a map making application which uses an array of default JButtons as the 'map' which can be edited by clicking from sample JButtons on a side panel. It's essentially a palette of MS Paint .pngs I created which you can click on the map with to alter their state. I use it to draw dungeons in video games, but you could reconfigure it to be let's say, a chess board. If anyone would like to see the code in it's entirety, I can share more.
variable creation & import statements:
..../*Created by John Thrush 06/25/18 WizMapMaker is a video game supplement that works as a drawing tool. The application creates an array of buttons which the user can toggle to paint images from a side panel. */ import java.awt.*; import javax.swing.*; import java.awt.event.*; public class WizMapMakerEdit extends JFrame { private final static int ROWS = 27; private final static int COLS = 32; private JPanel outerPane = new JPanel(); private JPanel gridPane = new JPanel(); private JPanel innerPane = new JPanel(); private JPanel optionsPane = new JPanel(); private JPanel samplePane = new JPanel(); private JButton tiles[][]; private JButton oButtons[]; private String oButtonNames[]; //add new buttons: PLACE NEW ITEM HERE - STEP 1 private JButton floor = new JButton(""); private JButton wallFill = new JButton("");
More variables.. see the commented for loop. That's what I'm trying to figure out.
private JLabel previewLabel= new JLabel(); private JLabel buttonLabel = new JLabel(""); private ReportClick handler = new ReportClick(); private GridLayout gl = new GridLayout(12,5); private GridLayout bl = new GridLayout(ROWS,COLS); private GridLayout sl = new GridLayout(1,1); private DefaultListCellRenderer dr = new DefaultListCellRenderer(); public WizMapMakerEdit(String title) { //set a title & min size for outer frame super(title); setMinimumSize(new Dimension(1800, 985)); //add components to panels tiles = new JButton[ROWS][COLS]; dr.setHorizontalAlignment(DefaultListCellRenderer.CENTER); previewLabel.setIcon(new ImageIcon(getClass().getResource("Floor.png"))); addOptions(); //add new button listeners: PLACE NEW ITEM HERE - STEP 2 for(int m=0; m<56; m++) { oButtons[m].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // change the icon displayed when button is pressed previewLabel.setIcon(new ImageIcon(getClass().getResource(oButtonNames[m]))); } } ); }/* for(int i=0; i<56; i++) { oButtons[i].setIcon(new ImageIcon(getClass().getResource(oButtonNames[i]))); optionsPane.add(oButtons[i]); } floor.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // change the icon displayed when button is pressed previewLabel.setIcon(new ImageIcon(getClass().getResource("Floor.png"))); } } ); wallFill.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // change the icon displayed when button is pressed previewLabel.setIcon(new ImageIcon(getClass().getResource("WallFill.png"))); } } );*/
This chunk is where I have about 56 repeated code for each of the JButtons. It's highly inefficient- I know there is a better way..
floor.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // change the icon displayed when button is pressed previewLabel.setIcon(new ImageIcon(getClass().getResource("Floor.png"))); } } );
Here's where the button array is configured:
private void buildButtons() { for(int i=0; i < ROWS; i++) { for(int j=0; j < COLS; j++) { tiles[i][j] = new JButton(); tiles[i][j].addActionListener(handler); tiles[i][j].setSize(300,300); tiles[i][j].add(buttonLabel); gridPane.add(tiles[i][j]); } } } //add new images to options: PLACE NEW ITEM HERE - STEP 3 private void addOptions() { JButton oButtons[] = new JButton[] {floor, wallFill, cornerNW, cornerSW, cornerNE, cornerSE, wallN, wallS, wallW, wallE, bumpNW, bumpNE, bumpSW, bumpSE, doorN, doorS, doorW, doorE, capN, capS, capW, capE, hallH, hallV, StairsUp, StairsDown, EVator, WarpIn, WarpOut, Pool, Pit, SPit, Shaft, SShaft, Spinner, Darkness, Boss, Event, NPC, NPCPatrol, LockDoorN, LockDoorS, LockDoorW, LockDoorE, SDoorN, SDoorS, SDoorW, SDoorE, FWallN, FWallS, FWallW, FWallE, OneDoorN, OneDoorS, OneDoorW, OneDoorE}; String[] oButtonNames = new String[] {"floor.png", "wallFill.png", "cornerNW.png", "cornerSW.png", "cornerNE.png", "cornerSE.png", "wallN.png", "wallS.png", "wallW.png", "wallE.png", "bumpNW.png", "bumpNE.png", "bumpSW.png", "bumpSE.png", "doorN.png", "doorS.png", "doorW.png", "doorE.png", "capN.png", "capS.png", "capW.png", "capE.png", "hallH.png", "hallV.png", "StairsUp.png", "StairsDown.png", "EVator.png", "WarpIn.png", "WarpOut.png", "Pool.png", "Pit.png", "SPit.png", "Shaft.png", "SShaft.png", "Spinner.png", "Darkness.png", "Boss.png", "Event.png", "NPC.png", "NPCPatrol.png", "LockDoorN.png", "LockDoorS.png", "LockDoorW.png", "LockDoorE.png", "SDoorN.png", "SDoorS.png", "SDoorW.png", "SDoorE.png", "FWallN.png", "FWallS.png", "FWallW.png", "FWallE.png", "OneDoorN.png", "OneDoorS.png", "OneDoorW.png", "OneDoorE.png"}; for(int i=0; i<56; i++) { oButtons[i].setIcon(new ImageIcon(getClass().getResource(oButtonNames[i]))); optionsPane.add(oButtons[i]); } optionsPane.add(erase); }
Again, thanks for any assistance on this! The error I get says something about an inner class in the loop. I'm assuming I have some recursion going on.
B1F_Map_Wizardry5.jpg