Crear una aplicación que muestre un círculo en medio de la pantalla y
mediante dos botones permitir que se desplace a izquierda o derecha.
Descargar código.
Descargar ejecutable.
import java.awt.Color;
public class Ejemplo5 extends JFrame implements ActionListener{
private static final long serialVersionUID = -8651410754270577359L;
private JPanel contentPane;
private JButton btnUp;
private JButton btnLeft;
private JButton btnRight;
private JButton btnDown;
private int coord_x, coord_y;
private int tam_x, tam_y;
private JPanel panel_1;
private JButton btnMenos;
private JButton btnMas;
private JPanel panel_2;
private JButton btnCuadrado;
private JButton btnCirculo;
private String figura, color;
private JPanel panel_3;
private JButton btnRojo;
private JButton btnVerde;
private JButton btnAzul;
private JButton btnAmarillo;
private JButton btnNaranja;
public static void main(String[] args) {
Ejemplo5 frame = new Ejemplo5();
frame.setVisible(true);
}
public Ejemplo5() {
setResizable(false);
this.setCoord_x(100);
this.setCoord_y(100);
this.setTam_x(100);
this.setTam_y(100);
this.setFigura("circulo");
this.setColor("rojo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 647, 496);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JPanel panel = new JPanel();
panel.setBounds(359, 357, 212, 100);
panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
panel.setBackground(Color.LIGHT_GRAY);
panel_1 = new JPanel();
panel_1.setBounds(273, 357, 76, 100);
panel_1.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
panel_1.setBackground(Color.LIGHT_GRAY);
panel_2 = new JPanel();
panel_2.setBounds(149, 357, 114, 100);
panel_2.setBackground(Color.LIGHT_GRAY);
panel_2.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
panel_2.setLayout(null);
btnCuadrado = new JButton("Cuadrado");
btnCuadrado.setName("btnCuadrado");
btnCuadrado.setBounds(10, 11, 89, 23);
btnCuadrado.addActionListener(this);
panel_2.add(btnCuadrado);
btnCirculo = new JButton("Circulo");
btnCirculo.setName("btnCirculo");
btnCirculo.setBounds(10, 42, 89, 23);
btnCirculo.addActionListener(this);
panel_2.add(btnCirculo);
btnMenos = new JButton("-");
btnMenos.setName("btnMenos");
btnMenos.setBounds(17, 67, 42, 23);
btnMenos.setFont(new Font("Arial", Font.PLAIN, 11));
btnMenos.addActionListener(this);
btnMas = new JButton("+");
btnMas.setName("btnMas");
btnMas.setBounds(17, 10, 42, 23);
btnMas.setFont(new Font("Arial", Font.PLAIN, 11));
btnMas.addActionListener(this);
JLabel lblTamao = new JLabel("TAMA\u00D1O");
lblTamao.setBounds(15, 43, 45, 14);
lblTamao.setFont(new Font("Arial", Font.PLAIN, 11));
lblTamao.setHorizontalAlignment(SwingConstants.CENTER);
panel_1.setLayout(null);
panel_1.add(btnMenos);
panel_1.add(btnMas);
panel_1.add(lblTamao);
panel.setLayout(null);
btnUp = new JButton("Arriba");
btnUp.setFont(new Font("Arial", Font.PLAIN, 11));
btnUp.setName("btnUp");
btnUp.setBounds(71, 7, 70, 23);
panel.add(btnUp);
btnUp.addActionListener(this);
btnDown = new JButton("Abajo");
btnDown.setFont(new Font("Arial", Font.PLAIN, 11));
btnDown.setName("btnDown");
btnDown.setBounds(71, 67, 70, 23);
panel.add(btnDown);
btnDown.addActionListener(this);
btnLeft = new JButton("Izq.");
btnLeft.setFont(new Font("Arial", Font.PLAIN, 11));
btnLeft.setName("btnLeft");
btnLeft.setBounds(4, 38, 65, 23);
panel.add(btnLeft);
btnLeft.addActionListener(this);
btnRight = new JButton("Der.");
btnRight.setFont(new Font("Arial", Font.PLAIN, 11));
btnRight.setName("btnRight");
btnRight.setBounds(142, 38, 65, 23);
panel.add(btnRight);
JLabel lblDireccin = new JLabel("DIRECCI\u00D3N");
lblDireccin.setHorizontalAlignment(SwingConstants.CENTER);
lblDireccin.setFont(new Font("Arial", Font.PLAIN, 11));
lblDireccin.setBounds(73, 42, 65, 14);
panel.add(lblDireccin);
btnRight.addActionListener(this);
contentPane.setLayout(null);
contentPane.add(panel_2);
contentPane.add(panel_1);
contentPane.add(panel);
panel_3 = new JPanel();
panel_3.setBounds(581, 12, 50, 445);
contentPane.add(panel_3);
panel_3.setLayout(null);
btnRojo = new JButton("");
btnRojo.setIgnoreRepaint(true);
btnRojo.setBorderPainted(false);
btnRojo.setName("btnRojo");
btnRojo.setBackground(Color.RED);
btnRojo.setBounds(6, 11, 38, 38);
btnRojo.addActionListener(this);
panel_3.add(btnRojo);
btnVerde = new JButton("");
btnVerde.setName("btnVerde");
btnVerde.setIgnoreRepaint(true);
btnVerde.setBorderPainted(false);
btnVerde.setBackground(Color.GREEN);
btnVerde.setBounds(6, 60, 38, 38);
btnVerde.addActionListener(this);
panel_3.add(btnVerde);
btnAzul = new JButton("");
btnAzul.setName("btnAzul");
btnAzul.setIgnoreRepaint(true);
btnAzul.setBorderPainted(false);
btnAzul.setBackground(Color.BLUE);
btnAzul.setBounds(6, 109, 38, 38);
btnAzul.addActionListener(this);
panel_3.add(btnAzul);
btnAmarillo = new JButton("");
btnAmarillo.setName("btnAmarillo");
btnAmarillo.setIgnoreRepaint(true);
btnAmarillo.setBorderPainted(false);
btnAmarillo.setBackground(Color.YELLOW);
btnAmarillo.setBounds(6, 157, 38, 38);
btnAmarillo.addActionListener(this);
panel_3.add(btnAmarillo);
btnNaranja = new JButton("");
btnNaranja.setName("btnNaranja");
btnNaranja.setIgnoreRepaint(true);
btnNaranja.setBorderPainted(false);
btnNaranja.setBackground(Color.ORANGE);
btnNaranja.setBounds(6, 206, 38, 38);
btnNaranja.addActionListener(this);
panel_3.add(btnNaranja);
}
@Override
public void paint(Graphics g){
super.paint(g);
switch(this.getColor()){
case "rojo":
g.setColor(Color.RED);
break;
case "verde":
g.setColor(Color.GREEN);
break;
case "azul":
g.setColor(Color.BLUE);
break;
case "amarillo":
g.setColor(Color.YELLOW);
break;
case "naranja":
g.setColor(Color.ORANGE);
break;
}
switch (this.getFigura()){
case "circulo":
g.fillOval(this.getCoord_x(), this.getCoord_y(), this.getTam_x(), this.getTam_y());
break;
case "cuadrado":
g.fillRect(this.getCoord_x(), this.getCoord_y(), this.getTam_x(), this.getTam_y());
break;
}
}
@Override
public void actionPerformed(ActionEvent ev) {
if (ev.getSource() instanceof JButton){
JButton boton = (JButton) ev.getSource();
switch (boton.getName()){
case "btnUp":
this.setCoord_y(this.getCoord_y()-10);
break;
case "btnDown":
this.setCoord_y(this.getCoord_y()+10);
break;
case "btnLeft":
this.setCoord_x(this.getCoord_x()-10);
break;
case "btnRight":
this.setCoord_x(this.getCoord_x()+10);
break;
case "btnMas":
this.setTam_x(this.getTam_x()+10);
this.setTam_y(this.getTam_y()+10);
break;
case "btnMenos":
this.setTam_x(this.getTam_x()-10);
this.setTam_y(this.getTam_y()-10);
break;
case "btnCirculo":
this.setFigura("circulo");
break;
case "btnCuadrado":
this.setFigura("cuadrado");
break;
case "btnRojo":
this.setColor("rojo");
break;
case "btnVerde":
this.setColor("verde");
break;
case "btnAzul":
this.setColor("azul");
break;
case "btnAmarillo":
this.setColor("amarillo");
break;
case "btnNaranja":
this.setColor("naranja");
break;
}
repaint();
}
}
public int getCoord_x() {
return coord_x;
}
public void setCoord_x(int coord_x) {
this.coord_x = coord_x;
}
public int getCoord_y() {
return coord_y;
}
public void setCoord_y(int coord_y) {
this.coord_y = coord_y;
}
public int getTam_y() {
return tam_y;
}
public void setTam_y(int tam_y) {
this.tam_y = tam_y;
}
public int getTam_x() {
return tam_x;
}
public void setTam_x(int tam_x) {
this.tam_x = tam_x;
}
public String getFigura() {
return figura;
}
public void setFigura(String figura) {
this.figura = figura;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
No hay comentarios:
Publicar un comentario