Just wanted to say a quick thank you to all those that turned up last night for my birthday. I had a great time and hope you all did too, thanks for all the drinks!
Same time next year? lol
Sunday, 1 July 2007
Saturday, 30 June 2007
Java Snippet - Hello World Applet
I have had a request from a close friend for a tutorial on creating a simple Hello World Applet, so here we go (So don't say I never listen to my public).
First of all create a new Java file called "HelloWorld.java", you can do this using WordPad or any other text editor if you don't have a proper programming complier. If you would like a complier but don't know which one to get then I recommend NetBeans, its free, easy to use and has loads of great features which makes writing code a breeze.
Now for the code, in the new file we created put the following code:
And there you have your first applet. Obviously this is a very simplified version of an applet where we have only used a panel and a label but you can also add all sorts of other controls such as buttons, lists, text boxes, drop downs etc. In a future article I will talk about these as well as the different ways you can position your panels (you can have more than panel and position them within each other) and components.
Thats all I am going to write today because its my birthday and I have things to do, so you had better appreciate this tutorial lol.
First of all create a new Java file called "HelloWorld.java", you can do this using WordPad or any other text editor if you don't have a proper programming complier. If you would like a complier but don't know which one to get then I recommend NetBeans, its free, easy to use and has loads of great features which makes writing code a breeze.
Now for the code, in the new file we created put the following code:
import java.applet.*;
import java.awt.*;
public class HelloWorld extends Applet
{
private Panel helloPanel;
private Label helloLabel;
public void init()
{
// Create a new panel to contain out Label object
helloPanel = new Panel();
helloPanel.setLayout(new BorderLayout());
// Create a new label saying "Hello World" and centre it
helloLabel = new Label("Hello World", Label.CENTER);
// Added the newly created label to the centre of the panel
helloPanel.add(helloLabel, BorderLayout.CENTER);
// Added the new panel to the applet
add(helloPanel);
}
}
And there you have your first applet. Obviously this is a very simplified version of an applet where we have only used a panel and a label but you can also add all sorts of other controls such as buttons, lists, text boxes, drop downs etc. In a future article I will talk about these as well as the different ways you can position your panels (you can have more than panel and position them within each other) and components.
Thats all I am going to write today because its my birthday and I have things to do, so you had better appreciate this tutorial lol.
Tuesday, 26 June 2007
Java Snippet - Redirecting system output streams
Some people I know have been asking me to post tutorials on C++ and Java, but instead of doing a full tutorial I thought I might just post helpful little snippets of code that you can use in your programs.
The first of these is a neat bit of Java code that redirects calls to System.out and System.err. So when you use System.out instead of printing to the console it actually prints to a text file, this is great for log files.
First of all you need to add the following variables to your Java program:
Then add the following function/method:
This function is the first thing I call in my application, that way all subsequent calls to System.out and System.err will write to the appropriate log files. The function itself is pretty straight forward.
First of all we try and open a stream to the desired log file, we do this on this line:
This opens the file specified by "mypath/mylogname.log" and readies it for printing, the true value just tells the function to add the new text to the end of any existing text in this file, perfect for logs, if you want it to wipe the file and start fresh each time just set this flag to false.
Next if we are successful we tell System.out to print to the new file we just opened instead of the console, to do this we call the following:
The rest of the code explains itself really, its just simple error checking so that if the program cant open the log files it will fallback to printing to the console and not expload.
NOTE: If you are using this code in an applet you will need to make the applet files into a .jar file and then sign the .jar file with a appropriate security certificate otherwise web browsers may not allow your applet to write files to disk. View this page for details on .jar files
Packaging Programs in JAR Files
I hope this code was of use to someone out there, maybe I will post something more exciting next time... but maybe not
The first of these is a neat bit of Java code that redirects calls to System.out and System.err. So when you use System.out instead of printing to the console it actually prints to a text file, this is great for log files.
First of all you need to add the following variables to your Java program:
private PrintStream logFile;
private PrintStream errFile;
Then add the following function/method:
public void initLogs()
{
// Try to redirect system output to log file
try
{
logFile = new PrintStream(new FileOutputStream("mypath/mylogname.log", true), true);
}
catch (FileNotFoundException e)
{
System.err.println("Error: Could not find log file!");
}
catch (IOException e)
{
System.err.println("Error: Could not open log file!");
e.printStackTrace();
}
// Fail gracefully
if (logFile != null)
{
System.setOut(logFile);
}
// Try to redirect system errors to error log file
try
{
errFile = new PrintStream(new FileOutputStream("mypathname/mylogname.err.log", true), true);
}
catch (FileNotFoundException e)
{
System.err.println("Error: Could not find log file!");
}
catch (IOException e)
{
System.err.println("Error: Could not open log file!");
e.printStackTrace();
}
// Fail gracefully
if (errFile != null)
{
System.setErr(errFile);
}
}
This function is the first thing I call in my application, that way all subsequent calls to System.out and System.err will write to the appropriate log files. The function itself is pretty straight forward.
First of all we try and open a stream to the desired log file, we do this on this line:
logFile = new PrintStream(new FileOutputStream("mypath/mylogname.log", true);
This opens the file specified by "mypath/mylogname.log" and readies it for printing, the true value just tells the function to add the new text to the end of any existing text in this file, perfect for logs, if you want it to wipe the file and start fresh each time just set this flag to false.
Next if we are successful we tell System.out to print to the new file we just opened instead of the console, to do this we call the following:
System.setOut(logFile);
The rest of the code explains itself really, its just simple error checking so that if the program cant open the log files it will fallback to printing to the console and not expload.
NOTE: If you are using this code in an applet you will need to make the applet files into a .jar file and then sign the .jar file with a appropriate security certificate otherwise web browsers may not allow your applet to write files to disk. View this page for details on .jar files
Packaging Programs in JAR Files
I hope this code was of use to someone out there, maybe I will post something more exciting next time... but maybe not
Sunday, 24 June 2007
The Beginning
Here it begins, the Blog of IronBat!
The possibilities are endless, the chance for fame and glory awaits. More than likely this venture into blogging will come to nothing though. We shall see...
The possibilities are endless, the chance for fame and glory awaits. More than likely this venture into blogging will come to nothing though. We shall see...
Subscribe to:
Posts (Atom)