Re: Inconsistent CPU usage
RedGrittyBrick wrote:
Elliot wrote:
Hi Christian
Thanks for the friendly help and the ideas which I will research.
I wonder if anyone has example code to display a splash screen with
input fields and which then goes on to display the main application. .
Here's some rubbish I dashed off in a couple of mins. It compiles, runs
and does what I expected it to do.
[snip]
I decided to use this as an opportunity to learn about java.net.Socket.
---------------------------- 8< ----------------------------
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class PwSocket {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new PwSocket();
}
});
}
private JLabel label = new JLabel("Busy...");
PwSocket() {
final String pw = JOptionPane.showInputDialog("Password");
final SocketThing socketThing = new SocketThing(pw);
final JFrame f = new JFrame("Passworded Socket");
new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
socketThing.start();
return null;
}
@Override
protected void done() {
if (socketThing.hasError()) {
JOptionPane.showMessageDialog(f,
socketThing.getErrorMessage());
System.exit(1);
} else {
label.setText("["+socketThing.getData()+"]");
}
}
}.execute();
JPanel p = new JPanel();
p.add(label);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
class SocketThing {
private Socket sock;
private PrintWriter out = null;
private BufferedReader in = null;
private String errorMessage;
private String data;
SocketThing(String pw) {
System.out.println("Creating SocketThing...");
if (!pw.equals("Foo")) {
errorMessage = "Bad password!";
} else {
try {
sock = new Socket("www.google.com", 80);
out = new PrintWriter(sock.getOutputStream(), true);
InputStream is = sock.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
in = new BufferedReader(isr);
} catch (UnknownHostException e) {
e.printStackTrace();
errorMessage = e.getMessage();
} catch (IOException e) {
e.printStackTrace();
errorMessage = e.getMessage();
}
}
System.out.println("SocketThing created.");
}
public void start() {
System.out.println("Starting SocketThing...");
if (out != null) {
out.println("GET / HTTP/1.1");
out.println("");
try {
data = in.readLine();
} catch (IOException e) {
e.printStackTrace();
errorMessage = e.getMessage();
return;
}
}
System.out.println("SocketThing finished");
}
public boolean hasError() {
return errorMessage != null;
}
public String getErrorMessage() {
return errorMessage;
}
public String getData() {
return data;
}
}
}
---------------------------- 8< ----------------------------
--
RGB