package es; import java.io.*; import java.util.*; import javax.imageio.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.swing.*; public class Interfase extends JApplet { private boolean isStandalone = false; ArrayList arrayNiteracio = new ArrayList(); ArrayList arrayResultatLectura = new ArrayList(); PostProcess postpr=new PostProcess(); String strResultat = ""; JPanel jPbase = new JPanel(); BorderLayout borderLayout1 = new BorderLayout(); BorderLayout borderLayout2 = new BorderLayout(); JPanel jPsuperior = new JPanel(); JPanel jPcentral = new JPanel(); JPanel jPinferior = new JPanel(); JLabel jLtitol = new JLabel(); JLabel jLcredits = new JLabel(); String txtAxioma=""; String RewritingRule=""; String strOutputDir=""; BorderLayout borderLayout4 = new BorderLayout(); JTextField txtCentreX = new JTextField(); JTextField txtIteracio = new JTextField(); GridLayout gridLayout4 = new GridLayout(); JPanel jPiteracions = new JPanel(); JLabel jLalt = new JLabel(); JComboBox cTipusLletra = new JComboBox(); JTextField txtEscala = new JTextField(); JLabel jLample = new JLabel(); JLabel jLmidaLletra = new JLabel(); JPanel jPcentreX = new JPanel(); JPanel jP1 = new JPanel(); JPanel jP2 = new JPanel(); JPanel jP3 = new JPanel(); JPanel jPsplitRight = new JPanel(); JLabel jLiteracions = new JLabel(); JSplitPane jSplitPane = new JSplitPane(); JTextField txtCentreY = new JTextField(); JLabel jLescala = new JLabel(); JPanel jPworld = new JPanel(); JPanel jPrwrule = new JPanel(); JLabel jLtipusLletra = new JLabel(); JLabel jLcentreY = new JLabel(); BorderLayout borderLayout3 = new BorderLayout(); JPanel jPsplitLeft = new JPanel(); JTextField txtMidaLletra = new JTextField(); JPanel jPcentreY = new JPanel(); GridLayout gridLayout5 = new GridLayout(); JTextField txtAltura = new JTextField(); JPanel jPescala = new JPanel(); JTextField txtAmple = new JTextField(); JScrollPane jSPworldContainer = new JScrollPane(); JLabel jLcentreX = new JLabel(); Kernel objKernel; boolean isOutOfMEmory=false; JButton bDibuixa = new JButton(); JButton jBchooseDir = new JButton(); //Get a parameter value public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } //Construct the applet public Interfase() { } //Initialize the applet public void init() { RewritingRule="F+F-F-FF+F+F-F"; cTipusLletra.addItem("Dialog"); cTipusLletra.addItem("DialogInput"); cTipusLletra.addItem("Monospaced"); cTipusLletra.addItem("Dialog"); cTipusLletra.addItem("Serif"); cTipusLletra.addItem("SansSerif"); txtAxioma="F+F+F+F"; try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { jPbase.setBackground(Color.white); jPbase.setPreferredSize(new Dimension(800, 600)); jPcentral.setBackground(new Color(0, 201, 87)); jLtitol.setFont(new java.awt.Font("Arial", 1, 20)); jLtitol.setText("insula"); jLcredits.setFont(new java.awt.Font("Dialog", 1, 11)); jLcredits.setText("insula - Desarrollada por Roger Olivella Pujol"); jPsuperior.setBackground(new Color(0, 201, 87)); jPinferior.setBackground(new Color(0, 201, 87)); jPbase.setLayout(borderLayout2); jPcentral.setLayout(borderLayout4); jPcentral.setMaximumSize(new Dimension(32767, 32767)); txtCentreX.setPreferredSize(new Dimension(50, 20)); txtCentreX.setText("500"); txtIteracio.setMinimumSize(new Dimension(20, 20)); txtIteracio.setPreferredSize(new Dimension(22, 20)); txtIteracio.setText("2"); gridLayout4.setColumns(1); gridLayout4.setRows(3); jPiteracions.setBackground(new Color(0, 201, 87)); jLalt.setText("Altura"); jLalt.setVisible(true); txtEscala.setMinimumSize(new Dimension(20, 20)); txtEscala.setPreferredSize(new Dimension(27, 20)); txtEscala.setText("50"); jLample.setText("Ancho"); jLmidaLletra.setText("Tamaño letra"); jPcentreX.setBackground(new Color(0, 201, 87)); jP2.setBackground(new Color(0, 201, 84)); jP1.setLayout(gridLayout5); jP1.setBackground(new Color(0, 201, 84)); jPsplitRight.setLayout(borderLayout3); jPsplitRight.setBackground(Color.white); jLiteracions.setText("Iteraciones"); jSplitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT); jSplitPane.setBackground(new Color(0, 201, 87)); jSplitPane.setEnabled(false); jSplitPane.setDividerSize(2); txtCentreY.setPreferredSize(new Dimension(50, 20)); txtCentreY.setText("500"); jLescala.setLocale(java.util.Locale.getDefault()); jLescala.setText("Escala"); jPworld.setBackground(Color.black); jPrwrule.setBackground(new Color(0, 201, 87)); jLtipusLletra.setLocale(java.util.Locale.getDefault()); jLtipusLletra.setText("Tipo letra"); jLcentreY.setText("CY"); jPsplitLeft.setLayout(gridLayout4); jPsplitLeft.setBackground(new Color(0, 201, 87)); txtMidaLletra.setPreferredSize(new Dimension(27, 20)); txtMidaLletra.setText("10"); jPcentreY.setBackground(new Color(0, 201, 87)); gridLayout5.setColumns(1); gridLayout5.setRows(5); txtAltura.setPreferredSize(new Dimension(50, 20)); txtAltura.setSelectionEnd(3); txtAltura.setText("2000"); jP3.setBackground(new Color(0, 201, 84)); jPescala.setBackground(new Color(0, 201, 87)); txtAmple.setPreferredSize(new Dimension(50, 20)); txtAmple.setText("2000"); txtAmple.setVisible(true); jSPworldContainer.getViewport().setBackground(Color.white); jSPworldContainer.setBorder(null); jSPworldContainer.setMinimumSize(new Dimension(500, 500)); jSPworldContainer.setPreferredSize(new Dimension(500, 500)); jSPworldContainer.setRequestFocusEnabled(true); jLcentreX.setLocale(java.util.Locale.getDefault()); jLcentreX.setText("CX"); bDibuixa.setPreferredSize(new Dimension(80, 35)); bDibuixa.addActionListener(new Interfase_bDibuixa_actionAdapter(this)); bDibuixa.setText("Dibuja"); jBchooseDir.setMinimumSize(new Dimension(103, 23)); jBchooseDir.setPreferredSize(new Dimension(130, 28)); jBchooseDir.setText("Guardar imagen"); jBchooseDir.addActionListener(new Interfase_jBchooseDir_actionAdapter(this)); this.getContentPane().add(jPbase,BorderLayout.CENTER); jPbase.add(jPsuperior, BorderLayout.NORTH); jPsuperior.add(jLtitol, null); jPbase.add(jPcentral, BorderLayout.CENTER); jPbase.add(jPinferior, BorderLayout.SOUTH); this.getContentPane().setLayout(borderLayout1); this.setBackground(new Color(0, 201, 87));//És el color Esmaragda --> R0, G201, B87 this.getContentPane().add(jPbase, BorderLayout.NORTH); jPinferior.add(jLcredits, null); jSplitPane.setDividerLocation(200); jP1.add(jPcentreY, null); jP1.add(jPcentreX, null); jPcentreX.add(jLiteracions, null); jPcentreX.add(txtIteracio, null); jPcentreX.add(jLescala, null); jPcentreX.add(txtEscala, null); jP1.add(jPescala, null); jPescala.add(jLalt, null); jPescala.add(txtAltura, null); jPescala.add(jLample, null); jPescala.add(txtAmple, null); jP1.add(jPrwrule, null); jPrwrule.add(jLcentreX, null); jPrwrule.add(txtCentreX, null); jPrwrule.add(jLcentreY, null); jPrwrule.add(txtCentreY, null); jP1.add(jPiteracions, null); jPiteracions.add(jLtipusLletra, null); jPiteracions.add(cTipusLletra, null); jSplitPane.add(jPsplitRight, JSplitPane.BOTTOM); jPsplitRight.add(jSPworldContainer, BorderLayout.CENTER); //jSPworldContainer.add(jPworld, null); jSplitPane.add(jPsplitLeft, JSplitPane.TOP); jPcentral.add(jSplitPane, BorderLayout.NORTH); jPsplitLeft.add(jP1, null); jPsplitLeft.add(jP2, null); jP2.add(jLmidaLletra, null); jP2.add(txtMidaLletra, null); jPsplitLeft.add(jP3, null); jP3.add(bDibuixa, null); jP3.add(jBchooseDir, null); jSPworldContainer.getViewport().add(jPworld, null); } //Iniciar el applet public void start() { } //Detener el applet public void stop() { } //Destruir el applet public void destroy() { } //Get Applet information public String getAppletInfo() { return "Applet Information"; } //Get parameter info public String[][] getParameterInfo() { return null; } void bDibuixa_actionPerformed(ActionEvent e) { isOutOfMEmory=false; int altura=Integer.parseInt(txtAltura.getText()); int ample=Integer.parseInt(txtAmple.getText()); if(altura>=3900 | ample>=3900){ isOutOfMEmory=true; } double centreX=Double.parseDouble(txtCentreX.getText()); double centreY=Double.parseDouble(txtCentreY.getText()); double dEscala=Double.parseDouble(txtEscala.getText()); int iAltura=Integer.parseInt(txtAltura.getText()); int iAmple=Integer.parseInt(txtAmple.getText()); int strMidaLletra=Integer.parseInt(txtMidaLletra.getText()); String strTipusLletra=cTipusLletra.getSelectedItem().toString(); objKernel=new Kernel(centreX,centreY,iAltura,iAmple,strOutputDir,strTipusLletra,strMidaLletra); arrayNiteracio.clear();//neteja array d'iteracions arrayNiteracio.add(txtAxioma);//Axioma = arrayNiteracio(0), per definició. //SUBSTITUEIX for (int i = 0; i < Integer.parseInt(txtIteracio.getText()); i++) { strResultat = objKernel.substitueix(RewritingRule,arrayNiteracio.get(i).toString(), "F"); arrayNiteracio.add(i + 1, strResultat); } //LLEGEIX objKernel.llegeix(strResultat); //DIBUIXA objKernel.gestionaDibuix(dEscala); //Presenta la imatge al panell WORLD if(isOutOfMEmory==false){ jPworld.removeAll(); jSPworldContainer.setVisible(true); BufferedImage aux=objKernel.getBufferedImage(); JLabel showImage = new JLabel(new ImageIcon(objKernel.getBufferedImage())); jPworld.add(showImage, null); jPbase.validate(); jPworld.validate(); } else if(isOutOfMEmory==true){ jSPworldContainer.setVisible(false); } } void jBchooseDir_actionPerformed(ActionEvent e) { JFileChooser fileChooser=new JFileChooser("."); fileChooser.setFileSelectionMode(JFileChooser.SAVE_DIALOG); int status=fileChooser.showSaveDialog(null); if(status==JFileChooser.APPROVE_OPTION){ //fileChooser.setSelectedFile(new File("C:\\tallerDir\\testdefinitiu.png")); try { FileOutputStream fop = new FileOutputStream(fileChooser.getSelectedFile().getParent()+"\\"+fileChooser.getSelectedFile().getName()); ImageIO.write(objKernel.getBufferedImage(),"png",fop); } catch (IOException ioe){ ioe.printStackTrace(); } } } } class Interfase_bDibuixa_actionAdapter implements java.awt.event.ActionListener { Interfase adaptee; Interfase_bDibuixa_actionAdapter(Interfase adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.bDibuixa_actionPerformed(e); } } class Interfase_jBchooseDir_actionAdapter implements java.awt.event.ActionListener { Interfase adaptee; Interfase_jBchooseDir_actionAdapter(Interfase adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jBchooseDir_actionPerformed(e); } }