This site requires JavaScript, please enable it in your browser!
Greenfoot back
K_wow
K_wow wrote ...

2013/8/22

Clear System Console?

K_wow K_wow

2013/8/22

#
I was thinking about having a readme show up whenever my game was reset using the "System.out.println" method, but the system console does not clear when I reset. Is there a method I can call to clear the system console?
Gevater_Tod4711 Gevater_Tod4711

2013/8/22

#
Try System.console().flush(); This method is normaly used to store the data in the console to a file but if there is no connected buffered output it should just clear the console. But I'm not shure if this realy works. You'll have to try. May you should not use System.out.println for a readme file because the console (normally) only is shown when you start the game in greenfoot. Not on the webside and also not when you compile your project to a jar.
K_wow K_wow

2013/8/22

#
Gevater_Tod4711 wrote...
Try System.console().flush(); This method is normaly used to store the data in the console to a file but if there is no connected buffered output it should just clear the console. But I'm not shure if this realy works. You'll have to try. May you should not use System.out.println for a readme file because the console (normally) only is shown when you start the game in greenfoot. Not on the webside and also not when you compile your project to a jar.
Oh, well is there any other way I can display a text box that works for a jar, without a heap of complicated code?
K_wow K_wow

2013/8/22

#
Also, using System.console().flush() gives a nullPointerException, and using System.out.flush() does nothing.
Gevater_Tod4711 Gevater_Tod4711

2013/8/22

#
Well if you don't want to use the console you could either draw the message on the greenfoot world so everyone can read it or you can create a JDialog or JFrame object with the text. The code for such an dialog would be:
import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridBagLayout;
import javax.swing.JTextPane;
import java.awt.GridBagConstraints;

public class InformationPopUp extends JDialog {
	
	private static final long serialVersionUID = 1L;
	
	private final JPanel contentPanel = new JPanel();
	
	public InformationPopUp() {
		setBounds(100, 100, 450, 300);//here you can change the position and size of the dialog;
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		GridBagLayout gbl_contentPanel = new GridBagLayout();
		gbl_contentPanel.columnWidths = new int[]{0, 0};
		gbl_contentPanel.rowHeights = new int[]{0, 0};
		gbl_contentPanel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_contentPanel.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		contentPanel.setLayout(gbl_contentPanel);
		{
			JTextPane textPane = new JTextPane();//That's your JTextPane where you can add the text (also a JLabel would work);
			GridBagConstraints gbc_textPane = new GridBagConstraints();
			gbc_textPane.fill = GridBagConstraints.BOTH;
			gbc_textPane.gridx = 0;
			gbc_textPane.gridy = 0;
			textPane.setText("Add your text here");//This text is printed in the JTextPane;
			contentPanel.add(textPane, gbc_textPane);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");//The OK button;
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						dispose();//make the dialog close when you click the OK button;
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
		}
	}
}
You can create the JDialog like this:
//in your world constructor;
new InformationPopUp().setVisible(true);
K_wow K_wow

2013/8/22

#
Gevater_Tod4711 wrote...
-snip-
That's a lot of code. Probably easier to just draw it onto the world, unless there is an easy way to open an external readme file, but I have no idea how I would do it.
Gevater_Tod4711 Gevater_Tod4711

2013/8/22

#
You may use Runntime.getRunntime().exec("theReadmeFile.txt"); But I'm again not shure if this works because I didn't try. Maybe you need to add the whole path so that would be a problem. Also I don't think that this method will work in online applets.
davmac davmac

2013/8/22

#
You can create the JDialog like this:
Not quite, that breaks the Swing threading rules. You need:
EventQueue.invokeLater(new Runnable() {
    public void run() {
        new InformationPopUp().setVisible(true);  
    }
});
Gevater_Tod4711 Gevater_Tod4711

2013/8/22

#
Yes this method is more professionel. But would new InformationPopUp().setVisible(true) not work? When I use this offline it always works but I'm not shure if it would work in an applet. However I'll try to get used to this style.
davmac davmac

2013/8/22

#
But would new InformationPopUp().setVisible(true) not work?
It depends on what you mean by "work". It might appear to work most of the time, but then just once every so often it might not work. Or it might stop working when you update Java. Or, it might not work on someone else's computer. Or, it might seem to work but cause something else to fail unexpectedly later. That's the nature of thread-related issues.
You need to login to post a reply.