Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
package ohmslaw;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;
        

public class ohmslaw extends JApplet implements ActionListener {
    
    double  Volt, Amp, Ohm,Volt2,Amp2,Ohm2;
    JLabel Voltage,
           Current,
           Resistance,
           Voltage2,
           Current2,
           Resistance2;
          
    JTextField volt,amp,ohm,volt2,amp2,ohm2;
    JButton compute, plot;

    
    public void init() 
    {
      
      Container c = getContentPane();
      c.setLayout(new FlowLayout());  
          
      Voltage = new JLabel("Voltage");     
      c.add(Voltage);  
      volt = new JTextField(8);
      c.add(volt); 
      
      Current = new JLabel("Current");     
      c.add(Current);  
      amp = new JTextField(8);
      c.add(amp);
      
      Resistance = new JLabel("Resistance");     
      c.add(Resistance);  
     
      ohm = new JTextField (8);
      ohm.setEditable (false);
      c.add(ohm);
      
      
      Voltage2 = new JLabel("Voltage2");     
      c.add(Voltage2);  
      volt2 = new JTextField(8);
      c.add(volt2); 
      
      Current2 = new JLabel("Current2");     
      c.add(Current2);  
      amp2 = new JTextField(8);
      c.add(amp2);
      
      Resistance2 = new JLabel("Resistance2");     
      c.add(Resistance2);  
     
      ohm2 = new JTextField (8);
      ohm2.setEditable (false);
      c.add(ohm2);
      
      compute = new JButton("Compute");
      compute.addActionListener (this);
      c.add(compute);
  
      plot = new JButton("Plot");
      plot.addActionListener (this);
      c.add(plot);
      
    }
    
        public void actionPerformed (ActionEvent e)
        {       
            Volt = Double.parseDouble(volt.getText());
            Amp = Double.parseDouble(amp.getText());
            Ohm = computeOhm(Volt,Amp);
            ohm.setText(Double.toString(Ohm));
            Volt2 = Double.parseDouble(volt2.getText());
            Amp2 = Double.parseDouble(amp2.getText());
            Ohm2 = computeOhm2(Volt2,Amp2);
            ohm2.setText(Double.toString(Ohm2));
            
        }
        public double computeOhm(double Volt, double Amp)
        {
            double dohm = (Volt/Amp);
            return dohm;        
        }         
         
        public double computeOhm2(double Volt2, double Amp2)
        {
            double dohm2 = (Volt2/Amp2);
            return dohm2;        
        }
    
        public class trial extends Application {
 
        @Override public void start(Stage stage) {
            stage.setTitle("Ohm's Law");
            //defining the axes
            final NumberAxis xAxis = new NumberAxis();
            final NumberAxis yAxis = new NumberAxis();
            xAxis.setLabel("Voltage");
            yAxis.setLabel("Current");
            //creating the chart
            final LineChart<number,number> lineChart = 
                    new LineChart<number,number>(xAxis,yAxis);
                
            lineChart.setTitle("Resistance Chart");
            //defining a series
            XYChart.Series series = new XYChart.Series();
            series.setName("Voltage vs. Current Graph");
            //populating the series with data
            series.getData().add(new XYChart.Data(volt, amp));
            series.getData().add(new XYChart.Data(volt2, amp2));
        
            Scene scene  = new Scene(lineChart,800,600);
            lineChart.getData().add(series);
       
            stage.setScene(scene);
            stage.show();
        }
 
        public void plotmain(String[] args) {
            launch(args);
        }
    }    
}
Posted
Updated 2-Jul-14 1:02am
v3

1 solution

First off, I see that the Plot button is attached to the same ActionListener as the Compute button.

Secondly, and most importantly, displaying of JavaFX components from a Java applet or application requires some Java Swing JavaFX "magic". I simply supply a simple solution to get you going (as opposed to explaining every embedding step).

Java
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

@SuppressWarnings("serial")
public class SimpleChart extends JApplet {
    
    double Volt, Amp, Ohm, Volt2, Amp2, Ohm2;
    JLabel Voltage, Current, Resistance, Voltage2, Current2, Resistance2;
    
    JTextField volt, amp, ohm, volt2, amp2, ohm2;
    JButton compute, plot;
    
    private final JFrame frame = new JFrame("Graph");
    private final JFXPanel jfxPanel = new JFXPanel();
    
    private LineChart<Number, Number> lineChart;
    
    public double computeOhm(double Volt, double Amp) {
        double dohm = (Volt / Amp);
        return dohm;
    }
    
    public double computeOhm2(double Volt2, double Amp2) {
        double dohm2 = (Volt2 / Amp2);
        return dohm2;
    }
    
    private void createScene() {
        
        Platform.runLater(new Runnable() {
            
            @Override
            public void run() {
                
                // defining the axes
                final NumberAxis xAxis = new NumberAxis();
                final NumberAxis yAxis = new NumberAxis();
                xAxis.setLabel("Voltage");
                yAxis.setLabel("Current");
                
                // creating the chart
                lineChart = new LineChart<Number, Number>(xAxis, yAxis);
                lineChart.setTitle("Resistance Chart");
                
                // defining a series
                XYChart.Series<Number, Number> series = new XYChart.Series<Number, Number>();
                series.setName("Voltage vs. Current Graph");
                
                // populating the series with data
                lineChart.getData().add(series);
                
                // Java Swing embedding JavaFX
                jfxPanel.setScene(new Scene(lineChart));
            }
        });
    }
    
    public void init() {
        
        createScene();
        
        Container c = getContentPane();
        c.setLayout(new FlowLayout());
        
        Voltage = new JLabel("Voltage");
        c.add(Voltage);
        volt = new JTextField(8);
        c.add(volt);
        
        Current = new JLabel("Current");
        c.add(Current);
        amp = new JTextField(8);
        c.add(amp);
        
        Resistance = new JLabel("Resistance");
        c.add(Resistance);
        
        ohm = new JTextField(8);
        ohm.setEditable(false);
        c.add(ohm);
        
        Voltage2 = new JLabel("Voltage2");
        c.add(Voltage2);
        volt2 = new JTextField(8);
        c.add(volt2);
        
        Current2 = new JLabel("Current2");
        c.add(Current2);
        amp2 = new JTextField(8);
        c.add(amp2);
        
        Resistance2 = new JLabel("Resistance2");
        c.add(Resistance2);
        
        ohm2 = new JTextField(8);
        ohm2.setEditable(false);
        c.add(ohm2);
        
        compute = new JButton("Compute");
        compute.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                Volt = Double.parseDouble(volt.getText());
                Amp = Double.parseDouble(amp.getText());
                Ohm = computeOhm(Volt, Amp);
                ohm.setText(Double.toString(Ohm));
                Volt2 = Double.parseDouble(volt2.getText());
                Amp2 = Double.parseDouble(amp2.getText());
                Ohm2 = computeOhm2(Volt2, Amp2);
                ohm2.setText(Double.toString(Ohm2));
            }
        });
        c.add(compute);
        
        this.plot = new JButton("Plot");
        this.plot.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                
                Platform.runLater(new Runnable() {
                    
                    @Override
                    public void run() {
                        // get and clear the series
                        XYChart.Series<Number, Number> series = lineChart.getData().get(0);
                        series.getData().clear();
                        
                        // populating the series with data
                        series.getData().add(new XYChart.Data<Number, Number>(Volt, Amp));
                        series.getData().add(new XYChart.Data<Number, Number>(Volt2, Amp2));
                        
                        // display the frame
                        frame.setVisible(true);
                    }
                });
            }
        });
        
        c.add(plot);
        
        frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
        frame.getContentPane().add(jfxPanel, BorderLayout.CENTER);
        frame.setPreferredSize(new Dimension(400, 400));
        frame.pack();
        
        setPreferredSize(new Dimension(1024, 600));
    }
}


I hope this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900