import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Dibujar2 extends JFrame implements ActionListener {
/*crear una ventana que tenga 2 botones
* 1er boton crea un cuadrado con fondo rojo
* 2do boton cierra la aplicacion.
*/
public Dibujar2()
{
setSize(400,400);
setTitle("Dibujo");
Container contenedor=this.getContentPane();
contenedor.setLayout(new BorderLayout());
JPanel p=new JPanel();
contenedor.add(p,BorderLayout.SOUTH);
JButton boton=new JButton("Dibujar");
boton.addActionListener(this);
boton.setActionCommand("Dibujar");
p.add(boton);
JButton boton2=new JButton("Salir");
boton2.addActionListener(this);
boton2.setActionCommand("Salir");
p.add(boton2);
contenedor.add(panel,BorderLayout.CENTER);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String comando=e.getActionCommand();
if(comando.equals("Salir"))
{
System.exit(0);
}
Graphics g=panel.getGraphics();
g.setColor(Color.red);
g.fillRect(110, 90, 150, 150);
}
Mypanel panel=new Mypanel();
public static void main(String[] args) {
new Dibujar2();
}
}
class Mypanel extends JPanel{
public void painComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.blue);
g.drawLine(0, 0, 100, 100);
g.drawLine(150,50,(int)(this.getSize()).getWidth(),(int)(this.getSize()).getHeight());
g.drawRect(100,80,200,200);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Dibujar2 extends JFrame implements ActionListener {
/*crear una ventana que tenga 2 botones
* 1er boton crea un cuadrado con fondo rojo
* 2do boton cierra la aplicacion.
*/
public Dibujar2()
{
setSize(400,400);
setTitle("Dibujo");
Container contenedor=this.getContentPane();
contenedor.setLayout(new BorderLayout());
JPanel p=new JPanel();
contenedor.add(p,BorderLayout.SOUTH);
JButton boton=new JButton("Dibujar");
boton.addActionListener(this);
boton.setActionCommand("Dibujar");
p.add(boton);
JButton boton2=new JButton("Salir");
boton2.addActionListener(this);
boton2.setActionCommand("Salir");
p.add(boton2);
contenedor.add(panel,BorderLayout.CENTER);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String comando=e.getActionCommand();
if(comando.equals("Salir"))
{
System.exit(0);
}
Graphics g=panel.getGraphics();
g.setColor(Color.red);
g.fillRect(110, 90, 150, 150);
}
Mypanel panel=new Mypanel();
public static void main(String[] args) {
new Dibujar2();
}
}
class Mypanel extends JPanel{
public void painComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.blue);
g.drawLine(0, 0, 100, 100);
g.drawLine(150,50,(int)(this.getSize()).getWidth(),(int)(this.getSize()).getHeight());
g.drawRect(100,80,200,200);
}
}
No hay comentarios:
Publicar un comentario