Xtrminator

PaintFiguras

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