First of all, here is my code:
MyControlPanel.java
package calculations; import java.awt.*; import javax.swing.*; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class MyControlPanel extends javax.swing.JPanel { private MyShape theShape; static int min = 0; static int max = 100; static int initial = 0; JSlider jSlider; JLabel jLabel1; JLabel jLabel2; JLabel jLabel3; JTextField jText1; JTextField jText2; public MyControlPanel() { initComponents(); jSlider = new JSlider(JSlider.HORIZONTAL, min, max, initial); jSlider.setMajorTickSpacing(10); jSlider.setMinorTickSpacing(1); jSlider.setPaintLabels(true); jSlider.setPaintTicks(true); jSlider.setValue(70); jSlider.addChangeListener(new MyChangeAction()); jText1 = new JTextField("Displaying JSlider value"); jText1 = new JTextField(); jText2 = new JTextField(); jText1.setText(""); jText2.setText(""); jLabel1 = new JLabel(); jLabel2 = new JLabel(); jLabel3 = new JLabel(); jLabel1.setText("Shape Dimension"); jLabel2.setText("Boundary Length = "); jLabel3.setText("Area = "); this.add(jSlider) ; this.add(jLabel1) ; this.add(jLabel2) ; this.add(jText1) ; this.add(jLabel3) ; this.add(jText2) ; setLayout(new FlowLayout()); } public void setShape(MyShape suppliedShape) { theShape = suppliedShape; } public class MyChangeAction implements ChangeListener { @Override public void stateChanged(ChangeEvent e) { double sliderValue = jSlider.getValue(); double area; double boundaryLength; String str = Double.toString(sliderValue); jLabel1.setText(str); area = theShape.getArea(sliderValue); boundaryLength = theShape.getBoundaryLength(sliderValue); jText1.setText(Double.toString(Math.round(boundaryLength))); jText2.setText(Double.toString(Math.round(area))); } } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 400, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE) ); }// </editor-fold> // Variables declaration - do not modify // End of variables declaration }
MyFrame.java
package calculations; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class MyFrame extends javax.swing.JFrame implements ActionListener { public MyShape myShape; JMenuItem square = new JMenuItem(); JMenuItem triangle = new JMenuItem(); JMenuItem circle = new JMenuItem(); MyControlPanel theControlPanel = new MyControlPanel(); public MyFrame() { initComponents(); JMenuBar jMenuBar = new JMenuBar(); JMenu Shape = new JMenu(); Shape.setText("Shape"); square.addActionListener(new MySquareAction()); triangle.addActionListener(new MyTriangleAction()); circle.addActionListener(new MyCircleAction()); square.setText("Square"); triangle.setText("Triangle"); circle.setText("Circle"); jMenuBar.add(Shape); Shape.add(square); Shape.add(triangle); Shape.add(circle); setJMenuBar(jMenuBar); MyControlPanel controlPanel = new MyControlPanel(); setLayout(new FlowLayout()); this.add(controlPanel); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 400, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE) ); pack(); }// </editor-fold> public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url] */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { new MyFrame().setVisible(true); } }); } // Variables declaration - do not modify // End of variables declaration @Override public void actionPerformed(ActionEvent e) { } public class MySquareAction implements ActionListener { @Override public void actionPerformed(ActionEvent e) { myShape = new Square(); theControlPanel.setShape(myShape); } } public class MyTriangleAction implements ActionListener { @Override public void actionPerformed(ActionEvent e) { myShape = new Triangle(); theControlPanel.setShape(myShape); } } public class MyCircleAction implements ActionListener { @Override public void actionPerformed(ActionEvent e) { myShape = new Circle(); theControlPanel.setShape(myShape); } } }
If you want you can download my whole program here: http://www.filedropper.com/calculations
Sorry If download links aren't allowed, ill remove it straight away if so.
When i run the program and move the slider it is meant to calculate the boundary length and area, of the chosen shape, which is chosen using the menuItems(Circle, Square, Triangle).
When i move the slider, I get a this error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at calculations.MyControlPanel$MyChangeAction.stateChanged(MyControlPanel.java:79) at javax.swing.JSlider.fireStateChanged(JSlider.java:432) at javax.swing.JSlider$ModelListener.stateChanged(JSlider.java:350) at javax.swing.DefaultBoundedRangeModel.fireStateChanged(DefaultBoundedRangeModel.java:364) at javax.swing.DefaultBoundedRangeModel.setRangeProperties(DefaultBoundedRangeModel.java:302) at javax.swing.DefaultBoundedRangeModel.setValueIsAdjusting(DefaultBoundedRangeModel.java:231) at javax.swing.JSlider.setValueIsAdjusting(JSlider.java:652) at javax.swing.plaf.basic.BasicSliderUI$TrackListener.mousePressed(BasicSliderUI.java:1632) at javax.swing.plaf.synth.SynthSliderUI$SynthTrackListener.mousePressed(SynthSliderUI.java:924) at java.awt.Component.processMouseEvent(Component.java:6502) at javax.swing.JComponent.processMouseEvent(JComponent.java:3320) at java.awt.Component.processEvent(Component.java:6270) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4861) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4489) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Window.dispatchEventImpl(Window.java:2719) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:694) at java.awt.EventQueue$3.run(EventQueue.java:692) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:708) at java.awt.EventQueue$4.run(EventQueue.java:706) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:705) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Can anyone help me with this ? Probably easier if you download my program..
Thanks in advance!