Xtrminator

PintaFiguras

May 17th, 2012
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.io.*;
  4. import javax.swing.*;
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11. public class PintaFiguras extends javax.swing.JFrame {
  12.  
  13. int X1,X2,Y1,Y2,figura=0;
  14. Color kolor = new Color(000);
  15.  
  16. int puntos[]=new int [7];
  17. int relle=0;
  18.  
  19. public PintaFiguras() {
  20. initComponents();
  21. }
  22.  
  23.  
  24. @SuppressWarnings("unchecked")
  25. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  26. private void initComponents() {
  27.  
  28. about = new javax.swing.JFrame();
  29. jLabel1 = new javax.swing.JLabel();
  30. jLabel2 = new javax.swing.JLabel();
  31. jLabel3 = new javax.swing.JLabel();
  32. jLabel4 = new javax.swing.JLabel();
  33. color = new javax.swing.JFrame();
  34. kolores = new javax.swing.JColorChooser();
  35. Pintar = new javax.swing.JButton();
  36. dialogo = new javax.swing.JFileChooser();
  37. jMenuBar1 = new javax.swing.JMenuBar();
  38. Archivo = new javax.swing.JMenu();
  39. Guardar = new javax.swing.JMenuItem();
  40. Abrir = new javax.swing.JMenuItem();
  41. Salir = new javax.swing.JMenuItem();
  42. Figuras = new javax.swing.JMenu();
  43. Linea = new javax.swing.JMenuItem();
  44. Rectangulo = new javax.swing.JMenuItem();
  45. Ovalo = new javax.swing.JMenuItem();
  46. Formato = new javax.swing.JMenu();
  47. Colores = new javax.swing.JMenuItem();
  48. Relleno = new javax.swing.JCheckBoxMenuItem();
  49. Ayuda = new javax.swing.JMenu();
  50. About = new javax.swing.JMenuItem();
  51.  
  52. about.setMinimumSize(new java.awt.Dimension(300, 400));
  53.  
  54. jLabel1.setText("Programa que pretende dibujar figuras...");
  55.  
  56. jLabel2.setText("Version 1.0");
  57.  
  58. jLabel3.setText("IIND. Juan Carlos H.");
  59.  
  60. jLabel4.setText("14220");
  61.  
  62. org.jdesktop.layout.GroupLayout aboutLayout = new org.jdesktop.layout.GroupLayout(about.getContentPane());
  63. about.getContentPane().setLayout(aboutLayout);
  64. aboutLayout.setHorizontalGroup(
  65. aboutLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  66. .add(aboutLayout.createSequentialGroup()
  67. .add(35, 35, 35)
  68. .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 466, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
  69. .addContainerGap(445, Short.MAX_VALUE))
  70. .add(org.jdesktop.layout.GroupLayout.TRAILING, aboutLayout.createSequentialGroup()
  71. .addContainerGap(658, Short.MAX_VALUE)
  72. .add(aboutLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  73. .add(jLabel4)
  74. .add(jLabel3)
  75. .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 203, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
  76. .add(85, 85, 85))
  77. );
  78. aboutLayout.setVerticalGroup(
  79. aboutLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  80. .add(aboutLayout.createSequentialGroup()
  81. .add(40, 40, 40)
  82. .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
  83. .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 416, Short.MAX_VALUE)
  84. .add(jLabel2)
  85. .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
  86. .add(jLabel3)
  87. .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
  88. .add(jLabel4)
  89. .add(38, 38, 38))
  90. );
  91.  
  92. color.setMinimumSize(new java.awt.Dimension(800, 800));
  93.  
  94. Pintar.setText("Pintar");
  95. Pintar.addActionListener(new java.awt.event.ActionListener() {
  96. public void actionPerformed(java.awt.event.ActionEvent evt) {
  97. PintarActionPerformed(evt);
  98. }
  99. });
  100.  
  101. org.jdesktop.layout.GroupLayout colorLayout = new org.jdesktop.layout.GroupLayout(color.getContentPane());
  102. color.getContentPane().setLayout(colorLayout);
  103. colorLayout.setHorizontalGroup(
  104. colorLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  105. .add(colorLayout.createSequentialGroup()
  106. .add(372, 372, 372)
  107. .add(Pintar)
  108. .addContainerGap(480, Short.MAX_VALUE))
  109. .add(kolores, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 932, Short.MAX_VALUE)
  110. );
  111. colorLayout.setVerticalGroup(
  112. colorLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  113. .add(colorLayout.createSequentialGroup()
  114. .addContainerGap()
  115. .add(kolores, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 463, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
  116. .add(18, 18, 18)
  117. .add(Pintar)
  118. .addContainerGap())
  119. );
  120.  
  121. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  122. addMouseListener(new java.awt.event.MouseAdapter() {
  123. public void mousePressed(java.awt.event.MouseEvent evt) {
  124. formMousePressed(evt);
  125. }
  126. public void mouseReleased(java.awt.event.MouseEvent evt) {
  127. formMouseReleased(evt);
  128. }
  129. });
  130.  
  131. Archivo.setText("Archivo");
  132.  
  133. Guardar.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_G, java.awt.event.InputEvent.ALT_MASK));
  134. Guardar.setText("Guardar");
  135. Archivo.add(Guardar);
  136.  
  137. Abrir.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.ALT_MASK));
  138. Abrir.setText("Abrir");
  139. Archivo.add(Abrir);
  140.  
  141. Salir.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.ALT_MASK));
  142. Salir.setText("Salir");
  143. Salir.addActionListener(new java.awt.event.ActionListener() {
  144. public void actionPerformed(java.awt.event.ActionEvent evt) {
  145. SalirActionPerformed(evt);
  146. }
  147. });
  148. Archivo.add(Salir);
  149.  
  150. jMenuBar1.add(Archivo);
  151.  
  152. Figuras.setText("Figuras");
  153.  
  154. Linea.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.ALT_MASK));
  155. Linea.setText("Linea");
  156. Linea.addActionListener(new java.awt.event.ActionListener() {
  157. public void actionPerformed(java.awt.event.ActionEvent evt) {
  158. LineaActionPerformed(evt);
  159. }
  160. });
  161. Figuras.add(Linea);
  162.  
  163. Rectangulo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, java.awt.event.InputEvent.ALT_MASK));
  164. Rectangulo.setText("Rectangulo");
  165. Rectangulo.addActionListener(new java.awt.event.ActionListener() {
  166. public void actionPerformed(java.awt.event.ActionEvent evt) {
  167. RectanguloActionPerformed(evt);
  168. }
  169. });
  170. Figuras.add(Rectangulo);
  171.  
  172. Ovalo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.ALT_MASK));
  173. Ovalo.setText("Ovalo");
  174. Ovalo.addActionListener(new java.awt.event.ActionListener() {
  175. public void actionPerformed(java.awt.event.ActionEvent evt) {
  176. OvaloActionPerformed(evt);
  177. }
  178. });
  179. Figuras.add(Ovalo);
  180.  
  181. jMenuBar1.add(Figuras);
  182.  
  183. Formato.setText("Formato");
  184.  
  185. Colores.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.ALT_MASK));
  186. Colores.setText("Colores");
  187. Colores.addActionListener(new java.awt.event.ActionListener() {
  188. public void actionPerformed(java.awt.event.ActionEvent evt) {
  189. ColoresActionPerformed(evt);
  190. }
  191. });
  192. Formato.add(Colores);
  193.  
  194. Relleno.setSelected(true);
  195. Relleno.setText("Relleno");
  196. Relleno.addActionListener(new java.awt.event.ActionListener() {
  197. public void actionPerformed(java.awt.event.ActionEvent evt) {
  198. RellenoActionPerformed(evt);
  199. }
  200. });
  201. Formato.add(Relleno);
  202.  
  203. jMenuBar1.add(Formato);
  204.  
  205. Ayuda.setText("Ayuda");
  206.  
  207. About.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.ALT_MASK));
  208. About.setText("Acerca de...");
  209. About.addActionListener(new java.awt.event.ActionListener() {
  210. public void actionPerformed(java.awt.event.ActionEvent evt) {
  211. AboutActionPerformed(evt);
  212. }
  213. });
  214. Ayuda.add(About);
  215.  
  216. jMenuBar1.add(Ayuda);
  217.  
  218. setJMenuBar(jMenuBar1);
  219.  
  220. org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
  221. getContentPane().setLayout(layout);
  222. layout.setHorizontalGroup(
  223. layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  224. .add(0, 1200, Short.MAX_VALUE)
  225. );
  226. layout.setVerticalGroup(
  227. layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  228. .add(0, 606, Short.MAX_VALUE)
  229. );
  230.  
  231. pack();
  232. }// </editor-fold>
  233.  
  234. private void SalirActionPerformed(java.awt.event.ActionEvent evt) {
  235. // TODO add your handling code here:
  236. System.exit(0);
  237. }
  238.  
  239. private void formMousePressed(java.awt.event.MouseEvent evt) {
  240. // TODO add your handling code here:
  241. X1=evt.getX();
  242. Y1=evt.getY();
  243.  
  244.  
  245. }
  246.  
  247. private void LineaActionPerformed(java.awt.event.ActionEvent evt) {
  248. // TODO add your handling code here:
  249. figura=1;
  250. }
  251.  
  252. private void RectanguloActionPerformed(java.awt.event.ActionEvent evt) {
  253. // TODO add your handling code here:
  254. figura=2;
  255. }
  256.  
  257. private void OvaloActionPerformed(java.awt.event.ActionEvent evt) {
  258. // TODO add your handling code here:
  259. figura=3;
  260. }
  261.  
  262. private void formMouseReleased(java.awt.event.MouseEvent evt) {
  263. // TODO add your handling code here:
  264. X2=evt.getX();
  265. Y2=evt.getY();
  266. repaint();
  267. }
  268.  
  269. private void AboutActionPerformed(java.awt.event.ActionEvent evt) {
  270. // TODO add your handling code here:
  271. about.show();
  272.  
  273. }
  274.  
  275. private void ColoresActionPerformed(java.awt.event.ActionEvent evt) {
  276. // TODO add your handling code here:
  277. color.show();
  278.  
  279. }
  280.  
  281. private void RellenoActionPerformed(java.awt.event.ActionEvent evt) {
  282. // TODO add your handling code here:
  283. if (Relleno.isSelected())
  284. {
  285. relle=1;
  286. }
  287. else relle=0;
  288.  
  289. }
  290.  
  291. private void PintarActionPerformed(java.awt.event.ActionEvent evt) {
  292. kolor = kolores.getColor();
  293. repaint();// TODO add your handling code here:
  294. color.setVisible(false);
  295. }
  296.  
  297. public void paint( Graphics g)
  298. {
  299. super.paint (g);
  300. g.setColor(kolor);
  301. switch (figura)
  302. {
  303. case 1: {g.drawLine(X1, Y1, X2, Y2);break;}
  304.  
  305. case 2:
  306. {
  307. if ((X1<X2)&&(Y1<Y2))
  308. {g.drawRect(X1, Y1, (X2-X1),(Y2-Y1));}
  309. else if ((X1>X2)&&(Y1>Y2))
  310. {g.drawRect(X2,Y2,(X1-X2),(Y1-Y2));}
  311. else if ((X1>X2)&&(Y1<Y2))
  312. {g.drawRect(X2,Y1,(X1-X2),(Y2-Y1));}
  313. else if ((X1<X2)&&(Y1>Y2))
  314. {g.drawRect(X1,Y2,(X2-X1),(Y1-Y2));}
  315. //g.fillRect(X1, Y1, X2, Y2);
  316.  
  317. break;
  318. }
  319. case 3: {g.drawOval(X1, Y1, X2, Y2);break;}
  320.  
  321. case 4:
  322. {
  323. if ((X1<X2)&&(Y1<Y2))
  324. {g.drawRect(X1, Y1, (X2-X1),(Y2-Y1));}
  325. else if ((X1>X2)&&(Y1>Y2))
  326. {g.drawRect(X2,Y2,(X1-X2),(Y1-Y2));}
  327. else if ((X1>X2)&&(Y1<Y2))
  328. {g.drawRect(X2,Y1,(X1-X2),(Y2-Y1));}
  329. else if ((X1<X2)&&(Y1>Y2))
  330. {g.drawRect(X1,Y2,(X2-X1),(Y1-Y2));}
  331. }
  332.  
  333. }
  334. }
  335.  
  336. public static void main(String args[]) {
  337. /* Set the Nimbus look and feel */
  338. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  339. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  340. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  341. */
  342. try {
  343. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  344. if ("Nimbus".equals(info.getName())) {
  345. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  346. break;
  347. }
  348. }
  349. } catch (ClassNotFoundException ex) {
  350. java.util.logging.Logger.getLogger(PintaFiguras.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  351. } catch (InstantiationException ex) {
  352. java.util.logging.Logger.getLogger(PintaFiguras.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  353. } catch (IllegalAccessException ex) {
  354. java.util.logging.Logger.getLogger(PintaFiguras.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  355. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  356. java.util.logging.Logger.getLogger(PintaFiguras.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  357. }
  358. //</editor-fold>
  359.  
  360. /* Create and display the form */
  361. java.awt.EventQueue.invokeLater(new Runnable() {
  362.  
  363. public void run() {
  364. new PintaFiguras().setVisible(true);
  365. }
  366. });
  367. }
  368. // Variables declaration - do not modify
  369. private javax.swing.JMenuItem About;
  370. private javax.swing.JMenuItem Abrir;
  371. private javax.swing.JMenu Archivo;
  372. private javax.swing.JMenu Ayuda;
  373. private javax.swing.JMenuItem Colores;
  374. private javax.swing.JMenu Figuras;
  375. private javax.swing.JMenu Formato;
  376. private javax.swing.JMenuItem Guardar;
  377. private javax.swing.JMenuItem Linea;
  378. private javax.swing.JMenuItem Ovalo;
  379. private javax.swing.JButton Pintar;
  380. private javax.swing.JMenuItem Rectangulo;
  381. private javax.swing.JCheckBoxMenuItem Relleno;
  382. private javax.swing.JMenuItem Salir;
  383. private javax.swing.JFrame about;
  384. private javax.swing.JFrame color;
  385. private javax.swing.JFileChooser dialogo;
  386. private javax.swing.JLabel jLabel1;
  387. private javax.swing.JLabel jLabel2;
  388. private javax.swing.JLabel jLabel3;
  389. private javax.swing.JLabel jLabel4;
  390. private javax.swing.JMenuBar jMenuBar1;
  391. private javax.swing.JColorChooser kolores;
  392. // End of variables declaration
  393. }
Advertisement
Add Comment
Please, Sign In to add comment