Showing posts with label vi. Show all posts
Showing posts with label vi. Show all posts

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:



 

Friday, 19 April 2013

How I Created a Simple Java Applet

I used vi to create a file like this:

UBUNTU > cat Andrew_Was_Here.java
// A simple applet.
import java.awt.Graphics;
public class Andrew_Was_Here
extends java.applet.Applet
{
public void paint(Graphics g)
  {
  g.drawString("Andrew Was Here!",20,40);
  }
}
UBUNTU >

I compiled it:

UBUNTU > javac Andrew_Was_Here.java
UBUNTU

Applets run within a browser so I wrote a few lines of HTML:

UBUNTU > cat Andrew_Was_Here.html
<html>
<head>
<title>Andrew Was Here</title>
</head>
<body>
<applet name="Andrew Was Here"
 code="Andrew_Was_Here.class"
 width = 400 height = 200>
</applet>
</body>
</html>
UBUNTU >


Then I typed the following command at the UBUNTU prompt:

appletviewer Andrew_Was_Here.html

... and the applet was displayed like this. As usual, click on the image to enlarge it and bring it into focus:

I was also able to run it in a browser as shown below: