Java by Example - basic graphics functions
Welcome! I'm Johannes Wallroth. I want to try and teach you Java, this new and exciting programming language. I think everybody can learn it, and I hope my tutorial will make it easy and fun. You don't have to have programmed before. I will introduce a series of fairly simple examples, in order to enable you to get a grasp of the possibilities and the syntax of the Java language, and I'll try to explain everything you need to know about them. We are going to use version 1.0 of the language, because I think it is much easier to learn than the later versions, especially without prior knowledge of C++, and you can easily switch to newer versions, once you have learned the basics. I'm going to show only those parts that are most common for games and graphical applets. To explore the possibilities further, I recommend using the API documentation that I have bundled with the Java Development Kit.
If you are an absolute beginner, you will possibly have to look for additional tutorials on the web or in books, although I'll try to cover most of the basics as well. Please read also the comments inside the sourcecodes (lines beginning with // are comments, colored red in the online version). Don't get confused if you don't know what some lines of code mean, just try to follow the examples and make changes to them, to see what they are doing. In a short time, everything will come naturally to you. Most of my example applets are 300 x 300 pixels in size, but you can easily change this, by editing the code inside the HTML-file of the project. Just look for the line "APPLET CODE=XYZ.class WIDTH=300 HEIGHT=300" and change the width and height parameters.
If you want to recompile any of my applets, please make sure your main class (only class in the simple applets) has the same name as the one you have entered in the project settings! If you get an error message pointing to the class name, you must change the name of the class to the one you have entered in the project settings.
|
Click here to download all the Java source files of the following examples.
You can download this tutorial in a zip-file here (313 kB).
The first example applet shows you how to use the most common graphic functions in Java like circles, rectangles, lines and text. Here you can also see how polygons are done. I you don't specify a font size and style, then this is how standard text shows up (in black if you don't specify a color). This applet has only one standard method: paint, which is used in every graphical applet.
//Sourcecode
import java.awt.*;
import java.applet.*;
//This project demonstrates the basic drawing functions in Java
//Predifined colors in Java are: black, blue, cyan, darkGray, gray,
//green, lightGray, magenta, orange, pink, red, white and yellow
public class Project1 extends Applet
{
public void paint (Graphics g)
{
//Paint Background black (300 x 300 Pixels)
g.setColor(Color.black);
g.fillRect(0,0,300,300);
//Red square
g.setColor(Color.red);
g.fillRect(0,0,50,50);
//Yellow circle
g.setColor(Color.yellow);
g.fillOval(100,10,100,100);
//Orange oval
g.setColor(Color.orange);
g.fillOval(230,50,50,70);
//Blue rectangle with round corners
g.setColor(Color.blue);
g.fillRoundRect(10,130,160,60,20,20);
//Cyan colored triangle - not so easy
//You can make polygons with more corners the same way
g.setColor(Color.cyan);
//set all x-coordinates of the corners
int x[] = {20,80,140};
//set all y-coordinates of the corners
int y[] = {270,200,270};
//the third parameter is the number of corners
g.fillPolygon(x,y,3);
//Green line
g.setColor(Color.green);
g.drawLine(0,0,300,300);
//Some white text
g.setColor(Color.white);
g.drawString("This is some Text",180,250);
}
}
|
Easy, isn't it? Java has all you need for colorful graphics built right into it, you don't need to import special libraries.
|