DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Nick has posted 1 posts at DZone. View Full User Profile

Java 2D Graphics (AWT)

06.15.2012
| 5756 views |
  • submit to reddit
package com.dyrio.graphics;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Graphics1 extends Frame {

	private static final long serialVersionUID = 1L;
	private int xPosition = 100;
	private int yPosition = 50;
	private static final int xSize = 300;
	private static final int ySize = 100;
	private static Graphics2D graphics2D;
	private static final String MESSAGE = "Welcome to 2D Graphics";
	
	public Graphics1() {
		addWindowListener(new ExitAdapter());
	}
	
	public void paint(Graphics g) {
		graphics2D = (Graphics2D) g;
		graphics2D.drawString(MESSAGE, xPosition, yPosition);
	}
	
	public static void main(String[] args) {
		Graphics1 graphics1 = new Graphics1();
		graphics1.setTitle("Graphics 1");
		graphics1.setSize(xSize,ySize);
		graphics1.setVisible(true);
	}
	
	public class ExitAdapter extends WindowAdapter
	{
		public void windowClosing(WindowEvent e)
	     {
	       System.exit(0);
	     }
	}
}
A basic hello world graphics program. Use to get started in 2D graphics programming.