Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Graphics;
- import java.io.*;
- import javax.swing.*;
- public class PintaFiguras extends javax.swing.JFrame {
- int X1,X2,Y1,Y2,figura=0;
- Color kolor = new Color(000);
- int puntos[]=new int [7];
- int relle=0;
- public PintaFiguras() {
- initComponents();
- }
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- about = new javax.swing.JFrame();
- jLabel1 = new javax.swing.JLabel();
- jLabel2 = new javax.swing.JLabel();
- jLabel3 = new javax.swing.JLabel();
- jLabel4 = new javax.swing.JLabel();
- color = new javax.swing.JFrame();
- kolores = new javax.swing.JColorChooser();
- Pintar = new javax.swing.JButton();
- dialogo = new javax.swing.JFileChooser();
- jMenuBar1 = new javax.swing.JMenuBar();
- Archivo = new javax.swing.JMenu();
- Guardar = new javax.swing.JMenuItem();
- Abrir = new javax.swing.JMenuItem();
- Salir = new javax.swing.JMenuItem();
- Figuras = new javax.swing.JMenu();
- Linea = new javax.swing.JMenuItem();
- Rectangulo = new javax.swing.JMenuItem();
- Ovalo = new javax.swing.JMenuItem();
- Formato = new javax.swing.JMenu();
- Colores = new javax.swing.JMenuItem();
- Relleno = new javax.swing.JCheckBoxMenuItem();
- Ayuda = new javax.swing.JMenu();
- About = new javax.swing.JMenuItem();
- about.setMinimumSize(new java.awt.Dimension(300, 400));
- jLabel1.setText("Programa que pretende dibujar figuras...");
- jLabel2.setText("Version 1.0");
- jLabel3.setText("IIND. Juan Carlos H.");
- jLabel4.setText("14220");
- org.jdesktop.layout.GroupLayout aboutLayout = new org.jdesktop.layout.GroupLayout(about.getContentPane());
- about.getContentPane().setLayout(aboutLayout);
- aboutLayout.setHorizontalGroup(
- aboutLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(aboutLayout.createSequentialGroup()
- .add(35, 35, 35)
- .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 466, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
- .addContainerGap(445, Short.MAX_VALUE))
- .add(org.jdesktop.layout.GroupLayout.TRAILING, aboutLayout.createSequentialGroup()
- .addContainerGap(658, Short.MAX_VALUE)
- .add(aboutLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(jLabel4)
- .add(jLabel3)
- .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 203, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
- .add(85, 85, 85))
- );
- aboutLayout.setVerticalGroup(
- aboutLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(aboutLayout.createSequentialGroup()
- .add(40, 40, 40)
- .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 416, Short.MAX_VALUE)
- .add(jLabel2)
- .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
- .add(jLabel3)
- .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
- .add(jLabel4)
- .add(38, 38, 38))
- );
- color.setMinimumSize(new java.awt.Dimension(800, 800));
- Pintar.setText("Pintar");
- Pintar.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- PintarActionPerformed(evt);
- }
- });
- org.jdesktop.layout.GroupLayout colorLayout = new org.jdesktop.layout.GroupLayout(color.getContentPane());
- color.getContentPane().setLayout(colorLayout);
- colorLayout.setHorizontalGroup(
- colorLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(colorLayout.createSequentialGroup()
- .add(372, 372, 372)
- .add(Pintar)
- .addContainerGap(480, Short.MAX_VALUE))
- .add(kolores, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 932, Short.MAX_VALUE)
- );
- colorLayout.setVerticalGroup(
- colorLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(colorLayout.createSequentialGroup()
- .addContainerGap()
- .add(kolores, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 463, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
- .add(18, 18, 18)
- .add(Pintar)
- .addContainerGap())
- );
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- addMouseListener(new java.awt.event.MouseAdapter() {
- public void mousePressed(java.awt.event.MouseEvent evt) {
- formMousePressed(evt);
- }
- public void mouseReleased(java.awt.event.MouseEvent evt) {
- formMouseReleased(evt);
- }
- });
- Archivo.setText("Archivo");
- Guardar.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_G, java.awt.event.InputEvent.ALT_MASK));
- Guardar.setText("Guardar");
- Archivo.add(Guardar);
- Abrir.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.ALT_MASK));
- Abrir.setText("Abrir");
- Archivo.add(Abrir);
- Salir.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.ALT_MASK));
- Salir.setText("Salir");
- Salir.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- SalirActionPerformed(evt);
- }
- });
- Archivo.add(Salir);
- jMenuBar1.add(Archivo);
- Figuras.setText("Figuras");
- Linea.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.ALT_MASK));
- Linea.setText("Linea");
- Linea.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- LineaActionPerformed(evt);
- }
- });
- Figuras.add(Linea);
- Rectangulo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, java.awt.event.InputEvent.ALT_MASK));
- Rectangulo.setText("Rectangulo");
- Rectangulo.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- RectanguloActionPerformed(evt);
- }
- });
- Figuras.add(Rectangulo);
- Ovalo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.ALT_MASK));
- Ovalo.setText("Ovalo");
- Ovalo.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- OvaloActionPerformed(evt);
- }
- });
- Figuras.add(Ovalo);
- jMenuBar1.add(Figuras);
- Formato.setText("Formato");
- Colores.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.ALT_MASK));
- Colores.setText("Colores");
- Colores.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- ColoresActionPerformed(evt);
- }
- });
- Formato.add(Colores);
- Relleno.setSelected(true);
- Relleno.setText("Relleno");
- Relleno.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- RellenoActionPerformed(evt);
- }
- });
- Formato.add(Relleno);
- jMenuBar1.add(Formato);
- Ayuda.setText("Ayuda");
- About.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.ALT_MASK));
- About.setText("Acerca de...");
- About.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- AboutActionPerformed(evt);
- }
- });
- Ayuda.add(About);
- jMenuBar1.add(Ayuda);
- setJMenuBar(jMenuBar1);
- org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(0, 1200, Short.MAX_VALUE)
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(0, 606, Short.MAX_VALUE)
- );
- pack();
- }// </editor-fold>
- private void SalirActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- System.exit(0);
- }
- private void formMousePressed(java.awt.event.MouseEvent evt) {
- // TODO add your handling code here:
- X1=evt.getX();
- Y1=evt.getY();
- }
- private void LineaActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- figura=1;
- }
- private void RectanguloActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- figura=2;
- }
- private void OvaloActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- figura=3;
- }
- private void formMouseReleased(java.awt.event.MouseEvent evt) {
- // TODO add your handling code here:
- X2=evt.getX();
- Y2=evt.getY();
- repaint();
- }
- private void AboutActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- about.show();
- }
- private void ColoresActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- color.show();
- }
- private void RellenoActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- if (Relleno.isSelected())
- {
- relle=1;
- }
- else relle=0;
- }
- private void PintarActionPerformed(java.awt.event.ActionEvent evt) {
- kolor = kolores.getColor();
- repaint();// TODO add your handling code here:
- color.setVisible(false);
- }
- public void paint( Graphics g)
- {
- super.paint (g);
- g.setColor(kolor);
- switch (figura)
- {
- case 1: {g.drawLine(X1, Y1, X2, Y2);break;}
- case 2:
- {
- if ((X1<X2)&&(Y1<Y2))
- {g.drawRect(X1, Y1, (X2-X1),(Y2-Y1));}
- else if ((X1>X2)&&(Y1>Y2))
- {g.drawRect(X2,Y2,(X1-X2),(Y1-Y2));}
- else if ((X1>X2)&&(Y1<Y2))
- {g.drawRect(X2,Y1,(X1-X2),(Y2-Y1));}
- else if ((X1<X2)&&(Y1>Y2))
- {g.drawRect(X1,Y2,(X2-X1),(Y1-Y2));}
- //g.fillRect(X1, Y1, X2, Y2);
- break;
- }
- case 3: {g.drawOval(X1, Y1, X2, Y2);break;}
- case 4:
- {
- if ((X1<X2)&&(Y1<Y2))
- {g.drawRect(X1, Y1, (X2-X1),(Y2-Y1));}
- else if ((X1>X2)&&(Y1>Y2))
- {g.drawRect(X2,Y2,(X1-X2),(Y1-Y2));}
- else if ((X1>X2)&&(Y1<Y2))
- {g.drawRect(X2,Y1,(X1-X2),(Y2-Y1));}
- else if ((X1<X2)&&(Y1>Y2))
- {g.drawRect(X1,Y2,(X2-X1),(Y1-Y2));}
- }
- }
- }
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(PintaFiguras.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(PintaFiguras.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(PintaFiguras.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(PintaFiguras.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new PintaFiguras().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JMenuItem About;
- private javax.swing.JMenuItem Abrir;
- private javax.swing.JMenu Archivo;
- private javax.swing.JMenu Ayuda;
- private javax.swing.JMenuItem Colores;
- private javax.swing.JMenu Figuras;
- private javax.swing.JMenu Formato;
- private javax.swing.JMenuItem Guardar;
- private javax.swing.JMenuItem Linea;
- private javax.swing.JMenuItem Ovalo;
- private javax.swing.JButton Pintar;
- private javax.swing.JMenuItem Rectangulo;
- private javax.swing.JCheckBoxMenuItem Relleno;
- private javax.swing.JMenuItem Salir;
- private javax.swing.JFrame about;
- private javax.swing.JFrame color;
- private javax.swing.JFileChooser dialogo;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JLabel jLabel3;
- private javax.swing.JLabel jLabel4;
- private javax.swing.JMenuBar jMenuBar1;
- private javax.swing.JColorChooser kolores;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment