Graphics




You can write fully window style programs in Java. You can also write programs that are fully graphical (games).

Examples:


Example 1: A Complete Program

// ********************************************************** 
// this is the main program to get things started

import javax.swing.JFrame;

public class GraphicsExamples extends JFrame
{
	private static final int WIDTH = 800;
	private static final int HEIGHT = 600;

	public GraphicsExamples()
	{
		super("Graphics Examples");
		
		setSize(WIDTH,HEIGHT);

		// getContentPane() controls which class draws on our screen
		getContentPane().add(new GraphicsWindow());		
		
		setVisible(true);
	}
	
	public static void main( String args[] )
	{
		GraphicsExamples run = new GraphicsExamples();
	}
}


// ********************************************************** 
// this is the class that actually does the drawing on the 
// window

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Canvas;
import java.util.*;
import java.awt.*;

public class GraphicsWindow extends Canvas
{
   public GraphicsWindow()    //constructor - sets up the class
   {
      setSize(800,600);
      setBackground(Color.WHITE);
      setVisible(true);
   }

   public void paint( Graphics window )
   {
   	  	Font font = new Font("Courier New",Font.BOLD,16);
   	  	window.setFont(font);
   	  
	  	window.setColor(Color.ORANGE);           

		window.drawString("Graphics Methods",35,20); // String, x, y

		window.setColor(Color.BLACK);

		window.drawString("setColor()       sets the draw color",35,75);
		window.drawString("drawString()     draws a String at x,y",35,100);

		window.drawString("drawRect()       draws a rectangle at x,y,width,height",35,135);
		window.drawString("fillRect()       draws and fills a rectangle at x,y,width,height",35,155);

		window.drawString("drawOval()       draws an oval inscribed at x,y,width,height",35,185);
		window.drawString("fillOval()       draws and fills an oval at x,y,width,height",35,210);

		window.drawString("drawLine()       draws a line at x1,y1,x2,y2",35,245);

		
		
		window.setColor(Color.MAGENTA);

		window.drawString("window.drawRect(25,500,25,25);",35,285);
		window.drawString("window.fillRect(125,500,25,25);",35,315);

		window.drawString("window.drawLine(200,500,225,520);",35,350);

		window.drawString("window.drawString(\"Hello\",295,500);",35,380);

		window.drawString("window.drawOval( 470, 500, 30, 20 );",35,420);
		window.drawString("window.fillOval( 550, 500, 30, 20 );",35,450);
		

		// now we will draw some stuff
		window.setColor(Color.RED);
		
		window.drawRect(25,500,25,25);        // x, y, width, height		
		window.fillRect(125,500,25,25);       // x, y, width, height
		
		window.drawLine(200,500,225,520);     // x1, y1, x2, y2
		window.drawString("Hello",295,500);   // String, x, y
		
		window.drawOval( 470, 500, 30, 20 );  // x, y, width, height
		window.fillOval( 550, 500, 30, 20 );  // x, y, width, height
      
   }

}



Example 2: A Code Segment that draws an image on the screen

// The following code segment will draw an image on the screen
// window is the reference to the Graphics library
// You will need the import statements shown below

import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;

// this would go in your paint method or some other method called by paint
try
{
    BufferedImage myPicture = ImageIO.read(new File("MyImage.GIF"));
    window.drawImage(myPicture,100,100,null);
}
catch(Exception e)
{
    System.out.println("Error reading MyImage.GIF");
}