Java tutorial
/* ----------------------------------------------------------------- @file CarApplet.java @author Juan Hernandez Garcia ----------------------------------------------------------------- Copyright (C) 2014 Modesto Modesto T Lopez-Lopez Francisco Nogueras Lara Juan Hernandez Garcia University of Granada -------------------------------------------------------------------- This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.juanhg.car; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.awt.Stroke; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.Point2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSlider; import javax.swing.LayoutStyle.ComponentPlacement; import javax.swing.border.BevelBorder; import javax.swing.border.CompoundBorder; import javax.swing.border.EtchedBorder; import javax.swing.border.LineBorder; import javax.swing.border.MatteBorder; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import org.jfree.chart.annotations.XYAnnotation; import com.juanhg.util.Time; import com.raccoon.easyjchart.Grafica; import com.raccoon.easyjchart.JPanelGrafica; public class CarApplet extends JApplet implements Runnable { private static final long serialVersionUID = -3017107307819023599L; private final String road = "road.png"; private final String car = "car.png"; private final String piston = "piston.png"; private final String head = "head.png"; final int pistonY0 = 3; final int pistonY1 = 7; //Control variables long sleepTime = 30; boolean end = false; Time time = new Time(); //Inputs double Va, r, mg, Vcar, a, Qc; //Thread that executed the simulation private Thread flujo = null; //Model private CarModel model; //Charts private Grafica chartPlot, chartPiston, chartCar; //Panels private JPanelGrafica panelPlot, panelMotor, panelCar; int supXLimit = 10; int infXLimit = 0; int supYLimit = 10; int infYLimit = 0; int supPistonXLimit = 8; int infPistonXLimit = 0; int supPistonYLimit = 15; int infPistonYLimit = -2; int supRoadXLimit = 2000; int infRoadXLimit = 0; int supRoadYLimit = 10; int infRoadYLimit = 0; //Images BufferedImage roadImage, carImage, pistonImage, headImage; //Annotations XYAnnotation exampleAnnotation = null; XYAnnotation carAnnotation = null; XYAnnotation headAnnotation = null; XYAnnotation lineAnnotation = null; XYAnnotation pistonBodyAnnotation = null; XYAnnotation liquidAnnotation = null; //Labels private JLabel lblWcValue; private JLabel lblRValue, lblMgValue, lblVaValue, lblNcValue, lblVcarValue; private JLabel lblWc, lblQcValue, lblTmaxValue, lblO3, lblVValue, lblPValue, lblEtaValue, lblXValue, lblFValue; //Sliders private JSlider sliderVa, sliderR, sliderMg, sliderVcar, sliderQc; //Buttons JButton btnLaunchSimulation; public CarApplet() { } public void init() { try { java.awt.EventQueue.invokeAndWait(new Runnable() { public void run() { initComponents(); } }); } catch (Exception ex) { ex.printStackTrace(); } repaint(); } public void initComponents() { setSize(1040, 590); this.autogeneratedCode(); //Obtain values from interface this.readInputs(); this.initSimulation(); } private void sliderI1Event() { //350 - 450 if (sliderVa.getValueIsAdjusting()) { lblVaValue.setText(Integer.toString(sliderVa.getValue())); } } private void sliderI2Event() { //270-330 if (sliderR.getValueIsAdjusting()) { double staticF; staticF = (double) sliderR.getValue(); lblRValue.setText("" + staticF); } } private void sliderI3Event() { //1-5 double dynamicF; if (sliderMg.getValueIsAdjusting()) { dynamicF = (double) sliderMg.getValue(); lblMgValue.setText("" + dynamicF); } } private void sliderI4Event() { //6-10 double dynamicF; if (sliderVcar.getValueIsAdjusting()) { dynamicF = (double) sliderVcar.getValue(); lblVcarValue.setText("" + dynamicF); } } private void sliderI5Event() { double dynamicF; if (sliderQc.getValueIsAdjusting()) { dynamicF = (double) sliderQc.getValue(); lblQcValue.setText("" + dynamicF); Qc = dynamicF; } } void btnLaunchSimulationEvent(ActionEvent event) { boolean buttonsOn = false; if (flujo != null && flujo.isAlive()) { end = true; buttonsOn = true; while (flujo.isAlive()) { } this.readInputs(); this.initSimulation(); btnLaunchSimulation.setText("Iniciar"); sliderVa.setEnabled(buttonsOn); sliderMg.setEnabled(buttonsOn); sliderVcar.setEnabled(buttonsOn); sliderR.setEnabled(buttonsOn); sliderQc.setEnabled(buttonsOn); repaint(); } else { buttonsOn = false; btnLaunchSimulation.setText("Finalizar"); //Obtain values from interface this.readInputs(); this.initSimulation(); //Initializes and runs the thread (Run()) flujo = new Thread(); flujo = new Thread(this); sliderVa.setEnabled(buttonsOn); sliderMg.setEnabled(buttonsOn); sliderVcar.setEnabled(buttonsOn); sliderR.setEnabled(buttonsOn); sliderQc.setEnabled(buttonsOn); time = new Time(); time.start(); flujo.start(); } } @Override public void run() { int precision = 8; float grosor = 2f; end = false; lblWcValue.setText(dToString(model.getWc(), precision)); lblNcValue.setText(dToString(model.getNciclos(), precision)); lblFValue.setText(dToString(model.getF(), precision)); lblXValue.setText(dToString(model.getX() / 1000.0, precision + 1)); lblEtaValue.setText(dToString(model.getEta(), precision)); lblTmaxValue.setText(dToString(model.getTmax(), precision)); while (!end) { model.simulate(); chartPlot.replacePlot(0, model.currentPointAsArray(), "", Color.RED, grosor * 2, false); chartPlot.visualizaMuestras(0, true, grosor * 2); chartPlot.replacePlot(1, model.chart1AsArray(), "", Color.BLUE, grosor, true); chartPlot.replacePlot(2, model.chart2AsArray(), "", Color.BLUE, grosor, true); chartPlot.replacePlot(3, model.chart3AsArray(), "", Color.BLUE, grosor, true); chartPlot.replacePlot(4, model.chart4AsArray(), "", Color.BLUE, grosor, true); lblPValue.setText(dToString(model.getP(), precision + 2)); lblVValue.setText(dToString(model.getV(), precision)); updateCar(); updatePiston(); this.updatePanels(); repaint(); try { Thread.sleep(sleepTime); } catch (InterruptedException ex) { Logger.getLogger(CarApplet.class.getName()).log(Level.SEVERE, null, ex); } } } public String dToString(double value, int precision) { String x = String.valueOf(value); if (x.length() > precision) { return (x.substring(0, precision)); } else { return x; } } /** * Read the input values from the interface and loads its * in the variable of the class */ private void readInputs() { Va = sliderVa.getValue(); r = sliderR.getValue(); mg = sliderMg.getValue(); Vcar = sliderVcar.getValue(); Qc = sliderQc.getValue(); } //Init the elements of the simulation private void initSimulation() { float grosor = 2f; Point2D[] nullArray = new Point2D[0]; //Crear modelo model = new CarModel(Va, r, mg, Vcar, Qc); supRoadXLimit = (int) model.getX() + (int) (model.getX() * 0.1); // Inicializar charts chartPlot = new Grafica(nullArray, "", "", "V", "P", false, Color.BLUE, grosor, true); chartPlot.agregarGrafica(model.chart1AsArray(), "", Color.BLUE, grosor, true); chartPlot.agregarGrafica(model.chart2AsArray(), "", Color.BLUE, grosor, true); chartPlot.agregarGrafica(model.chart3AsArray(), "", Color.BLUE, grosor, true); chartPlot.agregarGrafica(model.chart4AsArray(), "", Color.BLUE, grosor, true); chartPlot.setRangeAxis((model.getV2() - model.getV1() * 0.1), (model.getV1() + model.getV1() * 0.1), model.getPa() - model.getPmax() * 0.1, model.getPmax() + model.getPmax() * 0.1); chartPiston = new Grafica(nullArray, "", "", "", "", false, Color.BLUE, grosor, true); chartPiston.setRangeAxis(infPistonXLimit, supPistonXLimit, infPistonYLimit, supPistonYLimit); // chartPiston.fijaFondo(Color.WHITE); chartPiston.setAxisVisible(false); chartCar = new Grafica(nullArray, "", "", "", "", false, Color.BLUE, grosor, true); chartCar.setRangeAxis(infRoadXLimit, supRoadXLimit, infRoadYLimit, supRoadYLimit); //Load Images roadImage = loadImage(road); carImage = loadImage(car); pistonImage = loadImage(piston); headImage = loadImage(head); //Set Images chartCar.setImageAtPoint(roadImage, (supRoadXLimit - infRoadXLimit) / 2.0, (supRoadYLimit - infRoadYLimit) / 2.0); carAnnotation = chartCar.setImageAtPoint(carImage, (supRoadXLimit - infRoadXLimit) / 2.0, (supRoadYLimit - infRoadYLimit) / 2.0); pistonBodyAnnotation = chartPiston.setImageAtPoint(pistonImage, (supPistonXLimit - infPistonXLimit) / 2.0, 3); lineAnnotation = chartPiston.drawBox(((supPistonXLimit - infPistonXLimit) / 2.0) - 0.1, -4, ((supPistonXLimit - infPistonXLimit) / 2.0) + 0.1, 7, new BasicStroke(2f), Color.BLACK, Color.GRAY); headAnnotation = chartPiston.setImageAtPoint(headImage, (supPistonXLimit - infPistonXLimit) / 2.0, 6.5); //Actualize panels updatePanels(); repaint(); } public void initPiston() { double infXLimit = 3; double supXLimit = 5; double infYLimit = 0; double supYLimit = 10; Stroke stroke = new BasicStroke(2f); chartPiston.drawLine(infXLimit, infYLimit, infXLimit, supYLimit, stroke, Color.BLACK); chartPiston.drawLine(supXLimit, infYLimit, supXLimit, supYLimit, stroke, Color.BLACK); chartPiston.drawLine(infXLimit, infYLimit, infXLimit, supYLimit, stroke, Color.BLACK); } /** * Load a image in the specified path * @param fileName Absolute or relative path to the image * @return BufferedImage that contains the image */ public BufferedImage loadImage(String fileName) { BufferedImage buff = null; try { buff = ImageIO.read(getClass().getResourceAsStream(fileName)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } return buff; } int obtainExponent(double number) { int exponent = 0; while (0 == (int) number) { exponent--; number *= 10; } return exponent; } private void updateCar() { double xfinal = model.getX(); double tFinal = 10; double currentX = 0; time.pause(); double currentTime = (double) time.getTime() / 1000.0; if (currentTime <= tFinal) { chartCar.deleteAnnotation(carAnnotation); currentX = normalize(currentTime, 0, 10, 0, xfinal); carAnnotation = chartCar.setImageAtPoint(carImage, currentX, (supRoadYLimit - infRoadYLimit) / 2.0); } time.start(); System.out.println(tFinal); } private void updatePiston() { Color liquidColor; if (model.getCurrentPhase() <= 2) { liquidColor = new Color(100, 180, 255, 150); } else { liquidColor = new Color(255, 100, 100, 150); } double currentH = normalize(model.getH(), model.getHMin(), model.getHMax(), 0.5, 6.5); currentH = 7 - currentH; chartPiston.deleteAnnotation(liquidAnnotation); liquidAnnotation = chartPiston.drawBox(2.8, currentH - 0.2, 5, 7.5, null, null, liquidColor); chartPiston.deleteAnnotation(pistonBodyAnnotation); pistonBodyAnnotation = chartPiston.setImageAtPoint(pistonImage, (supPistonXLimit - infPistonXLimit) / 2.0, 3); chartPiston.deleteAnnotation(lineAnnotation); lineAnnotation = chartPiston.drawBox(((supPistonXLimit - infPistonXLimit) / 2.0) - 0.1, -4, ((supPistonXLimit - infPistonXLimit) / 2.0) + 0.1, currentH, new BasicStroke(2f), Color.BLACK, Color.GRAY); chartPiston.deleteAnnotation(headAnnotation); headAnnotation = chartPiston.setImageAtPoint(headImage, ((supPistonXLimit - infPistonXLimit) / 2.0) - 0.025, currentH); } private void updatePanels() { panelPlot.actualizaGrafica(chartPlot); panelMotor.actualizaGrafica(chartPiston); panelCar.actualizaGrafica(chartCar); } public double normalize(double x, double a, double b, double c, double d) { return (x * (d - c)) / (b - a) + (c * b - a * d) / (b - a); } private void autogeneratedCode() { JPanel panel_control = new JPanel(); panel_control.setBorder(new CompoundBorder(new EtchedBorder(EtchedBorder.RAISED, null, null), new BevelBorder(BevelBorder.RAISED, null, null, null, null))); JPanel panelInputs = new JPanel(); panelInputs.setToolTipText(""); panelInputs.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0))); JPanel panelOutputs = new JPanel(); panelOutputs.setToolTipText(""); panelOutputs.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0))); JPanel panelTitleOutputs = new JPanel(); panelTitleOutputs.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null)); JLabel labelOutputData = new JLabel("Datos de la Simulaci\u00F3n"); labelOutputData.setFont(new Font("Tahoma", Font.PLAIN, 14)); panelTitleOutputs.add(labelOutputData); lblWc = new JLabel("Wc:"); lblWc.setFont(new Font("Tahoma", Font.PLAIN, 14)); lblWcValue = new JLabel(); lblWcValue.setText("0"); lblWcValue.setFont(new Font("Tahoma", Font.PLAIN, 14)); JLabel lblNc = new JLabel("Ciclos:"); lblNc.setFont(new Font("Tahoma", Font.PLAIN, 14)); lblNcValue = new JLabel(); lblNcValue.setText("0"); lblNcValue.setFont(new Font("Tahoma", Font.PLAIN, 14)); lblO3 = new JLabel("Tmax:"); lblO3.setFont(new Font("Tahoma", Font.PLAIN, 14)); lblTmaxValue = new JLabel(); lblTmaxValue.setText("0"); lblTmaxValue.setFont(new Font("Tahoma", Font.PLAIN, 14)); JLabel lblV = new JLabel("V:"); lblV.setFont(new Font("Tahoma", Font.PLAIN, 14)); JLabel lblP = new JLabel("P:"); lblP.setFont(new Font("Tahoma", Font.PLAIN, 14)); JLabel lblH = new JLabel("Eta:"); lblH.setFont(new Font("Tahoma", Font.PLAIN, 14)); lblEtaValue = new JLabel(); lblEtaValue.setText("0"); lblEtaValue.setFont(new Font("Tahoma", Font.PLAIN, 14)); lblPValue = new JLabel(); lblPValue.setText("0"); lblPValue.setFont(new Font("Tahoma", Font.PLAIN, 14)); lblVValue = new JLabel(); lblVValue.setText("0"); lblVValue.setFont(new Font("Tahoma", Font.PLAIN, 14)); JLabel lblF = new JLabel("F:"); lblF.setFont(new Font("Tahoma", Font.PLAIN, 14)); lblFValue = new JLabel(); lblFValue.setText("0"); lblFValue.setFont(new Font("Tahoma", Font.PLAIN, 14)); JLabel lblDist = new JLabel("Dist:"); lblDist.setFont(new Font("Tahoma", Font.PLAIN, 14)); lblXValue = new JLabel(); lblXValue.setText("0"); lblXValue.setFont(new Font("Tahoma", Font.PLAIN, 14)); GroupLayout gl_panelOutputs = new GroupLayout(panelOutputs); gl_panelOutputs.setHorizontalGroup(gl_panelOutputs.createParallelGroup(Alignment.LEADING) .addGroup(gl_panelOutputs.createSequentialGroup().addGroup(gl_panelOutputs .createParallelGroup(Alignment.LEADING) .addGroup(gl_panelOutputs.createSequentialGroup().addContainerGap().addGroup(gl_panelOutputs .createParallelGroup(Alignment.LEADING) .addComponent(lblNc, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE) .addGroup(gl_panelOutputs.createParallelGroup(Alignment.TRAILING, false) .addComponent(lblWc, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(lblP, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(lblV, GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE))) .addGroup(gl_panelOutputs.createParallelGroup(Alignment.LEADING, false) .addComponent(lblWcValue, GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE) .addComponent(lblNcValue, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(lblVValue, GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE) .addComponent(lblPValue, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(ComponentPlacement.RELATED) .addGroup(gl_panelOutputs.createParallelGroup(Alignment.LEADING) .addGroup(gl_panelOutputs.createSequentialGroup() .addComponent(lblDist, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED).addComponent(lblXValue, GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE)) .addGroup(gl_panelOutputs.createSequentialGroup() .addComponent(lblF, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED).addComponent(lblFValue, GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)) .addGroup(gl_panelOutputs.createSequentialGroup().addComponent(lblH) .addGap(18).addComponent(lblEtaValue, GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)) .addGroup(gl_panelOutputs.createSequentialGroup() .addComponent(lblO3, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(lblTmaxValue, GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE)))) .addComponent(panelTitleOutputs, GroupLayout.PREFERRED_SIZE, 299, GroupLayout.PREFERRED_SIZE)) .addContainerGap())); gl_panelOutputs.setVerticalGroup(gl_panelOutputs.createParallelGroup(Alignment.LEADING) .addGroup(gl_panelOutputs.createSequentialGroup() .addComponent(panelTitleOutputs, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addGap(20) .addGroup(gl_panelOutputs.createParallelGroup(Alignment.LEADING) .addGroup(gl_panelOutputs.createSequentialGroup().addComponent(lblWc) .addPreferredGap(ComponentPlacement.UNRELATED).addComponent(lblNc, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_panelOutputs.createSequentialGroup().addComponent(lblWcValue) .addPreferredGap(ComponentPlacement.UNRELATED).addComponent(lblNcValue, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_panelOutputs.createSequentialGroup() .addGroup(gl_panelOutputs.createParallelGroup(Alignment.BASELINE) .addComponent(lblH, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE) .addComponent(lblEtaValue, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(ComponentPlacement.UNRELATED) .addGroup(gl_panelOutputs.createParallelGroup(Alignment.BASELINE) .addComponent(lblO3, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE) .addComponent(lblTmaxValue, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)))) .addGap(11) .addGroup(gl_panelOutputs.createParallelGroup(Alignment.LEADING) .addComponent(lblV, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE) .addComponent(lblVValue, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE) .addComponent(lblF, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE) .addComponent(lblFValue, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)) .addGap(11) .addGroup(gl_panelOutputs.createParallelGroup(Alignment.LEADING) .addComponent(lblDist, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE) .addGroup(gl_panelOutputs.createParallelGroup(Alignment.BASELINE) .addComponent(lblP, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE) .addComponent(lblPValue, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)) .addComponent(lblXValue, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)) .addContainerGap(30, Short.MAX_VALUE))); panelOutputs.setLayout(gl_panelOutputs); JPanel panelLicense = new JPanel(); panelLicense.setBorder(new LineBorder(new Color(0, 0, 0))); JPanel panel_6 = new JPanel(); panel_6.setBorder(new LineBorder(new Color(0, 0, 0))); GroupLayout gl_panel_control = new GroupLayout(panel_control); gl_panel_control.setHorizontalGroup(gl_panel_control.createParallelGroup(Alignment.TRAILING) .addGroup(gl_panel_control.createSequentialGroup().addContainerGap().addGroup(gl_panel_control .createParallelGroup(Alignment.TRAILING) .addComponent(panelOutputs, GroupLayout.PREFERRED_SIZE, 300, GroupLayout.PREFERRED_SIZE) .addComponent(panelInputs, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 300, Short.MAX_VALUE) .addComponent(panel_6, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE) .addComponent(panelLicense, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)) .addContainerGap())); gl_panel_control.setVerticalGroup(gl_panel_control.createParallelGroup(Alignment.TRAILING) .addGroup(gl_panel_control.createSequentialGroup().addContainerGap() .addComponent(panelInputs, GroupLayout.PREFERRED_SIZE, 211, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(panelOutputs, GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE).addGap(18) .addComponent(panel_6, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED).addComponent(panelLicense, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addGap(24))); btnLaunchSimulation = new JButton("Iniciar"); btnLaunchSimulation.setFont(new Font("Tahoma", Font.PLAIN, 16)); btnLaunchSimulation.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { btnLaunchSimulationEvent(event); } }); GroupLayout gl_panel_6 = new GroupLayout(panel_6); gl_panel_6.setHorizontalGroup(gl_panel_6.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_6.createSequentialGroup().addContainerGap() .addComponent(btnLaunchSimulation, GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE) .addContainerGap())); gl_panel_6.setVerticalGroup(gl_panel_6.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_6 .createSequentialGroup().addContainerGap().addComponent(btnLaunchSimulation, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE) .addContainerGap(69, Short.MAX_VALUE))); panel_6.setLayout(gl_panel_6); JLabel lblNewLabel = new JLabel("GNU GENERAL PUBLIC LICENSE"); panelLicense.add(lblNewLabel); JLabel lblVa = new JLabel("Va"); lblVa.setFont(new Font("Tahoma", Font.PLAIN, 14)); JLabel lblR = new JLabel("R"); lblR.setFont(new Font("Tahoma", Font.PLAIN, 14)); JLabel lblMg = new JLabel("mg"); lblMg.setFont(new Font("Tahoma", Font.PLAIN, 14)); JPanel panelTitle = new JPanel(); panelTitle.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null)); lblRValue = new JLabel("8"); lblRValue.setFont(new Font("Tahoma", Font.PLAIN, 14)); lblMgValue = new JLabel("10"); lblMgValue.setFont(new Font("Tahoma", Font.PLAIN, 14)); lblVaValue = new JLabel("6"); lblVaValue.setFont(new Font("Tahoma", Font.PLAIN, 14)); sliderVa = new JSlider(); sliderVa.setMinimum(5); sliderVa.setMaximum(8); sliderVa.setMinorTickSpacing(1); sliderVa.setValue(6); sliderVa.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent event) { sliderI1Event(); } }); sliderR = new JSlider(); sliderR.setMinimum(7); sliderR.setMaximum(13); sliderR.setMinorTickSpacing(1); sliderR.setValue(8); sliderR.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { sliderI2Event(); } }); sliderMg = new JSlider(); sliderMg.setMinimum(1); sliderMg.setMaximum(20); sliderMg.setValue(10); sliderMg.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { sliderI3Event(); } }); JLabel lblIVcar = new JLabel("Velocidad"); lblIVcar.setFont(new Font("Tahoma", Font.PLAIN, 14)); lblVcarValue = new JLabel("120"); lblVcarValue.setFont(new Font("Tahoma", Font.PLAIN, 14)); sliderVcar = new JSlider(); sliderVcar.setMinimum(1); sliderVcar.setMaximum(160); sliderVcar.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent arg0) { sliderI4Event(); } }); sliderVcar.setValue(120); sliderVcar.setMinorTickSpacing(1); sliderQc = new JSlider(); sliderQc.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent arg0) { sliderI5Event(); } }); sliderQc.setMaximum(1500); sliderQc.setMinimum(500); sliderQc.setValue(600); sliderQc.setMinorTickSpacing(100); lblQcValue = new JLabel("600"); lblQcValue.setFont(new Font("Tahoma", Font.PLAIN, 14)); JLabel lblQc = new JLabel("Qc"); lblQc.setFont(new Font("Tahoma", Font.PLAIN, 14)); GroupLayout gl_panelInputs = new GroupLayout(panelInputs); gl_panelInputs.setHorizontalGroup(gl_panelInputs.createParallelGroup(Alignment.TRAILING) .addGroup(gl_panelInputs.createSequentialGroup().addGap(20).addGroup(gl_panelInputs .createParallelGroup(Alignment.TRAILING) .addGroup(gl_panelInputs.createSequentialGroup() .addComponent(lblQc, GroupLayout.PREFERRED_SIZE, 61, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED, 24, Short.MAX_VALUE) .addComponent(lblQcValue, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE) .addGap(18) .addComponent(sliderQc, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED)) .addGroup(gl_panelInputs.createSequentialGroup().addGroup(gl_panelInputs .createParallelGroup(Alignment.LEADING) .addGroup(gl_panelInputs.createSequentialGroup().addGroup(gl_panelInputs .createParallelGroup(Alignment.TRAILING) .addComponent(lblMg, GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE) .addGroup(gl_panelInputs.createParallelGroup(Alignment.LEADING) .addComponent(lblVa, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE) .addComponent(lblIVcar, Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE))) .addGap(18)) .addGroup(gl_panelInputs.createSequentialGroup() .addComponent(lblR, GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE) .addPreferredGap(ComponentPlacement.RELATED))) .addGroup(gl_panelInputs.createParallelGroup(Alignment.TRAILING) .addGroup(gl_panelInputs.createSequentialGroup() .addComponent(lblVcarValue, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE) .addGap(18) .addComponent(sliderVcar, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED)) .addGroup(gl_panelInputs.createSequentialGroup() .addGroup(gl_panelInputs.createParallelGroup(Alignment.LEADING) .addComponent(lblVaValue, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE) .addComponent(lblRValue, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE) .addComponent(lblMgValue, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE)) .addGap(18).addGroup( gl_panelInputs.createParallelGroup(Alignment.LEADING, false) .addComponent(sliderVa, 0, 0, Short.MAX_VALUE) .addComponent(sliderR, 0, 0, Short.MAX_VALUE) .addComponent(sliderMg, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)))))) .addGap(48)) .addComponent(panelTitle, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)); gl_panelInputs.setVerticalGroup(gl_panelInputs.createParallelGroup(Alignment.LEADING) .addGroup(gl_panelInputs.createSequentialGroup() .addComponent(panelTitle, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addGap(18) .addGroup(gl_panelInputs.createParallelGroup(Alignment.LEADING) .addGroup(gl_panelInputs.createSequentialGroup() .addGroup(gl_panelInputs.createParallelGroup(Alignment.LEADING) .addComponent(lblVaValue, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE) .addComponent( sliderVa, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(ComponentPlacement.RELATED) .addGroup(gl_panelInputs.createParallelGroup(Alignment.LEADING) .addGroup(gl_panelInputs.createParallelGroup(Alignment.BASELINE) .addComponent(lblRValue, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE) .addComponent(lblR)) .addComponent(sliderR, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGap(11) .addGroup(gl_panelInputs.createParallelGroup(Alignment.LEADING) .addGroup(gl_panelInputs.createParallelGroup(Alignment.BASELINE) .addComponent(lblMgValue, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE) .addComponent(lblMg)) .addComponent(sliderMg, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))) .addComponent(lblVa)) .addPreferredGap(ComponentPlacement.RELATED) .addGroup(gl_panelInputs.createParallelGroup(Alignment.TRAILING) .addGroup(gl_panelInputs.createParallelGroup(Alignment.BASELINE) .addComponent(lblVcarValue, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE) .addComponent(lblIVcar, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)) .addComponent(sliderVcar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(ComponentPlacement.RELATED) .addGroup(gl_panelInputs.createParallelGroup(Alignment.TRAILING) .addGroup(gl_panelInputs.createParallelGroup(Alignment.BASELINE) .addComponent(lblQc, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE) .addComponent(lblQcValue, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)) .addComponent(sliderQc, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGap(3))); JLabel lblDatosDeEntrada = new JLabel("Datos de Entrada"); lblDatosDeEntrada.setFont(new Font("Tahoma", Font.PLAIN, 14)); panelTitle.add(lblDatosDeEntrada); panelInputs.setLayout(gl_panelInputs); panel_control.setLayout(gl_panel_control); JPanel panel_visualizar = new JPanel(); panel_visualizar.setBackground(Color.WHITE); GroupLayout groupLayout = new GroupLayout(getContentPane()); groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING) .addGroup(groupLayout.createSequentialGroup().addContainerGap() .addComponent(panel_control, GroupLayout.PREFERRED_SIZE, 325, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(panel_visualizar, GroupLayout.PREFERRED_SIZE, 688, GroupLayout.PREFERRED_SIZE) .addContainerGap(94, Short.MAX_VALUE))); groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.TRAILING) .addGroup(groupLayout.createSequentialGroup().addContainerGap() .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING) .addComponent(panel_visualizar, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 568, Short.MAX_VALUE) .addComponent(panel_control, GroupLayout.PREFERRED_SIZE, 568, GroupLayout.PREFERRED_SIZE)) .addContainerGap())); JPanel panel = new JPanel(); panel.setBackground(Color.WHITE); JPanel panel_1 = new JPanel(); panelPlot = new JPanelGrafica(); GroupLayout gl_panel_1 = new GroupLayout(panel_1); gl_panel_1.setHorizontalGroup( gl_panel_1.createParallelGroup(Alignment.LEADING).addGap(0, 263, Short.MAX_VALUE) .addComponent(panelPlot, GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)); gl_panel_1 .setVerticalGroup(gl_panel_1.createParallelGroup(Alignment.LEADING).addGap(0, 241, Short.MAX_VALUE) .addComponent(panelPlot, GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)); panel_1.setLayout(gl_panel_1); JPanel panel_2 = new JPanel(); panelMotor = new JPanelGrafica(); GroupLayout gl_panel_2 = new GroupLayout(panel_2); gl_panel_2.setHorizontalGroup(gl_panel_2.createParallelGroup(Alignment.LEADING).addComponent(panelMotor, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE)); gl_panel_2.setVerticalGroup(gl_panel_2.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_2.createSequentialGroup() .addComponent(panelMotor, GroupLayout.PREFERRED_SIZE, 241, GroupLayout.PREFERRED_SIZE) .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); panel_2.setLayout(gl_panel_2); GroupLayout gl_panel = new GroupLayout(panel); gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(Alignment.TRAILING) .addGroup(gl_panel.createSequentialGroup().addContainerGap() .addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 258, GroupLayout.PREFERRED_SIZE) .addContainerGap())); gl_panel.setVerticalGroup(gl_panel.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel.createSequentialGroup().addContainerGap() .addGroup(gl_panel.createParallelGroup(Alignment.LEADING) .addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 241, GroupLayout.PREFERRED_SIZE) .addComponent(panel_2, 0, 0, Short.MAX_VALUE)) .addContainerGap())); panel.setLayout(gl_panel); JPanel panel_3 = new JPanel(); panel_3.setBackground(Color.WHITE); JPanel panel_4 = new JPanel(); panelCar = new JPanelGrafica(); GroupLayout gl_panel_4 = new GroupLayout(panel_4); gl_panel_4.setHorizontalGroup(gl_panel_4.createParallelGroup(Alignment.LEADING).addComponent(panelCar, GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)); gl_panel_4.setVerticalGroup(gl_panel_4.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_4.createSequentialGroup() .addComponent(panelCar, GroupLayout.PREFERRED_SIZE, 252, GroupLayout.PREFERRED_SIZE) .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); panel_4.setLayout(gl_panel_4); GroupLayout gl_panel_3 = new GroupLayout(panel_3); gl_panel_3.setHorizontalGroup(gl_panel_3.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_3.createSequentialGroup().addContainerGap() .addComponent(panel_4, GroupLayout.DEFAULT_SIZE, 648, Short.MAX_VALUE).addContainerGap())); gl_panel_3.setVerticalGroup(gl_panel_3.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_3.createSequentialGroup().addContainerGap() .addComponent(panel_4, GroupLayout.PREFERRED_SIZE, 252, Short.MAX_VALUE) .addContainerGap())); panel_3.setLayout(gl_panel_3); GroupLayout gl_panel_visualizar = new GroupLayout(panel_visualizar); gl_panel_visualizar.setHorizontalGroup(gl_panel_visualizar.createParallelGroup(Alignment.LEADING) .addGroup(Alignment.TRAILING, gl_panel_visualizar.createSequentialGroup().addContainerGap() .addGroup(gl_panel_visualizar.createParallelGroup(Alignment.TRAILING) .addComponent(panel_3, GroupLayout.PREFERRED_SIZE, 668, GroupLayout.PREFERRED_SIZE) .addComponent(panel, GroupLayout.DEFAULT_SIZE, 668, Short.MAX_VALUE)) .addContainerGap())); gl_panel_visualizar.setVerticalGroup(gl_panel_visualizar.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_visualizar.createSequentialGroup().addContainerGap() .addComponent(panel, GroupLayout.PREFERRED_SIZE, 261, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.UNRELATED) .addComponent(panel_3, GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE).addContainerGap())); panel_visualizar.setLayout(gl_panel_visualizar); getContentPane().setLayout(groupLayout); } }