Fixed nullpointerexceptions

This commit is contained in:
Yorick
2015-03-16 13:46:20 +00:00
parent d31272622c
commit 836f058c85
+80 -76
View File
@@ -18,26 +18,26 @@ import javax.swing.JTextField;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class PropertiesPanel extends JPanel { public class PropertiesPanel extends JPanel {
DrawObject selectedObject = null; DrawObject selectedObject = null;
Panel p = null; Panel p = null;
JTextField locationXField; JTextField locationXField;
JTextField locationYField; JTextField locationYField;
JTextField scaleField; JTextField scaleField;
JTextField rotationField; JTextField rotationField;
PropertiesPanel() PropertiesPanel()
{ {
super(); super();
BoxLayout box = new BoxLayout(this, BoxLayout.PAGE_AXIS); BoxLayout box = new BoxLayout(this, BoxLayout.PAGE_AXIS);
setLayout(box); setLayout(box);
setPreferredSize(new Dimension(200,0)); setPreferredSize(new Dimension(200,0));
setBorder(BorderFactory.createLineBorder(new Color(180,180,180))); setBorder(BorderFactory.createLineBorder(new Color(180,180,180)));
setBackground(new Color(220, 220, 220)); setBackground(new Color(220, 220, 220));
//NAME "PROPERTIES" //NAME "PROPERTIES"
JPanel propPanel = new JPanel(); JPanel propPanel = new JPanel();
JLabel propLabel = new JLabel("Properties"); JLabel propLabel = new JLabel("Properties");
@@ -46,8 +46,8 @@ public class PropertiesPanel extends JPanel {
propPanel.setMaximumSize(new Dimension(200, 60)); propPanel.setMaximumSize(new Dimension(200, 60));
propPanel.add(propLabel); propPanel.add(propLabel);
add(propPanel); add(propPanel);
//LOCATION //LOCATION
JPanel locationPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); JPanel locationPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JLabel locationLabel = new JLabel("Location"); JLabel locationLabel = new JLabel("Location");
@@ -60,7 +60,7 @@ public class PropertiesPanel extends JPanel {
JPanel locationXPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); JPanel locationXPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
locationXPanel.setPreferredSize(new Dimension(200, 50)); locationXPanel.setPreferredSize(new Dimension(200, 50));
locationPanel.add(locationXPanel); locationPanel.add(locationXPanel);
JLabel locationXLabel = new JLabel("X"); JLabel locationXLabel = new JLabel("X");
locationXPanel.add(locationXLabel); locationXPanel.add(locationXLabel);
locationXField = new JTextField(); locationXField = new JTextField();
@@ -69,14 +69,14 @@ public class PropertiesPanel extends JPanel {
locationXField.addKeyListener(new KeyListener() { locationXField.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) { public void keyTyped(KeyEvent e) {
try{ try{
locationXField.setBackground(Color.WHITE); locationXField.setBackground(Color.WHITE);
double xpos = Double.parseDouble(locationXField.getText()); double xpos = Double.parseDouble(locationXField.getText());
selectedObject.setPosition(new Point2D.Double(xpos, selectedObject.getPosition().getY())); selectedObject.setPosition(new Point2D.Double(xpos, selectedObject.getPosition().getY()));
p.update(); p.update();
} catch (NumberFormatException nfe) { } catch (NumberFormatException nfe) {
locationXField.setBackground(Color.RED); locationXField.setBackground(Color.RED);
} }
} }
public void keyReleased(KeyEvent e) {} public void keyReleased(KeyEvent e) {}
public void keyPressed(KeyEvent e) {} public void keyPressed(KeyEvent e) {}
@@ -88,7 +88,7 @@ public class PropertiesPanel extends JPanel {
JPanel locationYPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); JPanel locationYPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
locationYPanel.setPreferredSize(new Dimension(200, 50)); locationYPanel.setPreferredSize(new Dimension(200, 50));
locationPanel.add(locationYPanel); locationPanel.add(locationYPanel);
JLabel locationYLabel = new JLabel("Y"); JLabel locationYLabel = new JLabel("Y");
locationYPanel.add(locationYLabel); locationYPanel.add(locationYLabel);
locationYField = new JTextField(); locationYField = new JTextField();
@@ -103,7 +103,7 @@ public class PropertiesPanel extends JPanel {
selectedObject.setPosition(new Point2D.Double(selectedObject.getPosition().getX(), ypos));; selectedObject.setPosition(new Point2D.Double(selectedObject.getPosition().getX(), ypos));;
p.update(); p.update();
} catch (NumberFormatException nfe) { } catch (NumberFormatException nfe) {
locationYField.setBackground(Color.RED); locationYField.setBackground(Color.RED);
} }
} }
public void keyReleased(KeyEvent e) {} public void keyReleased(KeyEvent e) {}
@@ -112,8 +112,8 @@ public class PropertiesPanel extends JPanel {
} }
locationYPanel.add(locationYField); locationYPanel.add(locationYField);
} }
//SCALE //SCALE
JPanel scalePanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); JPanel scalePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JLabel scaleLabel = new JLabel("Scale"); JLabel scaleLabel = new JLabel("Scale");
@@ -126,7 +126,7 @@ public class PropertiesPanel extends JPanel {
JPanel scalePanel2 = new JPanel(new FlowLayout(FlowLayout.LEFT)); JPanel scalePanel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
scalePanel2.setPreferredSize(new Dimension(200, 50)); scalePanel2.setPreferredSize(new Dimension(200, 50));
scalePanel.add(scalePanel2); scalePanel.add(scalePanel2);
JLabel scale2Label = new JLabel(" "); JLabel scale2Label = new JLabel(" ");
scalePanel2.add(scale2Label); scalePanel2.add(scale2Label);
scaleField = new JTextField(); scaleField = new JTextField();
@@ -135,14 +135,14 @@ public class PropertiesPanel extends JPanel {
scaleField.addKeyListener(new KeyListener() { scaleField.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) { public void keyTyped(KeyEvent e) {
try{ try{
scaleField.setBackground(Color.WHITE); scaleField.setBackground(Color.WHITE);
double scale = Double.parseDouble(scaleField.getText()); double scale = Double.parseDouble(scaleField.getText());
selectedObject.setScale(scale); selectedObject.setScale(scale);
p.update(); p.update();
} catch (NumberFormatException nfe) { } catch (NumberFormatException nfe) {
scaleField.setBackground(Color.RED); scaleField.setBackground(Color.RED);
} }
} }
public void keyReleased(KeyEvent e) {} public void keyReleased(KeyEvent e) {}
public void keyPressed(KeyEvent e) {} public void keyPressed(KeyEvent e) {}
@@ -150,8 +150,8 @@ public class PropertiesPanel extends JPanel {
} }
scalePanel2.add(scaleField); scalePanel2.add(scaleField);
} }
//ROTATION //ROTATION
JPanel rotationPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); JPanel rotationPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JLabel rotationLabel = new JLabel("Rotation"); JLabel rotationLabel = new JLabel("Rotation");
@@ -164,7 +164,7 @@ public class PropertiesPanel extends JPanel {
JPanel rotationPanel2 = new JPanel(new FlowLayout(FlowLayout.LEFT)); JPanel rotationPanel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
rotationPanel2.setPreferredSize(new Dimension(200, 50)); rotationPanel2.setPreferredSize(new Dimension(200, 50));
rotationPanel.add(rotationPanel2); rotationPanel.add(rotationPanel2);
JLabel rotation2Label = new JLabel(" "); JLabel rotation2Label = new JLabel(" ");
rotationPanel2.add(rotation2Label); rotationPanel2.add(rotation2Label);
rotationField = new JTextField(); rotationField = new JTextField();
@@ -173,15 +173,15 @@ public class PropertiesPanel extends JPanel {
rotationField.addKeyListener(new KeyListener() { rotationField.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) { public void keyTyped(KeyEvent e) {
try{ try{
rotationField.setBackground(Color.WHITE); rotationField.setBackground(Color.WHITE);
int rotation = Integer.parseInt(rotationField.getText()) % 360; int rotation = Integer.parseInt(rotationField.getText()) % 360;
rotationField.setText(rotation +""); rotationField.setText(rotation +"");
selectedObject.setRotation(rotation); selectedObject.setRotation(rotation);
p.update(); p.update();
} catch (NumberFormatException nfe) { } catch (NumberFormatException nfe) {
rotationField.setBackground(Color.RED); rotationField.setBackground(Color.RED);
} }
} }
public void keyReleased(KeyEvent e) {} public void keyReleased(KeyEvent e) {}
public void keyPressed(KeyEvent e) {} public void keyPressed(KeyEvent e) {}
@@ -189,14 +189,14 @@ public class PropertiesPanel extends JPanel {
} }
rotationPanel2.add(rotationField); rotationPanel2.add(rotationField);
} }
//SPACER //SPACER
JPanel spacerPanel = new JPanel(); JPanel spacerPanel = new JPanel();
spacerPanel.setPreferredSize(new Dimension(200, 50)); spacerPanel.setPreferredSize(new Dimension(200, 50));
spacerPanel.setMaximumSize(new Dimension(200, 50)); spacerPanel.setMaximumSize(new Dimension(200, 50));
spacerPanel.setBackground(new Color(220, 220, 220)); spacerPanel.setBackground(new Color(220, 220, 220));
add(spacerPanel); add(spacerPanel);
//EXAMPLE //EXAMPLE
JPanel examplePanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); JPanel examplePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JLabel exampleLabel = new JLabel("Example"); JLabel exampleLabel = new JLabel("Example");
@@ -205,58 +205,62 @@ public class PropertiesPanel extends JPanel {
examplePanel.setMaximumSize(new Dimension(200, 100)); examplePanel.setMaximumSize(new Dimension(200, 100));
examplePanel.add(exampleLabel); examplePanel.add(exampleLabel);
add(examplePanel); add(examplePanel);
enableComponents(this, false); enableComponents(this, false);
} }
public void setSelected(DrawObject drOb) public void setSelected(DrawObject drOb)
{ {
selectedObject = drOb; selectedObject = drOb;
fillFields(); fillFields();
enableComponents(this, true); enableComponents(this, true);
} }
public void clearSelected() public void clearSelected()
{ {
enableComponents(this, false); enableComponents(this, false);
clearFields(this); clearFields(this);
selectedObject = null; selectedObject = null;
} }
public void update() { public void update() {
fillFields(); fillFields();
} }
private void enableComponents(Container container, boolean enable) { private void enableComponents(Container container, boolean enable) {
Component[] components = container.getComponents(); Component[] components = container.getComponents();
for (Component component : components) { for (Component component : components) {
component.setEnabled(enable); component.setEnabled(enable);
if (component instanceof Container) { if (component instanceof Container) {
enableComponents((Container)component, enable); enableComponents((Container)component, enable);
} }
} }
} }
private void clearFields(Container container) { private void clearFields(Container container) {
Component[] components = container.getComponents(); Component[] components = container.getComponents();
for (Component component : components) { for (Component component : components) {
if (component instanceof JTextField) { if (component instanceof JTextField) {
((JTextField) component).setText(""); ((JTextField) component).setText("");
} }
if (component instanceof Container) { if (component instanceof Container) {
clearFields((Container)component); clearFields((Container)component);
} }
} }
} }
private void fillFields() { private void fillFields() {
locationXField.setText(Math.round(selectedObject.getPosition().getX()) + ""); if(selectedObject != null)
locationYField.setText(Math.round(selectedObject.getPosition().getY()) + ""); {
scaleField.setText((double)Math.round(selectedObject.getScale()*10)/10 + ""); locationXField.setText(Math.round(selectedObject.getPosition().getX()) + "");
rotationField.setText(Math.round(selectedObject.getRotation()%360) + ""); locationYField.setText(Math.round(selectedObject.getPosition().getY()) + "");
} scaleField.setText((double)Math.round(selectedObject.getScale()*10)/10 + "");
rotationField.setText(Math.round(selectedObject.getRotation()%360) + "");
}
}
public void setPanel(Panel p) public void setPanel(Panel p)
{ {
this.p = p; this.p = p;