miércoles, 21 de mayo de 2014

Dibujar un circulo

Esta aplicación en Java Swing, permite dibujar un circulo, en forma dinámica variando la posición del centro del mismo utilizando dos controles deslizables. Dejo en video demostrativo, si quieren el código, pueden escribirme a correo.first@gmail.com


public class dibujaCirculoCD extends JFrame  implements ActionListener, ChangeListener{
JPanel panel;
JSlider js1, js2, js3;
JButton boton;
JLabel coordx, coordy, lblx,lbly, lblradio;
int valorx, valory, radio;
public static void main(String[] args) {
dibujaCirculoCD marco = new dibujaCirculoCD();
marco.setSize(650, 650);
marco.setTitle("Dibuja circulo utilizando JSilder");
marco.dibujaGUI();
marco.setLocationRelativeTo(null);
marco.setVisible(true);

}
private void dibujaGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container ventana = getContentPane();
ventana.setLayout(new FlowLayout());
panel = new JPanel();
panel.setPreferredSize(new Dimension(730,510));
panel.setBackground(Color.white);
ventana.add(panel);
coordx = new JLabel("Coordenada x");
ventana.add(coordx);
js1 = new JSlider(JSlider.HORIZONTAL,100,400,150);
ventana.add(js1);
js1.addChangeListener(this);
lblx = new JLabel("x");
ventana.add(lblx);
coordy = new JLabel("Coordenada y");
ventana.add(coordy);
js2 = new JSlider(JSlider.HORIZONTAL,100,400,150);
ventana.add(js2);
js2.addChangeListener(this);
lbly = new JLabel("y");
ventana.add(lbly);
lblradio = new JLabel("      Radio");
ventana.add(lblradio);
js3 = new JSlider(JSlider.HORIZONTAL,10,200,15);
ventana.add(js3);
js3.addChangeListener(this);
lblradio = new JLabel("R");
ventana.add(lblradio);
boton = new JButton("Dibujar Circulo");
ventana.add(boton);
boton.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e) {
Graphics papel = panel.getGraphics();
papel.setColor(Color.white);
papel.fillRect(0, 0, 630, 510);
papel.setColor(Color.BLACK);
papel.drawOval(valorx, valory, radio, radio);
}

@Override
public void stateChanged(ChangeEvent e) {
if (e.getSource()== js1) {
valorx = js1.getValue();
lblx.setText(Integer.toString(valorx));
}
if (e.getSource()== js2) {
valory = js2.getValue();
lbly.setText(Integer.toString(valory));
}

if (e.getSource()== js3) {
radio = js3.getValue();
lblradio.setText(Integer.toString(radio));
}
}


}

No hay comentarios:

Publicar un comentario