Over a million developers have joined DZone.

Java 2D Graphics (AWT)

DZone's Guide to

Java 2D Graphics (AWT)

· ·
Free Resource
A basic hello world graphics program. Use to get started in 2D graphics programming.
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");
	public class ExitAdapter extends WindowAdapter
		public void windowClosing(WindowEvent e)

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}