Saturday 23 August 2014

How to Close a Java Frame

I wanted to be able to close my Java window by clicking on the x in the top left hand corner. To do this, I added the WindowListener interface as shown below. The window does not look much different (except that it is blue this time) but I can close it now without returning to the UNIX prompt and using Ctrl+C. As usual, click on the image to enlarge it and bring it into focus:

andrew@UBUNTU:~/Java$ cat AndrewsFrame.java
import java.awt.*;
import java.awt.event.*;
public class AndrewsFrame
extends Frame
implements WindowListener
{
public AndrewsFrame()
  {
  super("Andrew's Frame");
  setSize(500,300);
  setBackground(Color.blue);
  setVisible(true);
  addWindowListener(this);
  }
public void windowClosing(WindowEvent x)
  {
  dispose();
  System.exit(0);
  }
public void windowActivated(WindowEvent x) { }
public void windowDeactivated(WindowEvent x) { }
public void windowOpened(WindowEvent x) { }
public void windowClosed(WindowEvent x) { }
public void windowIconified(WindowEvent x) { }
public void windowDeiconified(WindowEvent x) { }
public static void main (String args[])
  {
  AndrewsFrame af = new AndrewsFrame();
  }
}
andrew@UBUNTU:~/Java$ javac AndrewsFrame.java
andrew@UBUNTU:~/Java$ java AndrewsFrame 


Thursday 21 August 2014

How to Change the Background Colour in Java

I decided I did not like the background colour of my frame so I added the line shown in bold italics, compiled it and ran it:

andrew@UBUNTU:~/Java$ cat AndrewsFrame.java
import java.awt.*;
public class AndrewsFrame extends Frame
{
public AndrewsFrame()
  {
  super("Andrew's Frame");
  setSize(500,300);
  setBackground(Color.red);
  setVisible(true);
  }
public static void main (String args[])
  {
  AndrewsFrame af = new AndrewsFrame();
  }
}
andrew@UBUNTU:~/Java$ javac AndrewsFrame.java
andrew@UBUNTU:~/Java$ java AndrewsFrame


This time the background was red (as usual, click on the image to enlarge it and bring it into focus):

 

Wednesday 20 August 2014

How to Display an Empty Frame in Java

I created the file shown below using vi then compiled it and ran it:

andrew@UBUNTU:~/Java$ cat AndrewsFrame.java
import java.awt.Frame;
public class AndrewsFrame extends Frame
{
public AndrewsFrame()
  {
  super("Andrew's Frame");
  setSize(500,300);
  setVisible(true);
  }
public static void main (String args[])
  {
  AndrewsFrame af = new AndrewsFrame();
  }
}
andrew@UBUNTU:~/Java$ javac AndrewsFrame.java
andrew@UBUNTU:~/Java$ java AndrewsFrame


This displayed the small empty frame shown below. I found that I could move it by dragging and dropping the title bar and minimize and maximize it too. I could also resize it by clicking the edge of the frame and dragging it in the usual way. However, to close the frame, I had to return to the UNIX prompt and type Ctrl+C. I hope to show a better way to do this in a future post. As usual, click on the image to enlarge it and bring it into focus:



 

Thursday 14 August 2014

Browser Based Dice Thrower Using Java

I wanted to use Java to create a simple dice simulator, which I could run in a browser, so I wrote the following program and compiled it. It generates a random integer between 1 and 6, converts it into a string then displays it:

andrew@UBUNTU:~/Java$ cat Dice.java
import java.awt.Graphics;
public class Dice
extends java.applet.Applet
{
public void paint(Graphics g)
  {
  int int1 = (int) (Math.random() * 6 + 1);
  String string1 = String.valueOf(int1);
  g.drawString(string1,20,40);
  }
}
andrew@UBUNTU:~/Java$ javac Dice.java
andrew@UBUNTU:~/Java$

Then I wrote a few lines of HTML:

andrew@UBUNTU:~/Java$ cat Dice.html
<html>
<head>
<title>Dice Thrower</title>
</head>
<body>
<applet name="Dice Thrower"
 code="Dice.class"
 width = 400 height = 200>
</applet>
</body>
</html>
andrew@UBUNTU:~/Java$ 

... so I could run it like this. Each time I reloaded the page, the program created a new number. As usual, click on the image to enlarge it and bring it into focus:


Finally, I loaded it on the free web space which comes with my home Internet connection. If you would like to try it yourself, click on the link below (it should look like the screen above):