📄 ruleui.java
字号:
// then Enter data B // then save, you will be saving data A. // so be careful! buttonSave.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { JOptionPane.showMessageDialog( getFrame(), "<html>IF you have changed the settings since the last time you calculated the L-system,<br>"+ "the L-system you save will be the one last calculated--not the current data!</html>" ); // show save dialog FileDialog fd = new FileDialog(getFrame(), "Save Current L-System Settings As", FileDialog.SAVE); fd.setFile("Untitled.lss"); fd.show(); // on cancel, return if(fd.getFile() == null) return; // else do the thing File outputFile = new File(fd.getDirectory(), fd.getFile()); FileOutputStream outputFileStream = new FileOutputStream(outputFile); java.util.zip.GZIPOutputStream g = new java.util.zip.GZIPOutputStream( new BufferedOutputStream(outputFileStream)); ObjectOutputStream out = new ObjectOutputStream(g); out.writeObject(ls.l); // now need to do a little dance with the GZIPOutputStream to write // this stuff out correctly -- see sim.engine.SimState.writeToCheckpoint(OutputStream) // for more info out.flush(); g.finish(); g.flush(); out.close(); } catch (Exception ex) { ex.printStackTrace(); } } }); // buttonLoad loads the file's seed, rule, and expansions buttonLoad.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { // show load dialog FileDialog fd = new FileDialog(getFrame(), "Open L-System Settings File (.lss)", FileDialog.LOAD); fd.setFile("*.lss"); fd.show(); // on cancel, return if(fd.getFile() == null) return; File inputFile = new File(fd.getDirectory(), fd.getFile()); FileInputStream inputFileStream = new FileInputStream(inputFile); ObjectInputStream in = new ObjectInputStream( new java.util.zip.GZIPInputStream(new BufferedInputStream(inputFileStream))); ls.l = (Lsys)in.readObject(); in.close(); // change UI fields to reflect newly loaded settings // seed seedField.setText(ls.l.seed); // # expansions stepField.setText(String.valueOf(ls.l.expansions)); // line size dui.distField.setText(String.valueOf(ls.l.segsize)); // angle... has been stored in radians, so un-radian it dui.angleField.setText(String.valueOf(ls.l.angle*180/Math.PI)); // x, y // --- unneccessary now that Display2D options do this //dui.xField.setText(String.valueOf(ls.l.x)); //dui.yField.setText(String.valueOf(ls.l.y)); // rules // first clear table for(int t=0; t<ruleTable.getRowCount(); t++) { ruleTable.setValueAt(null, t, 0); ruleTable.setValueAt(null, t, 1); } // now set new stuff for(int t=0; t<ls.l.rules.size(); t++) { ruleTable.setValueAt(String.valueOf((char)((Rule)(ls.l.rules.get(t))).pattern), t, 0); ruleTable.setValueAt(Lsys.fromVector(((Rule)(ls.l.rules.get(t))).replace), t, 1); } } catch (Exception ex) { ex.printStackTrace(); } } }); // buttonHelp displays symbol help buttonHelp.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // this makes it free-floating rather than modal // so you can move to the side and consult the dialog while using the console JFrame help = new JFrame(); help.getContentPane().setLayout( new BorderLayout() ); help.getContentPane().add(helpPanel, BorderLayout.CENTER); help.setSize(400,300); help.setVisible(true); } }); ///// OK // now build the actual UI this.setLayout(new BorderLayout()); // so much panel juggling // looks like this: /* All JPanels use BorderLayout. N = NORTH, C = CENTER, S = SOUTH +-------JPanel this (ruleUI)-----------------+ | +-------JPanel "mid"---------------------+ | | | +--------JPanel "top"----------------+ | | | | | +--------LabelledList "list"-----+ | | | | | |N| Seed =============== | | | | | |N| | Expanions =============== | | | | |N| | +--------------------------------+ | | | | | |C Box( Calculate Cancel ) | | | | | |S (____progressbar__________) | | | | | +------------------------------------+ | | | |C Box( Save Load ) | | | +----------------------------------------+ | | | |C JScrollPane(JTable rules) | +--------------------------------------------+ So that's the overview of the next 50 lines or so */ JPanel top = new JPanel(); JPanel mid = new JPanel(); top.setLayout(new BorderLayout()); mid.setLayout(new BorderLayout()); // List of L-system parameters.. what is a LabelledList? // sim.display.LabelledList is a convenient way to draw lists of the format // text component // text component LabelledList list = new LabelledList() { Insets insets = new Insets(5, 5, 5, 5); public Insets getInsets() { return insets; } }; seedField.setText(ls.l.seed); list.addLabelled("Seed", seedField); list.addLabelled("Expansions", stepField); // add everything so far (in list) top.add(list, BorderLayout.NORTH); // box with calculate and cancel buttons Box b = new Box(BoxLayout.X_AXIS) { Insets insets = new Insets(5, 5, 5, 5); public Insets getInsets() { return insets; } }; b.add(buttonGo); b.add(buttonCancel); buttonCancel.setEnabled(false); b.add(Box.createGlue()); top.add(b, BorderLayout.CENTER); top.add(calcProgress, BorderLayout.SOUTH); calcProgress.setStringPainted(true); calcProgress.setString("Press Calculate"); b = new Box(BoxLayout.X_AXIS) { Insets insets = new Insets(5, 5, 5, 5); public Insets getInsets() { return insets; } }; b.add(buttonSave); b.add(buttonLoad); b.add(buttonHelp); b.add(Box.createGlue()); mid.add(top, BorderLayout.NORTH); mid.add(b, BorderLayout.CENTER); this.add(mid, BorderLayout.NORTH); // allows table to have named headers class NamedTableModel extends DefaultTableModel { NamedTableModel(int rows, int cols) { super(rows, cols); } public String getColumnName( int i ) { if(i == 0) return "Symbol"; else if(i == 1) return "Replacement"; else return "Error."; } }; ruleTable.setModel(new NamedTableModel(20,2)); // rules table setup // same defaults as in LsystemWithUI.java seedField.setText("F"); ruleTable.setValueAt("F", 0, 0); ruleTable.setValueAt("F[+F]F[-F]F", 0, 1); this.add(scrollPane, BorderLayout.CENTER); // Make the Help popup! // This is executed on a click of buttonHelp.. but we've got it set up now. LabelledList list2 = new LabelledList(); helpPanel.setLayout(new BorderLayout()); list2.addLabelled("Symbols", new JLabel("")); list2.addLabelled("Uppercase (A-Z)", new JLabel("Draw forward Distance units")); list2.addLabelled("Lowercase (a-z)", new JLabel("Move forward Distance units (no draw)")); list2.addLabelled("-", new JLabel("Turn right by angle degrees")); list2.addLabelled("+", new JLabel("Turn left by angle degrees")); list2.addLabelled("[", new JLabel("Push position, angle")); list2.addLabelled("]", new JLabel("Pop position, angle")); list2.addLabelled("", new JLabel("")); list2.addLabelled("Save: ", new JLabel("Saves the rules, seed, draw settings, and ")); list2.addLabelled("", new JLabel("calculated expansions of the ")); list2.addLabelled("", new JLabel("Last calculated L-system.")); list2.addLabelled("Load: ", new JLabel("Loads saved L-system settings.")); helpPanel.add(list2, BorderLayout.CENTER); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -