I'm trying to build a periodic table of the elements program. I have a couple of classes, and a JSON file acting as a database. I am using an executable jar file for a couple of reasons, one of the biggest is because Applet security restrictions doesn't apply me to read files like JSON files.
The periodic table image is going to be partially static. The current plan is to draw it out, and use a 2d array and the location of the cursor to determine other actions. (Such actions includes a popup-menu that shows extra information on the element)
My issue: What I have, I believe should be working, but only one of the element blocks is being shown in the end. (And it isn't even being drawn correctingly.... That little part is probably some minor bug that I can find later)
Here is what is shown: (It's the Periodic Table of the Elements... There is post to be 118 elements)
Here is my source files: (Main Source File)
package PeriodicTable;
class PeriodicTable {
public static void main (String[] args) {
Table table = new Table();
}
}
My overall json structure is as following:
JsonObject holds JsonArray (Elements)
Elements holds 118 JsonObjects (1 object per element)
JsonObjects hold information for their corresponding element
(As of right now, basically everything that is not shown on the screen)
package PeriodicTable;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.json.JsonArray;
import javax.json.JsonObject;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.io.IOException;
class Table {
private JFrame frmMain;
private Element[][] aryElements;
Dimension dmsScreenSize;
int intScreenHeight, intScreenWidth;
DataBaseReader dbReader;
private int intElementsColumns = 18, intElementsRows = 9;//18 columns in the periodic table, 7 rows + the 2 F block rows, 6 and 7. Totals 9 rows
public Table(){
dmsScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
intScreenWidth = (int)dmsScreenSize.getWidth();
intScreenHeight = (int)dmsScreenSize.getHeight();
frmMain = new JFrame("Periodic Table of the Elements");
frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmMain.setMinimumSize(dmsScreenSize);
dbReader = new DataBaseReader();
layout("Periodic Table");
frmMain.setVisible(true);
}
void layout(String strLayout){ //Creates the specified table, at the time there is only one.
switch(strLayout){
case "Periodic Table":
periodicTable();
break;
}
}
private void periodicTable() {
aryElements = new Element[intElementsRows][intElementsColumns]; //creates a two-dimensional array of the class Element, hold 9 by 18 blocks
try{
JsonArray jAryElements = dbReader.readDataBase(); //obtains the Element array from Elements.JSON
Element clsElement; //Define an element instance, but not create
for(int index = 0; index < jAryElements.size(); index++){ //go through the Element array (called jAryElements), for every json object in this array...
clsElement = new Element(jAryElements.getJsonObject(index));//create an instance of class Element with the current selected JsonObject
frmMain.add(clsElement); /*add that instance to the JFrame frmMain (Element extends JPanel)
*(For below) Store that instance in the 2d array
*storage location is dependant on the element's row and column in the periodic table*/
aryElements[Integer.parseInt(element.getString("row"))-1][Integer.parseInt(element.getString("columnNumber"))-1] = clsElement;
}
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
(Currently handling next to all graphics)
package PeriodicTable;
import javax.json.JsonObject;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
class Element extends JPanel {
JsonObject element;
int intX, intY, intWidth, intHeight;
Font aFont, bFont, cFont;
Element(JsonObject elementForeign){
element = elementForeign; //Store the json object from the database here
intWidth = 50; //width of an element
intHeight = 71; //height of an element
intX = 10+(intWidth*(Integer.parseInt(element.getString("columnNumber"))));
/*<10+> add some space to the left of the entire grid of elements
*<intWidth*(<some code>(<some code>("columnNumber")>
* position would be equal to the element width multiplied by it's column. */
intY = 50+(intHeight*(Integer.parseInt(element.getString("row")))); //Same as intX, but vertically
aFont = new Font("TimesRoman", Font.BOLD, 10);//some fonts for text inside each element
bFont = new Font("TimesRoman", Font.PLAIN, 9);
cFont = new Font("TimesRoman", Font.BOLD, 17);
}
public void update(Graphics gr){ //Post to stop the program from clearing the screen
paintComponent(gr);
}
public void paintComponent(Graphics gr){/*painting method. I've read someone describing the activation
*of this method to occur "magically" when ever something happens to the container
*such as resizing the main window.*/
gr.setColor(getColor("")); //The specifics for every piece of information to be drawn inside a element block
gr.drawRect(intX, intY, intWidth, intHeight);
gr.setColor(getColor(element.getString("group")));
gr.fillRect(intX+1, intY+1, intWidth-2, intHeight-2);
gr.setColor(getColor(""));
gr.setFont(aFont);
gr.drawString(element.getString("atomicNumber"), intX+4, intY+10);
gr.drawString(element.getString("molarMass"), intX+4, intY+29);
gr.setFont(bFont);
gr.drawString(element.getString("electronegativity"), intX+4, intY+20);
gr.drawString(element.getString("ionCharge1"), intX+4, intY+29);
gr.drawString(element.getString("ionCharge2"), intX+4, intY+38);
gr.drawString(element.getString("name"), intX+(intWidth/2)-((gr.getFontMetrics().stringWidth(element.getString("name")))/2), intY+56);
gr.setColor(getColor(element.getString("naturalState")));
gr.setFont(cFont);
gr.drawString(element.getString("symbol"), intX+((intWidth/2)-((gr.getFontMetrics().stringWidth(element.getString("symbol")))/2)), intY+45);
gr.setColor(getColor(element.getString("synthetic")));
gr.drawRect(intX+(intWidth-(2*(intWidth/5)))-1, intY+1, intWidth/5, intWidth/5);
gr.fillRect(intX+(intWidth-(2*(intWidth/5)))-1, intY+2, intWidth/5, intWidth/5);
gr.setColor(getColor(element.getString("diatomic")));
gr.drawRect(intX+(intWidth-(intWidth/5))-1, intY+1, intWidth/5, intWidth/5);
gr.fillRect(intX+(intWidth-(intWidth/5))-1, intY+2, intWidth/5, intWidth/5);
}
private Color getColor(String strType){ //Returns a color depending on the situation (the string that is passed in)
switch(strType){
case "Hydrogen":
return new Color(255,229,204);
case "Alkali Metal":
return new Color(255,102,102);
case "Alkali Earth Metal":
return new Color(255,204,204);
case "Transition Metal":
return new Color(153,255,153);
case "Inner-Transition Metal":
return new Color(0,255,0);
case "Metalloid":
return new Color(255,0,255);
case "Post-Transition Metal":
return new Color(255, 153,51);
case "Halogen":
return new Color(255,128,0);
case "Noble Gas":
return new Color(204,229,255);
case "Unknown-Post-Transition Metal":
case "Unknown-Halogen":
case "Unknown-Noble Gas":
case "false":
return new Color(255,255,255);
case "true":
return new Color(255,255,0);
case "Gas":
return new Color(255,0,0);
case "Liquid":
return new Color(0,0,255);
case "Solid":
default:
return new Color(0,0,0);
}
}
}
What versions/programs/etc am I using?
Notepad
Command Prompt
Java 8
javax.json-1.0.jar
The program will javac, jar and java -jar fine, the database reads fine and I can use the information. For that reason, I will not include additional information about those (unless someone asks).
Before this, I've tried using layouts and such, but I had a ridiculous amount of trouble with this... Eventually I had thought of using a 2d array to hold the information.
I've searched many sites, including:
http://www.oracle.com/technetwork/java/painting-140037.html
http://www.slideshare.net/martyhall/java-7-programming-tutorial-multithreaded-graphics-and-animation
Java clears screen when calling paint method - how to avoid that?
And many more. Some of the things I didn't understand, but I did learn a fair amount.
Note, I'm still rather new to java, my actual education goes as far as the end of a highschool online computer science course. Though while working on this extra project I've learned many things outside of the course.
To clearly state my question...
Any clue as to why the incremental painting is not working? How would I fix this?