import java.awt.*;
import javax.swing.JFrame;
import java.awt.event.*;
import javax.swing.JPanel;
import java.awt.Graphics;
// Prototipo de una aplicacon de ventana
// 1.- Se crea la clase que extienda a la clase Frame, JFrame o Window
public class Ventana_4
{
Ventana V4[] = new Ventana[4];
public static void main(String k[]) { new Ventana_4(); }
public Ventana_4()
{
// Ventana(Titulo, x, y, ancho, alto, tipo, tamanio);
// tipo 1 = circulo, tipo 2 = triangulo
// tamanio 0 = chico, tamanio 1 = grande
V4[0] = new Ventana("Circulo chico...", 10, 10, 600, 400, 1, 0);
V4[1] = new Ventana("Circulo grande...", 650, 10, 600, 400, 1, 1);
V4[2] = new Ventana("Triangulo grande...", 10, 450, 600, 400, 2, 1);
V4[3] = new Ventana("Triangulo chico...", 650, 450, 600, 400, 2, 0);
}
}
class Ventana extends JFrame
{
AreaDeDibujo Lienzo;
public Ventana(String Msg, int X, int Y, int Ancho, int Alto, int Tipo, int Tamanio)
{
this.getContentPane().setLayout(null); // Contenedor nulo
this.setTitle(Msg); // Titulo de la ventana
this.setResizable(false); // No es redimensionable
this.setLocation(X, Y); // Posicion desde la esquina superior izquierda del monitor
this.setSize(new Dimension(Ancho, Alto)); // Dimensiones
Lienzo = new AreaDeDibujo(Tipo, Tamanio, Ancho-10, Alto-10);
this.add(Lienzo);
Lienzo.setBounds(5,5,Ancho-10,Alto-10);
this.setVisible(true);
}
// 3.- Captura del evento "cerrar ventana"
protected void processWindowEvent(WindowEvent e)
{ super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } }
}
class AreaDeDibujo extends Canvas
{
int Tipo = 0, Tamanio = 0, Alto = 0, Ancho = 0;
public AreaDeDibujo(int Tipo, int Tamanio, int Alto, int Ancho)
{ this.Tipo = Tipo; this.Tamanio = Tamanio; this.Alto = Alto; this.Ancho = Ancho; }
public void paint(Graphics G)
{
G.clearRect(0, 0, Ancho, Alto);
if (Tamanio == 0) Tamanio = 80; else Tamanio = 200;
switch (Tipo)
{
case 1:
G.drawOval(Ancho/2 - Tamanio/2, Alto/2-Tamanio/2, Tamanio, Tamanio);
//(x,y,width,height);
break;
case 2:
int XP[] = {200, 50, 400};
int YP[] = {50, 50, 200};
G.drawPolygon(XP, YP, 3);
// drawPolygon(int[] xPoints, int[] yPoints, int nPoints);
break;
}
}
}