3 Replies - 11798 Views - Last Post: 26 March 2011 - 06:52 PM Rate Topic: -----

#1 LM_PROSTAR   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 31-January 10

Java Periodic Table project help!

Posted 26 March 2011 - 02:45 PM

Hey guys I'm doing a periodic table project and I'm having some trouble writing clean code here..I'm attempting to populate a grid layout using an array of buttons for each element and an array of labels for the blank spaces. The trouble I have is that it seems like it is going to be a lot of repetitive code =/

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;

public class Project2 extends JApplet implements ActionListener
{
   private static final int LABEL_SIZE = 19;
   private static final int BUTTON_SIZE = 50;
   //DATA DICTIONARY
   
   private JPanel centerPanel;            //panel that will hold the periodic table
   private BorderLayout centerLayout;     //place the periodic table in the center of the screen
   private JLabel labelArray[];           //array that will hold the labels in the periodic table
   private JButton buttonArray[];         //array that will hold the buttons for each element
   private GridLayout periodicLayout;     //layout to organize the multiple element buttons

   @Override
   public void init()                              //Set up the GUI (periodic table)
   {
      //Initialize the layout that will be used for the periodic tbale (10 rows, 19 columns)
      periodicLayout = new GridLayout(10, 19 , 3, 3);
      setLayout(periodicLayout);

      //Initilize the array that will hold all the buttons
      buttonArray = new JButton[BUTTON_SIZE];

      //Initilize the array that will hold all the buttons
      labelArray = new JLabel[LABEL_SIZE];

      //populate the buttonArray
      labelArray[0] = new JLabel(" "); labelArray[1] = new JLabel("1"); labelArray[2] = new JLabel("2");
      labelArray[3] = new JLabel("3"); labelArray[4] = new JLabel("4"); labelArray[5] = new JLabel("5");
      labelArray[6] = new JLabel("6"); labelArray[7] = new JLabel("7"); labelArray[8] = new JLabel("8");
      labelArray[9] = new JLabel("9"); labelArray[10] = new JLabel("10");
      labelArray[11] = new JLabel("11"); labelArray[12] = new JLabel("12");
      labelArray[13] = new JLabel("13"); labelArray[14] = new JLabel("14");
      labelArray[15] = new JLabel("15"); labelArray[16] = new JLabel("16");
      labelArray[17] = new JLabel("17"); labelArray[18] = new JLabel("18");
      

      //populate the buttonArray
      buttonArray[0] = new JButton("H"); buttonArray[1] = new JButton("He");
      buttonArray[2] = new JButton("Li"); buttonArray[3] = new JButton("Be");
      buttonArray[4] = new JButton("B"); buttonArray[5] = new JButton("C");
      buttonArray[6] = new JButton("N"); buttonArray[7] = new JButton("O");
      buttonArray[8] = new JButton("F"); buttonArray[9] = new JButton("Ne");
      buttonArray[10] = new JButton("Na"); buttonArray[11] = new JButton("Mg");
      buttonArray[12] = new JButton("Al"); buttonArray[13] = new JButton("Si");
      buttonArray[14] = new JButton("P"); buttonArray[15] = new JButton("S");
      buttonArray[16] = new JButton("Cl"); buttonArray[17] = new JButton("Ar");

      //Initialize the top panel which will hold the calculator's text field
      centerPanel = new JPanel();
      centerPanel.setLayout(periodicLayout);

      //first row
      centerPanel.add(labelArray[0]); 
      centerPanel.add(labelArray[1]);
      for(int i=0; i<18; i++)
      {
         centerPanel.add(labelArray[0]);
      }
      centerPanel.add(labelArray[18]);
      
      //Second row
      centerPanel.add(labelArray[1]); centerPanel.add(buttonArray[0]); centerPanel.add(labelArray[2]);
      for(int i=0;i<10;i++)
      {
         centerPanel.add(labelArray[0]);
      }
      for(int i=13; i<18; i++)
      {
         centerPanel.add(labelArray[i]);
      }
      centerPanel.add(buttonArray[2]);
      
      //Thrid Row
      //Fourth Row
      //Fifth Row
      //Sixth Row
      //Seventh Row
      //Eigth Row
      //Ninth Row
      //Tenth Row


      add(centerPanel, BorderLayout.CENTER);
   }


Any suggestions? Also there are labels for the number of the current row and column

thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Java Periodic Table project help!

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5612
  • View blog
  • Posts: 14,686
  • Joined: 18-April 07

Re: Java Periodic Table project help!

Posted 26 March 2011 - 03:14 PM

Have you considered creating a two dimensional array of "element" objects? Then you can create a list of labels whatever data you want to load and create instances of the button objects to then fill the array with. Would be much faster and cut down tons of code.

public class Element {
    private String name;
    private double atomicWeight;

    ... etc
}

// Then in your main program create an array of names or whatever data
// Use it to then build element objects and put them in a custom Element[][] array.

String[] elementNames = {"H","He","Li","Be"};



This is one nice approach. You can take variations of this too. The thought here is that you construct an element object which you can use to set its data, label, weight etc and then store into an array at a specified position.

Hope it helps! :)
Was This Post Helpful? 1
  • +
  • -

#3 LM_PROSTAR   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 31-January 10

Re: Java Periodic Table project help!

Posted 26 March 2011 - 03:45 PM

Ok so what you are saying is create a multi-dimensional array which I have thought of before BUT I wasn't sure If you would be able to do that in my case. In order to create a periodic table Im going to need to fill in the blank spaces so I used an array of labels. For each element I used a button which I then created an array of buttons for. If I used a multidimensional array how would I combine two different data types (labels and buttons)? I would have to give details on each element so yes I think a class Element would work.

Thanks =)
Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Java Periodic Table project help!

Posted 26 March 2011 - 06:52 PM

I'd avoid the array. You really don't have to hang on to the objects at all, just listen for them.

One trick is private methods to build your windows. Also, different classes for different elements.

Here's a quick example:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

class PeriodicPanel extends JPanel implements ActionListener {
	static final int ROWS = 7; // period
	static final int COLS = 18; // group
	
	public PeriodicPanel(){
		setLayout(new GridLayout(ROWS + 1, COLS + 1));
		
		addSpace(); for(int i=0; i<COLS; i++) { addLabel("" + (i+1)); }
		addLabel("1"); addEle("H"); addSpace(COLS-2); addEle("He");
		addLabel("2"); addEle("Li", "Be"); addSpace(10); addEle("B","C","N","O","F","Ne");
		addLabel("3"); addEle("Na","Mg"); addSpace(10); addEle("Al","Si","P","S","Cl","Ar");
		addLabel("4"); addSpace(COLS);
		addLabel("5"); addSpace(COLS);
		addLabel("6"); addSpace(COLS);
		addLabel("7"); addSpace(COLS);
	}

	private void addLabel(String name) { add(new JLabel(name)); }
	
	private void addSpace() { add(new JLabel(" ")); }
	
	private void addSpace(int n) { for(int i=0; i<n; i++) { addSpace(); } }
	
	private void addEle(String ... names) {
		for(String name : names) {
			JButton b = new JButton(name);
			b.addActionListener(this);
			b.setActionCommand(name);
			add(B)/>;
		}
	}
	
	public void actionPerformed(ActionEvent evt) {
		String cmd = evt.getActionCommand();
		System.out.println(cmd);
	}
}

public class Periodic {
	public static void main(String[] args) {
		JFrame frm = new JFrame("Periodic");
		frm.add(new PeriodicPanel(), BorderLayout.CENTER);
		frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frm.setSize(1100, 600);
		frm.setVisible(true);
	}
}



I don't like how the button looks. I'd probably make a customer JComponent for it, just to have a different look. You could also alter colors, store period, etc.

Kind of a fun project. Hope this helps.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1