REAL

//BACKUP package controlPI; import import import import import import import import import import import import java.ap

Views 183 Downloads 0 File size 257KB

Report DMCA / Copyright

DOWNLOAD FILE

Recommend stories

Citation preview

//BACKUP package controlPI; import import import import import import import import import import import import

java.applet.Applet; java.awt.*; java.awt.geom.Arc2D; java.awt.image.BufferedImage; java.io.*; java.util.ArrayList; java.util.Timer; java.util.TimerTask; java.util.Date; java.text.SimpleDateFormat; java.net.*; java.lang.*;

import javax.imageio.ImageIO; import javax.swing.*;

public class controller extends JApplet { public static ArrayList output_data = new ArrayList(); public static int T = 3; public static int T_log = 6; public static double ref=0.0; public String host, miInURL,miOutURL; public java.net.URL myServer = null; public java.net.URLConnection myConx = null; final int START = 1; // Starts operation (timer) final int STOP = 2; // Stops operation (timer) //final int IN = 3; final int SP = 4; final int PB = 5; final int RESET = 6; double level = 0.0; // Must be global double valve_p = 0.0; double sp = 0.0; double pb = 0.0; double reset = 0.0; private static final long serialVersionUID = 1L; Timer timer = new Timer("ReadData"); public static PI controlador = new PI(); public static Planta planta = new Planta(); public static Log historial = new Log();

public static javax.swing.JTextArea Hist = new javax.swing.JTextArea(23, 60); Image I; static Label l1=new Label("SP"),l2=new Label("OUT"),l3=new Label("IN"); public void init(){ Toolkit tk = Toolkit.getDefaultToolkit(); int xs = (int) tk.getScreenSize().getWidth() - 200; int ys = (int) tk.getScreenSize().getHeight() 200; System.out.println(xs + " " + ys); setSize(xs,ys); URL base; base = controller.class.getResource("/controlPI/image.jpg");

I=getImage(controller.class.getResource("image.jpg")); BackGroundPanel bgp = new BackGroundPanel(); bgp.setLayout(null); bgp.setBackGroundImage(I); bgp.setBorder(javax.swing.BorderFactory.createTitledBorder( "Image1"));

javax.swing.JPanel panelControl = new javax.swing.JPanel(); panelControl.setBorder(javax.swing.BorderFactory.createTitl edBorder("ControlPanel")); panelControl.setLayout( new FlowLayout()); javax.swing.JButton startButton = new javax.swing.JButton("Start"); TextField AAA = new TextField("AAASSSSSSSSSSSSSSSSSAA"); javax.swing.JButton stopButton = new javax.swing.JButton("Stop"); javax.swing.JLabel pidLabel = new

javax.swing.JLabel(" || PID:"); javax.swing.JButton pbButton = new javax.swing.JButton("PB"); panelControl.add(startButton); panelControl.add(stopButton);

panelControl.add(pidLabel); panelControl.add(pbButton); startButton.addActionListener(new controller.ControlPanel(START)); stopButton.addActionListener(new controller.ControlPanel(STOP)); pbButton.addActionListener(new controller.ControlPanel(PB)); java.awt.Container contenedor = getContentPane(); contenedor.setLayout(new GridLayout(2,2)); contenedor.add(panelControl); bgp.add(l1); bgp.add(l2); bgp.add(l3); l1.setLocation(678, 58); l2.setLocation(675,92); l3.setLocation(583,132); l1.setSize(40,18); l2.setSize(40,18); l3.setSize(40,18); contenedor.add(bgp); javax.swing.JPanel panelHist = new javax.swing.JPanel(); panelHist.setBorder(javax.swing.BorderFactory.createTitledB order("Historial")); panelHist.setLayout( new FlowLayout()); Hist.setEditable(false); Hist.setMargin(new Insets(5,5,5,5)); JScrollPane logScrollPane = new JScrollPane(Hist); panelHist.add(logScrollPane); contenedor.add( panelHist);

}

// Class to handle events on the control panel class ControlPanel implements java.awt.event.ActionListener { FilePermission filePermission = new FilePermission("C:/Users/pc/Desktop/controlPI/output.txt", "write"); int buttonID;

final double outs[] = {0.0, 0.0, 0.0}; public ControlPanel(int buttonID) { this.buttonID = buttonID; } public void actionPerformed(java.awt.event.ActionEvent actionEvent) { switch (buttonID) { case START: // The name of the file to open. String fileNameIn = "input.txt"; //InputStreamReader isr = ; // This will reference one line at a time String sp = null; try { // FileReader reads text files in the default encoding. /*FileReader fileReaderIn = new FileReader(fileNameIn);*/ InputStream is = ControlPanel.class.getResourceAsStream(fileNameIn); // Always wrap FileReader in BufferedReader. BufferedReader bufferedReaderIn = new BufferedReader(new InputStreamReader(is)); //String sp;

//line = sp; ref = 0.0; if((sp = bufferedReaderIn.readLine())!=null) ref = Double.valueOf(sp); System.out.format("\nSet point: %.2f\n",ref); Hist.append(String.format("\nSet point: %.2f\n",ref)); // Always close files. bufferedReaderIn.close(); } catch(FileNotFoundException ex) { System.out.println( "Unable to open file '" + fileNameIn + "'"); } catch(IOException ex) { System.out.println( "Error reading file '" + fileNameIn + "'"); // Or we could just do this: // ex.printStackTrace(); }

// tareas controlador = new PI(); planta = new Planta(); historial = new Log(); timer.schedule(planta, 0, T*1000); timer.schedule(controlador, 0, T*1000); timer.schedule(historial, 0, T_log*1000);

break;

case STOP: if ( planta.cancel() && controlador.cancel() && historial.cancel() ) { System.out.println("The Monitoring Process Timer was stopped!"); Hist.append("The Monitoring Process Timer was stopped!"); timer = new Timer("ReadData"); }else{ System.out.println("The Monitoring Process Timer was already stopped!"); Hist.append("The Monitoring Process Timer was already stopped!"); } break; case PB: String fileNameOut = "output.txt"; try { // Assume default encoding. File file = new File("C:\\Users\\pc\\Desktop\\controlPI\\output.txt"); // creates the file file.createNewFile(); // creates a FileWriter Object FileWriter writer = new FileWriter(file); // Writes the content to the file

// Always wrap FileWriter in BufferedWriter. // Note that write() does not automatically // append a newline character. for(int i=0;i