rurtubia
5/24/2015 - 11:43 AM

Example of Java awt.Graphics and awt.Color libraries.

Example of Java awt.Graphics and awt.Color libraries.

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;

public class GraphicsDemo extends JFrame {

	// The constructor follows
	public GraphicsDemo() {
		setTitle("Graphics demo");
		setSize(800, 480);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	// Main method - necessary in all classes.
	public static void main(String[] args) {
		// Creation of a GraphicsDemo Object
		GraphicsDemo demo = new GraphicsDemo();
	}
	
	public void paint(Graphics g) {
		//This sets the color of g as white
		g.setColor(Color.WHITE);
		//The first statement creates the background statement to which the others are drawn
		g.fillRect(0, 0, 800, 480);
		//This sets the color of g as blue
		g.setColor(Color.BLUE);
		//This will draw the outline of a Blue rectangle, as the color of g when this is called is blue
		g.drawRect(60, 200, 100, 250);
		//This will set the color of g as black
		g.setColor(Color.BLACK);
		//This will display a black string
		g.drawString("Hello World", 200, 400);
	}
}
Uses Java.awt.Graphics to draw a rectangle and a string of text.
Uses Java.awt.Color to set foreground object colors and window background color.

Usa Java.awt.Graphics para dibujar un rectángulo y un string de texto.
Usa Java.awt.Color para asignar color a figuras y al fondo de la ventana.