package cat; /** * @author Roger Olivella * @version 1.0 * * Development Environment : Eclipse * Name of the Application : Interfase2.java * Creation/Modification History : * * Roger O. 27/03/2006 Created, a partir de Interfase.java, que es va * crear amb el JBuilder (durant el 2005). El canvi ve imposat a l'hora de * migrar a Eclipse i utilitzar, per modificar el SWING, el * mòdul JILGOO. * Roger O. Modified: * */ import java.awt.BorderLayout; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GridLayout; import java.awt.Point; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.imageio.ImageIO; import javax.swing.BorderFactory; import javax.swing.ComboBoxModel; import javax.swing.DefaultComboBoxModel; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JColorChooser; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JDialog; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.SwingConstants; import javax.swing.border.BevelBorder; import javax.swing.border.LineBorder; import javax.swing.ImageIcon; import java.awt.Color; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Properties; import java.awt.image.BufferedImage; /** * This code was edited or generated using CloudGarden's Jigloo * SWT/Swing GUI Builder, which is free for non-commercial * use. If Jigloo is being used commercially (ie, by a corporation, * company or business for any purpose whatever) then you * should purchase a license for each developer using Jigloo. * Please visit www.cloudgarden.com for details. * Use of Jigloo implies acceptance of these licensing terms. * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE. */ public class Interfase2 extends javax.swing.JApplet { private JPanel jPanelBase; private JLabel jLabelTitle; private JTextField jTextFieldTeuNom; private JLabel jLabelTeuNom; private JButton jButtonHelp; private JPanel jPanelWorld; private JTextField jTextFieldBackColor; private JTextField jTextFieldCharColor; private JButton jButtonExit; private JLabel jLabelInterval2; private JLabel jLabelCharType; private JLabel jLabelInterval; private JButton jButtonSave; private JButton jButtonGO; private JButton jButtonBack; private JLabel jLabelBackColor; private JButton jButtonColors; private JLabel jLabelCharColor; private JTextField jTextFieldCharSize; private JLabel jLabelCharSize; private JComboBox jComboBoxCharType; private JTextField jTextFieldScale; private JLabel JLabelScale; private JComboBox jComboBoxIter; private JLabel jLabelIter; private JPanel jPanel11; private JPanel jPanel10; private JPanel jPanel1; private JPanel jPanel9; private JPanel jPanel8; private JPanel jPanel7; private JPanel jPanel6; private JPanel jPanel5; private JPanel jPanel4; private JPanel jPanel3; private JPanel jPanel2; private JScrollPane jScrollPane1; private JPanel jPanelTitle; JDialog popupWorking = new JDialog(); JOptionPane infoMsg=new JOptionPane(); private JComboBox jComboBoxCharSize=new JComboBox(); Kernel objKernel; ArrayList arrayNiteracio = new ArrayList(); ArrayList arrayResultatLectura = new ArrayList(); String strResultat = ""; String txtAxioma=""; String RewritingRule=""; String strOutputDir=""; Color colorLletra; Color colorFons; boolean flagSaveError=false; int maxBufiSize=3700;//Ha de venir d'un fitxer properties boolean isOutOfMEmory=false; //Dades numèriques: //A mà, he calculat que el nombre de rectes a cada costat per cas de final int numRectes1iter=6;//6 rectes per la 1a iteració final int numRectes2iter=26;//26 rectes per la 2a iteració final int numRectes3iter=106;//106 rectes per la 3a iteració final int numRectes4iter=400;//A ull final int numRectes5iter=800;//A ull //Per què aquest "6" recurrent? int contadorFinestresDimsExcedides=0; int contadorClicksDibuixa=0; boolean dibuixoDesdeComboIter=false; boolean esMesGranQue4=false; BufferedImage finalBufi=null; String dirToSave=""; boolean flagErrorProperties=false; /** * Auto-generated main method to display this * JApplet inside a new JFrame. */ public static void main(String[] args) { JFrame frame = new JFrame(); Interfase2 inst = new Interfase2(); frame.getContentPane().add(inst); ((JComponent)frame.getContentPane()).setPreferredSize(inst.getSize()); frame.pack(); frame.setVisible(true); } public Interfase2() { super(); initGUI(); kernelRun(); } private void initGUI() { try { { jPanelBase = new JPanel(); BorderLayout jPanelBaseLayout = new BorderLayout(); getContentPane().add(jPanelBase); jPanelBase.setPreferredSize(new java.awt.Dimension(800, 600)); jPanelBase.setLayout(jPanelBaseLayout); { jPanelTitle = new JPanel(); jPanelBase.add(jPanelTitle, BorderLayout.NORTH); FlowLayout jPanelTitleLayout = new FlowLayout(); jPanelTitle.setLayout(jPanelTitleLayout); jPanelTitle.setPreferredSize(new java.awt.Dimension(800, 50)); jPanelTitle.setBackground(new java.awt.Color(0,201,87)); { jLabelTitle = new JLabel(); jPanelTitle.add(jLabelTitle); jLabelTitle.setText("insula smaragdina"); jLabelTitle.setPreferredSize(new java.awt.Dimension(716, 31)); jLabelTitle.setFont(new java.awt.Font("SansSerif",0,22)); jLabelTitle.setHorizontalTextPosition(SwingConstants.CENTER); jLabelTitle.setHorizontalAlignment(SwingConstants.CENTER); } } { jScrollPane1 = new JScrollPane(); jPanelBase.add(jScrollPane1, BorderLayout.CENTER); jScrollPane1.setBackground(new java.awt.Color(0,0,0)); jScrollPane1.setPreferredSize(new java.awt.Dimension(600, 551)); { jPanelWorld = new JPanel(); jScrollPane1.setViewportView(jPanelWorld); jPanelWorld.setBackground(new java.awt.Color(0,0,0)); } } { jPanel1 = new JPanel(); jPanelBase.add(jPanel1, BorderLayout.WEST); GridLayout jPanel1Layout = new GridLayout(10, 1); jPanel1Layout.setHgap(5); jPanel1Layout.setVgap(5); jPanel1Layout.setColumns(1); jPanel1Layout.setRows(10); jPanel1.setLayout(jPanel1Layout); jPanel1.setPreferredSize(new java.awt.Dimension(199, 550)); jPanel1.setBackground(new java.awt.Color(0, 201, 87)); { jPanel2 = new JPanel(); jPanel1.add(jPanel2); jPanel2.setBackground(new java.awt.Color(0,201,87)); jPanel2.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED)); { jLabelIter = new JLabel(); jPanel2.add(jLabelIter); jLabelIter.setText("Complexitat"); jLabelIter.setPreferredSize(new java.awt.Dimension(93, 14)); jLabelIter.setFont(new java.awt.Font("Arial",1,14)); jLabelIter.setToolTipText("Nombre d'iteracions del fractal"); } { ComboBoxModel jComboBoxIterModel = new DefaultComboBoxModel( new String[] { " 0", " 1"," 2"," 3"," 4"," >4"}); jComboBoxIter = new JComboBox(); jPanel2.add(jComboBoxIter); jComboBoxIter.setModel(jComboBoxIterModel); jComboBoxIter.setPreferredSize(new java.awt.Dimension(51, 21)); jComboBoxIter.setLightWeightPopupEnabled(false); jComboBoxIter.setFont(new java.awt.Font("Arial",1,14)); jComboBoxIter.setSelectedIndex(0); jComboBoxIter.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent evt) { jComboBoxIterItemStateChanged(evt); } }); } } { jPanel3 = new JPanel(); jPanel1.add(jPanel3); jPanel3.setBackground(new java.awt.Color(0,201,87)); jPanel3.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED)); { JLabelScale = new JLabel(); jPanel3.add(JLabelScale); JLabelScale.setText("Mida"); JLabelScale.setFont(new java.awt.Font("Arial",1,14)); JLabelScale.setPreferredSize(new java.awt.Dimension(41, 17)); JLabelScale.setToolTipText("Mida del poema en conjunt"); } { jTextFieldScale = new JTextField(); jPanel3.add(jTextFieldScale); jTextFieldScale.setText("50"); jTextFieldScale.setPreferredSize(new java.awt.Dimension(29, 23)); jTextFieldScale.setFont(new java.awt.Font("Arial",1,14)); } { jLabelInterval = new JLabel(); jPanel3.add(jLabelInterval); jLabelInterval.setText("(1 - 77)"); jLabelInterval.setFont(new java.awt.Font("Arial",1,14)); jLabelInterval.setToolTipText("Interval vàlid"); } } { jPanel5 = new JPanel(); jPanel1.add(jPanel5); jPanel5.setBackground(new java.awt.Color(0,201,87)); jPanel5.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED)); jPanel5.setPreferredSize(new java.awt.Dimension(164, 60)); { jLabelCharType = new JLabel(); jPanel5.add(jLabelCharType); jLabelCharType.setText("Lletra"); jLabelCharType.setFont(new java.awt.Font("Arial",1,14)); jLabelCharType.setPreferredSize(new java.awt.Dimension(45, 17)); jLabelCharType.setToolTipText("Nom del tipus de lletra"); } { ComboBoxModel jComboBoxCharTypeModel = new DefaultComboBoxModel( new String[] {"Serif", "Sans Serif","Monospaced"}); jComboBoxCharType = new JComboBox(); jPanel5.add(jComboBoxCharType); jComboBoxCharType.setModel(jComboBoxCharTypeModel); jComboBoxCharType.setFont(new java.awt.Font( "Arial", 1, 14)); jComboBoxCharType.setBounds(-42, 5, 114, 18); jComboBoxCharType.setPreferredSize(new java.awt.Dimension(124, 22)); jComboBoxCharType.setSelectedIndex(1); jComboBoxCharType .addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent evt) { jComboBoxCharTypeItemStateChanged(evt); } }); } } { jPanel6 = new JPanel(); jPanel1.add(jPanel6); jPanel6.setBackground(new java.awt.Color(0,201,87)); jPanel6.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED)); { jLabelCharSize = new JLabel(); jPanel6.add(jLabelCharSize); jLabelCharSize.setText("Mida lletra "); jLabelCharSize.setFont(new java.awt.Font("Arial",1,14)); jLabelCharSize.setToolTipText("Mida de cada lletra"); jLabelCharSize.setPreferredSize(new java.awt.Dimension(84, 17)); } { jTextFieldCharSize = new JTextField(); jPanel6.add(jTextFieldCharSize); jTextFieldCharSize.setText("10"); jTextFieldCharSize.setFont(new java.awt.Font("Arial",1,14)); jTextFieldCharSize.setPreferredSize(new java.awt.Dimension(29, 23)); } { jLabelInterval2 = new JLabel(); jPanel6.add(jLabelInterval2); jLabelInterval2.setText("(1 - 30)"); jLabelInterval2.setFont(new java.awt.Font("SansSerif",1,14)); jLabelInterval2.setToolTipText("Interval vàlid"); } } { jPanel7 = new JPanel(); jPanel1.add(jPanel7); jPanel7.setBackground(new java.awt.Color(0,201,87)); jPanel7.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED)); { jLabelCharColor = new JLabel(); jPanel7.add(jLabelCharColor); jLabelCharColor.setText("Color lletres"); jLabelCharColor.setFont(new java.awt.Font("SansSerif",1,14)); jLabelCharColor.setToolTipText("Color de totes les lletres"); } { jTextFieldCharColor = new JTextField(); jPanel7.add(jTextFieldCharColor); jTextFieldCharColor .setPreferredSize(new java.awt.Dimension(15, 20)); jTextFieldCharColor .setBackground(new java.awt.Color(0, 0, 0)); jTextFieldCharColor.setToolTipText("Color de cada lletra"); jTextFieldCharColor.setEnabled(false); jTextFieldCharColor.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false)); } { jButtonColors = new JButton(); jPanel7.add(jButtonColors); jButtonColors.setText("Click!"); jButtonColors.setToolTipText("Click aquí per canviar el color de la lletra"); jButtonColors.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { jButtonColorsMouseClicked(evt); } }); } } { jPanel8 = new JPanel(); jPanel1.add(jPanel8); jPanel8.setBackground(new java.awt.Color(0,201,87)); jPanel8.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED)); { jLabelBackColor = new JLabel(); jPanel8.add(jLabelBackColor); jLabelBackColor.setText("Color fons"); jLabelBackColor.setFont(new java.awt.Font("SansSerif",1,14)); jLabelBackColor.setToolTipText("Color del fons"); } { jTextFieldBackColor = new JTextField(); jPanel8.add(jTextFieldBackColor); jTextFieldBackColor .setPreferredSize(new java.awt.Dimension(15, 20)); jTextFieldBackColor.setToolTipText("Color del fons"); jTextFieldBackColor.setEnabled(false); jTextFieldBackColor.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false)); } { jButtonBack = new JButton(); jPanel8.add(jButtonBack); jButtonBack.setText("Click!"); jButtonBack.setToolTipText("Click aquí per canviar el color del fons"); jButtonBack.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { jButtonBackMouseClicked(evt); } }); } } { jPanel9 = new JPanel(); jPanel1.add(jPanel9); jPanel9.setBackground(new java.awt.Color(0,201,87)); { jButtonGO = new JButton(); jPanel9.add(jButtonGO); jButtonGO.setText("CREA POEMA!"); jButtonGO.setSize(164, 50); jButtonGO.setPreferredSize(new java.awt.Dimension( 166, 41)); jButtonGO.setFont(new java.awt.Font( "SansSerif", 1, 14)); jButtonGO .setToolTipText("Click aquí per dibuixar la teva versió del poema"); jButtonGO.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { jButtonGOMouseClicked(evt); } }); } } { jPanel10 = new JPanel(); jPanel1.add(jPanel10); jPanel10.setBackground(new java.awt.Color(0,201,87)); { jLabelTeuNom = new JLabel(); jPanel10.add(jLabelTeuNom); jLabelTeuNom.setText("El teu nom:"); jLabelTeuNom.setFont(new java.awt.Font( "SansSerif", 1, 14)); } { jTextFieldTeuNom = new JTextField(); jPanel10.add(jTextFieldTeuNom); jTextFieldTeuNom .setPreferredSize(new java.awt.Dimension(82, 24)); } } { jPanel11 = new JPanel(); jPanel1.add(jPanel11); jPanel11.setBackground(new java.awt.Color(0,201,87)); { jButtonSave = new JButton(); jPanel11.add(jButtonSave); jButtonSave.setText("Guardar poema"); jButtonSave .setToolTipText("Click aquí per guardar el poema al disc dur"); jButtonSave.setFont(new java.awt.Font( "SansSerif", 1, 12)); jButtonSave.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { jButtonSaveMouseClicked(evt); } }); } } { jPanel4 = new JPanel(); jPanel1.add(jPanel4); jPanel4.setBackground(new java.awt.Color(0,201,87)); { jButtonHelp = new JButton(); jPanel4.add(jButtonHelp); jButtonHelp.setText("Ajuda"); jButtonHelp.setFont(new java.awt.Font("SansSerif",1,14)); jButtonHelp.setPreferredSize(new java.awt.Dimension(86, 22)); jButtonHelp.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { jButtonHelpMouseClicked(evt); } }); } { jButtonExit = new JButton(); jPanel4.add(jButtonExit); jButtonExit.setText("Tancar"); jButtonExit.setFont(new java.awt.Font( "SansSerif", 1, 14)); jButtonExit .setPreferredSize(new java.awt.Dimension(86, 22)); jButtonExit.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { jButtonExitMouseClicked(evt); } }); } } } } jComboBoxCharSize.addItem("10"); jComboBoxCharSize.addItem("20"); jComboBoxCharSize.addItem("30"); jComboBoxCharSize.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent evt) { jComboBoxCharSizeItemStateChanged(evt); } }); //Carrega el size de l'interfase: Properties properties = new Properties(); try { properties.load(new FileInputStream("dir.properties")); dirToSave=properties.getProperty("dir"); } catch (IOException ioe) { ioe.printStackTrace(); flagErrorProperties=true; } //int appletWidth=Integer.parseInt(properties.getProperty("appletWidth")); //int appletHeight=Integer.parseInt(properties.getProperty("appletHeight")); //this.setSize(appletWidth,appletHeight); getContentPane().setBackground(new java.awt.Color(0,201,87)); } catch (Exception e) { e.printStackTrace(); } } private void jButtonColorsMouseClicked(MouseEvent evt) { Color initialColor = Color.black; //how the dialog; this method does not return until the dialog is closed colorLletra = JColorChooser.showDialog(jPanelBase, "Color de les lletres", initialColor); jTextFieldCharColor.setBackground(colorLletra); } private void jButtonBackMouseClicked(MouseEvent evt) { Color initialColor = Color.white; //how the dialog; this method does not return until the dialog is closed colorFons = JColorChooser.showDialog(jPanelBase, "Color de fons", initialColor); jTextFieldBackColor.setBackground(colorFons); } private void jButtonGOMouseClicked(MouseEvent evt) { kernelRun(); } //Mètode que prepara i executa el Kernel private void kernelRun(){ contadorClicksDibuixa++; if (jComboBoxIter.getSelectedItem().toString().trim().equals(">4")) esMesGranQue4=true; if(!esMesGranQue4){ int strMidaLletra=0; double dEscala=0; String strTipusLletra=jComboBoxCharType.getSelectedItem().toString(); int numIteracio=Integer.parseInt(jComboBoxIter.getSelectedItem().toString().trim()); int iAltura=0; int iAmple=0; double centreX=0; double centreY=0; RewritingRule="F+F-F-FF+F+F-F"; txtAxioma="F+F+F+F"; boolean isEscalaErr=false; boolean isMidaLletraErr=false; //Fixo els valors inicial per les iteracions 3, 4 i 5. S'ha de fer aqui. if(numIteracio==3 & dibuixoDesdeComboIter){ strMidaLletra=6; dEscala=10; jTextFieldScale.setText(new Integer(new Double(dEscala).intValue()).toString()); jTextFieldCharSize.setText(new Integer(strMidaLletra).toString()); } else if(numIteracio==4 & dibuixoDesdeComboIter){ strMidaLletra=2; dEscala=6; jTextFieldScale.setText(new Integer(new Double(dEscala).intValue()).toString()); jTextFieldCharSize.setText(new Integer(strMidaLletra).toString()); } else if(numIteracio==5 & dibuixoDesdeComboIter){ strMidaLletra=1; dEscala=1; jTextFieldScale.setText(new Integer(new Double(dEscala).intValue()).toString()); jTextFieldCharSize.setText(new Integer(strMidaLletra).toString()); } /***************************************/ /***************Validacions*************/ /***************************************/ //Escala global: //1-No hi ha cap lletra try { dEscala=Double.parseDouble(jTextFieldScale.getText()); } catch (NumberFormatException nfe){ isEscalaErr=true; } //2-Està en l'interval correcte: if(numIteracio==1 & (dEscala<1 | dEscala>200)) { isEscalaErr=true; infoMsg.showMessageDialog(null,"Si us plau, introdueix un nombre entre 1 i 200 on posa \"Mida\".","Error d\'interval!",JOptionPane.ERROR_MESSAGE); } if(numIteracio==2 & (dEscala<1 | dEscala>100)) { isEscalaErr=true; infoMsg.showMessageDialog(null,"Si us plau, introdueix un nombre entre 1 i 100 on posa \"Mida\".","Error d\'interval!",JOptionPane.ERROR_MESSAGE); } if(numIteracio==3 & (dEscala<1 | dEscala>33)) { isEscalaErr=true; infoMsg.showMessageDialog(null,"Si us plau, introdueix un nombre entre 1 i 33 on posa \"Mida\".","Error d\'interval!",JOptionPane.ERROR_MESSAGE); } if(numIteracio==4 & (dEscala<1 | dEscala>8)) { isEscalaErr=true; infoMsg.showMessageDialog(null,"Si us plau, introdueix un nombre entre 1 i 8 on posa \"Mida\".","Error d\'interval!",JOptionPane.ERROR_MESSAGE); } //Mida lletra: //1-No hi ha cap lletra try { strMidaLletra=Integer.parseInt(jTextFieldCharSize.getText()); } catch (NumberFormatException nfe){ isMidaLletraErr=true; } //2-Està en l'interval correcte: if((numIteracio==1 | numIteracio==2 | numIteracio==3 | numIteracio==4) & (strMidaLletra<1 | strMidaLletra>40)) { isMidaLletraErr=true; infoMsg.showMessageDialog(null,"Si us plau, introdueix un nombre entre 1 i 40 on posa \"Mida lletra\"","Error d\'interval!",JOptionPane.ERROR_MESSAGE); } /***************************************/ /***********Fi validacions**************/ /***************************************/ if(!isEscalaErr & !isMidaLletraErr){//Si no hi ha errors, GO ON! //Càlcul de l'altura, ample i centres en funció de la mida de cada segment recte: if(numIteracio==0){//Iteració 0 jLabelInterval2.setVisible(false);//Trec el label del interval de la mida de lletra jTextFieldCharSize.setVisible(false);//Trec el Text Box jPanel6.add(jComboBoxCharSize); jComboBoxCharSize.setVisible(true); strMidaLletra=Integer.parseInt(jComboBoxCharSize.getSelectedItem().toString()); if(strMidaLletra==10){ iAmple=315; iAltura=50; centreX=15; centreY=20; } else if(strMidaLletra==20){ iAmple=580; iAltura=80; centreX=15; centreY=35; } else if(strMidaLletra==30){ iAmple=870; iAltura=120; centreX=15; centreY=45; } //Modificacions de l'interfície: jButtonBack.setVisible(true); jButtonColors.setVisible(true); jPanel3.setVisible(false); jButtonGO.setVisible(true); jPanel5.setVisible(false); } else if (numIteracio==1){//Iteració 1 int marge=60; if(dibuixoDesdeComboIter){ strMidaLletra=10; dEscala=50; jTextFieldScale.setText(new Integer(new Double(dEscala).intValue()).toString()); jTextFieldCharSize.setText(new Integer(strMidaLletra).toString()); } iAmple=(int)(numRectes1iter*dEscala+marge); centreX=dEscala+marge/2; iAltura=iAmple; centreY=centreX; //Modificacions de l'interfície: jButtonBack.setVisible(true); jButtonColors.setVisible(true); jPanel3.setVisible(true); jLabelInterval2.setVisible(true); jTextFieldCharSize.setVisible(true); jComboBoxCharSize.setVisible(false); jButtonGO.setVisible(true); jPanel5.setVisible(true); jLabelInterval.setText("1 a 200"); jLabelInterval2.setText("1 a 40"); } else if (numIteracio==2){//Iteració 2 int marge=60; if(dibuixoDesdeComboIter){ strMidaLletra=10; dEscala=50; jTextFieldScale.setText(new Integer(new Double(dEscala).intValue()).toString()); jTextFieldCharSize.setText(new Integer(strMidaLletra).toString()); } iAmple=(int)(numRectes2iter*dEscala+marge); centreX=5*dEscala+marge/2; iAltura=iAmple; centreY=centreX; //Modificacions de l'interfície: jButtonBack.setVisible(true); jButtonColors.setVisible(true); jPanel3.setVisible(true); jLabelInterval2.setVisible(true); jTextFieldCharSize.setVisible(true); jComboBoxCharSize.setVisible(false); jButtonGO.setVisible(true); jPanel5.setVisible(true); jLabelInterval.setText("1 a 100"); jLabelInterval2.setText("1 a 40"); } else if (numIteracio==3){//Iteració 3 int marge=120; if(dibuixoDesdeComboIter){ strMidaLletra=6; dEscala=10; jTextFieldScale.setText(new Integer(new Double(dEscala).intValue()).toString()); jTextFieldCharSize.setText(new Integer(strMidaLletra).toString()); } iAmple=(int)(numRectes3iter*dEscala+marge); if (iAmple>maxBufiSize) { iAmple=maxBufiSize; infoMsg.showMessageDialog(null,"S'ha excedit la mida màxima.\nA continuació es refarà a la màxima mida permesa.","Mida màxima excedida",JOptionPane.INFORMATION_MESSAGE); } centreX=22*dEscala+marge/2; iAltura=iAmple; centreY=centreX; //Modificacions de l'interfície: jButtonBack.setVisible(false); jButtonColors.setVisible(false); jPanel3.setVisible(true); jLabelInterval2.setVisible(true); jTextFieldCharSize.setVisible(true); jComboBoxCharSize.setVisible(false); jPanel5.setVisible(true); jButtonGO.setVisible(true); jTextFieldBackColor.setBackground(Color.WHITE); jTextFieldCharColor.setBackground(Color.BLACK); jLabelInterval.setText("1 a 33"); jLabelInterval2.setText("1 a 40"); } else if (numIteracio==4){//Iteració 4 int marge=240; if(dibuixoDesdeComboIter){ strMidaLletra=2; dEscala=6; jTextFieldScale.setText(new Integer(new Double(dEscala).intValue()).toString()); jTextFieldCharSize.setText(new Integer(strMidaLletra).toString()); } iAmple=(int)(numRectes4iter*dEscala+marge); if (iAmple>maxBufiSize) { iAmple=maxBufiSize; infoMsg.showMessageDialog(null,"S'ha excedit la mida màxima del poema.\nA continuació es refarà a la màxima mida permesa.","Mida màxima excedida",JOptionPane.INFORMATION_MESSAGE); } centreX=70*dEscala+marge/2; iAltura=iAmple; centreY=centreX; //Modificacions de l'interfície: jButtonBack.setVisible(false); jButtonColors.setVisible(false); jPanel3.setVisible(true); jLabelInterval2.setVisible(true); jTextFieldCharSize.setVisible(true); jComboBoxCharSize.setVisible(false); jPanel5.setVisible(true); jButtonGO.setVisible(true); jTextFieldBackColor.setBackground(Color.WHITE); jTextFieldCharColor.setBackground(Color.BLACK); jLabelInterval.setText("1 a 8"); jLabelInterval2.setText("1 a 40"); } else if (numIteracio==5){//Iteració 5 int marge=400; if(dibuixoDesdeComboIter){ strMidaLletra=2; dEscala=2; jTextFieldScale.setText(new Integer(new Double(dEscala).intValue()).toString()); jTextFieldCharSize.setText(new Integer(strMidaLletra).toString()); } iAmple=maxBufiSize; centreX=300*dEscala+marge/2; iAltura=iAmple; centreY=centreX; //Modificacions de l'interfície: jButtonBack.setVisible(false); jButtonColors.setVisible(false); jPanel3.setVisible(false); jPanel6.setVisible(false); jPanel5.setVisible(false); jTextFieldBackColor.setBackground(Color.WHITE); jTextFieldCharColor.setBackground(Color.BLACK); jButtonGO.setVisible(false); } objKernel=new Kernel(centreX,centreY,iAltura,iAmple,strOutputDir,strTipusLletra,strMidaLletra,jTextFieldCharColor.getBackground(),jTextFieldBackColor.getBackground(),numIteracio); arrayNiteracio.clear();//neteja array d'iteracions arrayNiteracio.add(txtAxioma);//Axioma = arrayNiteracio(0), per definició. /*********************************/ /********Execució del Kernel******/ /*********************************/ //SUBSTITUEIX if(numIteracio==0){ objKernel.dibuixaIter0(); } else { if(numIteracio>=3){ showPopup("Un moment, si us plau.","Creant el poema...",new Dimension(200,100),new Point(300,300)); } for (int i = 0; i < numIteracio ; i++) { strResultat = objKernel.substitueix(RewritingRule,arrayNiteracio.get(i).toString(), "F"); arrayNiteracio.add(i + 1, strResultat); } } //LLEGEIX if(numIteracio!=0) objKernel.llegeix(strResultat); //DIBUIXA if(numIteracio!=0) objKernel.gestionaDibuix(dEscala); /************************************/ /********Fi execució del Kernel******/ /************************************/ //Presenta la imatge al panell WORLD finalBufi=objKernel.getBufferedImage(); jPanelWorld.removeAll(); jPanelWorld.repaint();//Reload el panell jScrollPane1.setVisible(true); JLabel showImage = new JLabel(new ImageIcon(finalBufi)); jPanelWorld.add(showImage, null); jPanelBase.validate(); jPanelWorld.validate(); //Es tanca els popup de creant poema if(numIteracio>=3) popupWorking.setVisible(false); //Popup si s'excedeix mida visor: if(finalBufi.getWidth()>598 & finalBufi.getHeight()>547 & (contadorFinestresDimsExcedides==0 | contadorClicksDibuixa%10==0)){ contadorFinestresDimsExcedides++; infoMsg.showMessageDialog(null,"El poema ha excedit la mida del visor. Per veure'l en la seva extensió, \nutilitza les barres de desplaçamanet horitzontals i verticals.","El poema excedeix la mida del visor",JOptionPane.INFORMATION_MESSAGE); } //S'inicialitza la variable: dibuixoDesdeComboIter=false; } } else { infoMsg.showMessageDialog(null," Teòricament és possible generar el poema\n fins l'infinit. Degut però a les limitacions\n d\'aquest programa, no es permet represen-\n tar poemes més enllà de la quarta iteració.","Iteració més gran que 4...",JOptionPane.INFORMATION_MESSAGE); esMesGranQue4=false; } } private void jButtonSaveMouseClicked(MouseEvent evt) { //Carrega el directòri on es guardarà el PNG: /*Properties properties = new Properties(); String dirToSave=""; boolean flagErrorProperties=false; try { //El properties s'ha d'ubicar properties.load(new FileInputStream("dir.properties")); dirToSave=properties.getProperty("dir"); } catch (IOException ioe) { ioe.printStackTrace(); flagErrorProperties=true; }*/ //Recupera el nom de l'usuàri: String elTeuNom=jTextFieldTeuNom.getText().trim(); if(elTeuNom.equals("")){ infoMsg.showMessageDialog(null,"Si us plau, abans de guardar posa el teu nom a la casella de dalt.","Falta el nom",JOptionPane.INFORMATION_MESSAGE); } else { //Extreu la data d'avui amb precissió, per afegir-la al nom final del poema SimpleDateFormat sdf = new SimpleDateFormat("-ddMMyyyy-hhmmss"); String now=sdf.format(new Date()); String fileToSave=dirToSave+"poema-"+elTeuNom+now+".png"; try { FileOutputStream fop = new FileOutputStream(fileToSave); showPopup("Guardant el poema al disc dur...","Guardant el poema...",new Dimension(200,100),new Point(300,300)); ImageIO.write(objKernel.getBufferedImage(),"png",fop); popupWorking.setVisible(false); } catch (IOException ioe){ flagSaveError=true; ioe.printStackTrace(); } //Popup final: if(!flagSaveError & !flagErrorProperties){ infoMsg.showMessageDialog(null,"Ubicació i nom de l'arxiu: \n\n"+fileToSave,"Poema guardat correctament!",JOptionPane.INFORMATION_MESSAGE); } else if (flagSaveError){ infoMsg.showMessageDialog(null,"Si us plau, revisa el directori on es guardarà la imatge i torna-ho a provar.","Error!",JOptionPane.ERROR_MESSAGE); } else if (flagErrorProperties){ infoMsg.showMessageDialog(null,"No es troba dir.properties.\n\nPath: "+dirToSave+".","Error!",JOptionPane.ERROR_MESSAGE); } } //Es crea i configura el JFileChooser /*JFileChooser fileChooser=new JFileChooser("."); fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//nomes es poden escollir dirs fileChooser.setDialogTitle("Escull un directòri per guardar el teu poema"); watch(fileChooser);//Deshabilita el camp "File name" del JFileChooser int status=fileChooser.showSaveDialog(null);*/ /*if(status==JFileChooser.APPROVE_OPTION){ File selectedFile=fileChooser.getSelectedFile(); //Deploy (vigilar path en WINDOWS! => doble contrabarra o \\): //String dirToSave=selectedFile.getParent()+"/"+selectedFile.getName()+"/"; //Local: String dirToSave="/home/rolivella/Desktop/"; String fileToSave=dirToSave+"poema-insula"+now+".png"; try { FileOutputStream fop = new FileOutputStream(fileToSave); showPopup("Guardant el poema al disc dur...","Guardant el poema...",new Dimension(200,100),new Point(300,300)); ImageIO.write(objKernel.getBufferedImage(),"png",fop); popupWorking.setVisible(false); } catch (IOException ioe){ flagSaveError=true; ioe.printStackTrace(); } //Popup final: if(flagSaveError==false){ infoMsg.showMessageDialog(null,"Ubicació i nom de l'arxiu: \n\n"+fileToSave,"Poema guardat correctament!",JOptionPane.INFORMATION_MESSAGE); } else if (flagSaveError==true){ infoMsg.showMessageDialog(null,"Si us plau, revisi el directori on guardarà la imatge i torni-ho a provar.","Error!",JOptionPane.ERROR_MESSAGE); } }*/ } /** * Deshabilita automaticament el camp JTextField del JFileChooser. S'ha de fer aixi doncs * el camp esta encapsulat dins del JFileChooser i no hi ha cap metode per accedir-hi. * Extret del Java Forums (Swing), programat per Anton Nashatyrev (28/01/2004) * @param c Instancia del component JFileChooser */ static void watch(Container c) { int n = c.getComponentCount(); for (int i = 0; i < n; i++) { Component ch = c.getComponent(i); if (ch instanceof Container) { Container child = (Container) ch; if (child instanceof JTextField) { child.setEnabled(false); } else { watch(child); } } } } /** * Mostra un popup. És configuraSELECTEDble. * @param popupBodyMessage String Body del popup * @param popupTitle String Titol * @param popupSize Dimension Mida del popup * @param popupLocation Point Localitzacio */ /*public void showPopup(String popupBodyMessage,String popupTitle,Dimension popupSize,Point popupLocation,boolean isScrolling){ //Àrea de text: JTextArea popupTA=new JTextArea(); popupTA.repaint(); popupTA.setBackground(Color.WHITE); popupTA.setFont(new Font("Sans Serif", Font.PLAIN, 15)); popupTA.setText(popupBodyMessage); popupTA.setEditable(false); popupTA.setCaretPosition(0);//cursor al principi de l'àrea //Fi àrea text if(!isScrolling){ JPanel popupPanel=new JPanel(); popupPanel.setBackground(Color.WHITE); popupPanel.remove(popupTA); popupPanel.repaint(); popupPanel.add(popupTA); popupWorking.remove(popupPanel); popupWorking.repaint(); popupWorking.add(popupPanel);//addegeix el panell de popup al JDialog popupWorking.validate(); popupPanel.validate(); } else { JScrollPane popupPanel=new JScrollPane(); popupPanel.setBackground(Color.WHITE); popupPanel.remove(popupTA); popupPanel.repaint(); popupPanel.add(popupTA); popupPanel.getViewport().add(popupTA, null); popupWorking.remove(popupPanel); popupWorking.repaint(); popupWorking.add(popupPanel);//addegeix el panell de popup al JDialog popupWorking.validate(); } popupWorking.setTitle(popupTitle);//titol popup popupWorking.setSize(popupSize);//size popup popupWorking.setLocation(popupLocation);//location popup popupWorking.setVisible(true);//Fa visible el JDialog }*/ public void showPopup(String popupBodyMessage,String popupTitle,Dimension popupSize,Point popupLocation){ JTextArea popupTA=new JTextArea(); popupTA.setBackground(Color.WHITE); popupTA.setFont(new Font("Sans Serif", Font.PLAIN, 15)); popupWorking.setTitle(popupTitle); popupTA.setText(popupBodyMessage); JPanel popupPanel=new JPanel(); popupPanel.add(popupTA); popupWorking.setSize(popupSize); popupWorking.setLocation(popupLocation); popupPanel.setBackground(Color.WHITE); popupWorking.getContentPane().add(popupPanel); popupWorking.setVisible(true); popupWorking.setEnabled(false); } //Apretat botó de "Tancar": private void jButtonExitMouseClicked(MouseEvent evt) { System.exit(0); } private void jComboBoxIterItemStateChanged(ItemEvent evt) { if(evt.getStateChange()==evt.SELECTED){ dibuixoDesdeComboIter=true; kernelRun(); } } private void jComboBoxCharSizeItemStateChanged(ItemEvent evt) { if(evt.getStateChange()==evt.SELECTED){ kernelRun(); } } private void jComboBoxCharTypeItemStateChanged(ItemEvent evt) { if(evt.getStateChange()==evt.SELECTED){ kernelRun(); } } private void jButtonHelpMouseClicked(MouseEvent evt) { String titol="Ajuda"; //String body="\n AJUDA SOBRE EL GENERADOR DE POEMES:\n\n"; String body=" 1. Escull un nivell de \"Complexitat\" o nombre d'iteracions del fractal."; //body=body+"\n"; //body=body+" Complexitat 0: La frase que es repetirà indefinidament en les següents\n iteracions. NO és un fractal.\n"; //body=body+" Complexitat 1: Primera iteració, la més senzilla.\n"; //body=body+" Complexitat 2: Segona iteració. Fractal més comlex.\n"; //body=body+" Complexitat 3: Tercera iteració. El fractal més gran presentat a l'expo-\n sició.\n"; //body=body+"\n"; //body=body+" Les de nivell 1, 2 i 3 són les que es mostren a la present exposició.\n\n"; //body=body+"\n"; //body=body+" Complexitat 4: Quarta iteració.\n"; //body=body+" Complexitat >4: No es representa cap poema degut a les limitacions\n"; //body=body+" del programa. Teòricament, les iteracions poden arribar fins a\n l'infinit.\n"; //body=body+"\n"; body=body+" \n 2. Entra la \"Mida\" del poema en conjunt. Només accepta nombres\n (decimals o enters).\n"; //body=body+"\n"; body=body+" 3. Escull el tipus de \"Lletra\"."; //body=body+" No apareix per la iteració 5.\n"; body=body+"\n"; body=body+" 4. Entra la \"Mida de la lletra\". Només accepta nombres (decimals \n o enters).\n"; body=body+" 5. Entra el \"Color lletres\", o color que vols que tinguin les \n lletres. És negre per iteracions més grans o iguals a 3.\n"; body=body+" 6. Entra el \"Color fons\", o color que vols que tingui el fons.\n És blanc per iteracions més grans o iguals a 3.\n\n"; body=body+" Si esculls \"CREA POEMA!\", a la part dreta del programa es dibuixarà la\n teva versió del poema.\n\n"; body=body+" Si esculls \"Guardar poema\", es guardarà un arxiu PNG amb la teva\n"; body=body+" versió del poema. Fixa't en la finestra que et sortirà a continuació\n"; body=body+" per veure on s'ha guardat l'arxiu i com es diu. El format de l'arxiu és:\n\n"; body=body+" poema-el teu nom-ddmmaaaa-hhnnss.png, on d és dia, m és mes,\n"; body=body+" a és any, h és hora, n és minut i s és segon. Recorda d'omplir\n"; body=body+" el camp \"El teu nom\" abans d'apretar sobre el botó \"Guardar poema\"."; body=body+" \n\n Escull \"Tancar\" per tancar l'aplicació.\n\n"; //body=body+" NOTA: quan es dibuixin poemes de complexitat més gran o igual a 3,\n"; //body=body+" és possible que es produeixi un petit retard a l'hora d'utilitzar\n les"; //body=body+" barres de desplaçament. Aixó doncs, si us plau, espera't uns\n segons per manipular el poema després de que s'hagi dibuixat.\n\n"; //showPopup(body,titol,new Dimension(550,550),new Point(220,100),true); infoMsg.showMessageDialog(null,body,titol,JOptionPane.INFORMATION_MESSAGE); } }