Last modified 8 years ago Last modified on 2010-09-05 14:31:21


Pages linking to GraphicsInEnglish:

Graphics in finish

Graphics is desingned for course Programming 1 to be very easy to use library to make basic graphics. Still it is quite powerfull.

1. Usage

1.1 Library

  • copy the file Graphics.jar. Hint: Click the link by mouse right button and choose 'Save as...'.

1.2 Compliling and running from command line

  • For example the below source for SimpleGraphics can be compiled and run by:
  • do first the file and copy the code from example the code.
  • give the commands:
    javac -cp Graphics.jar
    java -cp .;Graphics.jar SimpleGraphics
    Remember that in Unix- ja Linux the path is separated by :, so on those you give commands
    java -cp .:Graphics.jar SimpleGraphics

1.3 A bit more easier way when repeated many times

  • It might be idea to add Graphics.jar to classpath in Windowsissa by command (notice . at the end, it must be there)
    set CLASSPATH=.;Graphics.jar
  • This command should be repeated every time the command prompt is rerun.
  • Graphics.jar can be on other place also, then you just need to tell the whole path: set CLASSPATH=.;n:\kurssit\ohj1\graphics\Graphics.jar
  • in Linux the corresponding command is:
    export CLASSPATH=.:Graphics.jar
  • After setting the path the compile and run is done by commands:
    java  SimpleGraphics

1.4 Uasge in Eclipse

  • if needed, create a new Java-project and add there a new class
  • press proects name be right button and choose Properties
  • Java Build Path/Libraries/Add? External JARs...
  • add Graphics.jar
  • run the program

2. Example of use

2.1 Simple example

  • source:/graphics/trunk/sample/SimpleGraphics.png source:/graphics/trunk/sample/SimpleGraphicsCoord.png
    public class SimpleGraphics {
    	public static void main(String[] args) {
    		EasyWindow window = new EasyWindow();
    		window.addCircle(50,50, 20);

2.2 More properties


public class GraphicsTest {
    public static void main(String[] args) {
        EasySpace space = new EasySpace(); //Create a spce where objects are added

	    double points[][] = {
	    	{ 0, -2 , 1}, 
	        { 2, -2 , 2},
	        { 2,  0 , 3},
	        { 0,  2 , 2},
	        {-2,  0 , 1}

	    space.addPolygon(points).setColor(255, 0, 0); //3D-polygon

	    space.addCircle(0, 0, 2).setColor(0, 255, 0);

	    space.addLine(-2.7, 2.5, 1, 1).setColor(0, 0 , 255);

	    space.addCircle(0, -2.5, 0.2);

	    Window window = new Window();
	    window.scale(-3, -3, 3, 3);
	    space.addAxis(5, 5, 5); //x-, y- ja z-axes that have lenght 5

	    window.setSpace(space); //Set window to show earlier created space


	    EasyWindow w2 = new EasyWindow(400, 400);
	    w2.scale(-5, -5, 5, 5);

	    w2.rotate(Axis.Y, 45); //Rotate along y-axes
	    w2.rotate(Axis.Z, 45); //Rotate along z-axes


	    space.addCircle(0, 0, -3, 1).setColor(192, 52, 200);

2.3 Special properties

If you want to run examples thru menu, give command:

  java -jar Graphics.jar

If you want to run examples below (suppose CLASSPATH is set), then give gommand (change the class name if needed):

  java  sample.CarSample

In the example there is two control windows. The other controls the picture orientation and the other the man orientation. If you check the automatic chackbox, then it will continuously rotate along checked axes.

"TouchPad" gives easy way to control the orientation. On the other window it works mouse pressed and on the other without.

Jos haluat kokeilla JA muokata alla olevia esimerkkejä, lataa koodi Lähdekoodi-linkista ja siellä Plain Text -linkistä hakemistoon sample (luo itsellesi tällainen hakemisto, jos sinulla ei vielä ole sellaista). Oletetaan että sinulla on CLASSPATH asetettuna. Oletetaan että olet muokannut tiedostoa graphicsSample/ ja olet tallentanut sen nimelle sample/ Silloin käännökset ja ajo esimerkiksi komennoilla jotka annetaan siis sample-hakemiston isä-hakemistossa:

If you want to try AND modify examples below, load the code from "Source code" -link and there Plain Text to directory called sample (create on if you do not have it already). Suppose you have CLASSPATH set. Supose you have edited file sample/ and have saved it with name sample/ Then the command to compile and run are given in parent directory to sample-directory:

  javac sample/
  java  sample.ManSample2