Advertisement
josemf

Untitled

May 22nd, 2016
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.95 KB | None | 0 0
  1. package jsopaletras_v02;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.GridLayout;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.awt.event.MouseEvent;
  9. import java.awt.event.MouseListener;
  10. import javax.swing.BorderFactory;
  11. import javax.swing.JCheckBoxMenuItem;
  12. import javax.swing.JColorChooser;
  13. import javax.swing.JFrame;
  14. import javax.swing.JLabel;
  15. import javax.swing.JMenu;
  16. import javax.swing.JMenuBar;
  17. import javax.swing.JMenuItem;
  18. import javax.swing.JOptionPane;
  19. import javax.swing.JPanel;
  20. /**
  21. * @author Edmond Duke
  22. * email: edu1738@gmail.com
  23. */
  24. public class frmSopaLetras extends JFrame implements ActionListener,MouseListener{
  25. private JLabel botones[][];
  26. private JMenuItem JMINuevo,JMIAcercaDe,JMICrear,JMIRemove,JMIColorF,JMIColorL,JMIColorC;
  27. private JCheckBoxMenuItem cuadricula,JMIPinta;
  28. private Sopaletras2 sopa;
  29. private Color color,colorf,colorl;
  30. private JPanel pane;
  31.  
  32. public frmSopaLetras(Sopaletras2 xSopa){
  33. super("jSopaLetras");
  34. color = null;
  35. colorf = null;
  36. colorl = null;
  37. sopa = xSopa;
  38. IniciaComponentes();
  39. int ancho,alto;
  40. ancho = (int)(26.85 * sopa.getTotalColumnas());
  41. alto = (int) (25.1 * sopa.getTotalFilas());
  42. if(ancho < 250)
  43. ancho = 250;
  44. if(alto < 127)
  45. alto = 127;
  46. this.setSize(ancho,alto);
  47. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  48. }
  49. public void IniciaComponentes(){
  50. pane = new JPanel(new GridLayout(sopa.getTotalFilas(),sopa.getTotalColumnas()));
  51. JPanel panePrincipal = new JPanel(new BorderLayout());
  52. JMenuBar barraMenu = new JMenuBar();
  53. JMenu mnuArchivo = new JMenu("Archivo");
  54. JMICrear = new JMenuItem("Crear nuevo...");
  55. JMICrear.addActionListener(new ActionListener(){
  56. public void actionPerformed(ActionEvent e) {
  57. ShowCreaSopaLetra();
  58. }
  59. });
  60. JMenuItem JMISalir = new JMenuItem("Salir");
  61. JMISalir.addActionListener(new ActionListener(){
  62. public void actionPerformed(ActionEvent e) {
  63. dispose();
  64. }
  65. });
  66. mnuArchivo.add(JMICrear);
  67. mnuArchivo.add(JMISalir);
  68. barraMenu.add(mnuArchivo);
  69.  
  70. JMenu mnuVer = new JMenu("Ver");
  71. JMIPinta = new JCheckBoxMenuItem("Ver Palabras Ocultas");
  72. JMIPinta.addActionListener(new ActionListener(){
  73. public void actionPerformed(ActionEvent e) {
  74. if(JMIPinta.getState())
  75. VerPalabras(1);
  76. else
  77. VerPalabras(0);
  78. }
  79. });
  80. mnuVer.add(JMIPinta);
  81.  
  82. JMenu mnuEdicion = new JMenu("Edición");
  83. JMenu mnuColor = new JMenu("Color");
  84. JMIColorC = new JMenuItem("Cuadrícula");
  85. JMIColorC.addActionListener(this);
  86. JMIColorF = new JMenuItem("Fondo");
  87. JMIColorF.addActionListener(this);
  88. JMIColorL = new JMenuItem("Letra");
  89. JMIColorL.addActionListener(this);
  90. mnuColor.add(JMIColorC);
  91. mnuColor.add(JMIColorF);
  92. mnuColor.add(JMIColorL);
  93.  
  94. cuadricula = new JCheckBoxMenuItem("Cuadrícula");
  95. cuadricula.setSelected(false);
  96. cuadricula.addActionListener(new ActionListener(){
  97. public void actionPerformed(ActionEvent e) {
  98. if(cuadricula.getState())
  99. BotonesEnCuadricula(1);
  100. else
  101. BotonesEnCuadricula(0);
  102. }
  103. });
  104. JMIRemove = new JMenuItem("Eliminar palabra");
  105. JMIRemove.addActionListener(new ActionListener(){
  106. public void actionPerformed(ActionEvent e) {
  107. ShowRemovePalabra();
  108. }
  109. });
  110. JMINuevo = new JMenuItem("Nuevos Caracteres");
  111. JMINuevo.addActionListener(new ActionListener(){
  112. public void actionPerformed(ActionEvent e) {
  113. sopa.New();
  114. }
  115. });
  116. mnuEdicion.add(mnuColor);
  117. mnuEdicion.add(cuadricula);
  118. mnuEdicion.add(JMIRemove);
  119. mnuEdicion.add(JMINuevo);
  120.  
  121. JMenu mnuAyuda = new JMenu("Ayuda");
  122. JMIAcercaDe = new JMenuItem("Acerca de jSopaLetras");
  123. JMIAcercaDe.addActionListener(new ActionListener(){
  124. public void actionPerformed(ActionEvent e) {
  125. ShowAcercaDe();
  126. }
  127. });
  128. mnuAyuda.add(JMIAcercaDe);
  129.  
  130. barraMenu.add(mnuVer);
  131. barraMenu.add(mnuEdicion);
  132. barraMenu.add(mnuAyuda);
  133.  
  134. botones = sopa.getJLabel();
  135. int i,j;
  136. for(i = 0; i < sopa.getTotalFilas(); i++){
  137. for(j = 0; j < sopa.getTotalColumnas(); j++){
  138. botones[i][j].addMouseListener(this);
  139. pane.add(botones[i][j]);
  140. }
  141. }
  142. panePrincipal.add(barraMenu, BorderLayout.NORTH);
  143. panePrincipal.add(pane,BorderLayout.CENTER);
  144. this.add(panePrincipal);
  145. }
  146. private void VerPalabras(int decision){
  147. if(decision == 0) { //Falso
  148. int i,j;
  149. for(i = 0; i < sopa.getTotalFilas(); i++)
  150. for(j = 0; j < sopa.getTotalColumnas(); j++){
  151. botones[i][j].setBackground(colorf);
  152. }
  153. }else{ //Verdadero
  154. sopa.PintaAllPalabra(Color.YELLOW);
  155. }
  156. }
  157. private void ColorFondo(Color xcolor){
  158. int i,j;
  159. for(i = 0; i < sopa.getTotalFilas(); i++){
  160. for(j = 0; j < sopa.getTotalColumnas(); j++)
  161. botones[i][j].setBackground(xcolor);
  162. }
  163. }
  164. private void ColorCuadricula(Color xcolor){
  165. int i,j;
  166. for(i = 0; i < sopa.getTotalFilas(); i++)
  167. for(j = 0; j < sopa.getTotalColumnas(); j++){
  168. botones[i][j].setBorder(BorderFactory.createLineBorder(xcolor));
  169. }
  170. }
  171. private void ColorLetra(Color xcolor){
  172. int i,j;
  173. for(i = 0; i < sopa.getTotalFilas(); i++)
  174. for(j = 0; j < sopa.getTotalColumnas(); j++){
  175. botones[i][j].setForeground(xcolor);
  176. }
  177. }
  178. private void BotonesEnCuadricula(int decision){
  179. int i,j;
  180. if(decision == 1){
  181. for(i = 0; i < sopa.getTotalFilas(); i++){
  182. for(j = 0; j < sopa.getTotalColumnas(); j++)
  183. botones[i][j].setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0)));
  184. }
  185. }else{
  186. for(i = 0; i < sopa.getTotalFilas(); i++){
  187. for(j = 0; j < sopa.getTotalColumnas(); j++)
  188. botones[i][j].setBorder(null);
  189. }
  190. }
  191. }
  192. private void ShowCreaSopaLetra(){
  193. if(JOptionPane.showConfirmDialog(rootPane, "¿Desea crear una nueva sopa de letras?. Se borrará los cambios del actual", "jSopaLetras", 1)==0){
  194. frmCreaSopaLetras frm = new frmCreaSopaLetras();
  195. frm.setVisible(true);
  196. this.dispose();
  197. }
  198. }
  199. private void ShowAcercaDe(){
  200. frmAcercaDe frm = new frmAcercaDe();
  201. frm.setLocationRelativeTo(this);
  202. frm.setVisible(true);
  203. }
  204.  
  205. private void ShowRemovePalabra(){
  206. frmEliminaPalabra frm = new frmEliminaPalabra(sopa, colorf, JMIPinta.getState());
  207. frm.setLocationRelativeTo(this);
  208. frm.setVisible(true);
  209. }
  210. public void Action_JMICrear(ActionEvent e){
  211. ShowCreaSopaLetra();
  212. }
  213. public void actionPerformed(ActionEvent e) {
  214. Object obj = e.getSource();
  215. if(obj == JMIColorF){
  216. MostrarColor(1);
  217. }else if(obj == JMIColorL){
  218. MostrarColor(2);
  219. }else if(obj == JMIColorC){
  220. if(cuadricula.getState())
  221. MostrarColor(0);
  222. else
  223. JOptionPane.showMessageDialog(rootPane, "La opción cuadrícula no está activada", "jSopaLetras", 1);
  224. }
  225. }
  226. private void MostrarColor(int modo){
  227. Color aux = null;
  228. switch(modo){
  229. case 1: aux = colorf; break;
  230. case 2: aux = colorl; break;
  231. }
  232. aux = JColorChooser.showDialog(rootPane,"Seleccione un color", aux);
  233. if(aux != null){
  234. if(modo == 1){
  235. ColorFondo(aux);
  236. if(JMIPinta.getState())
  237. sopa.PintaAllPalabra(Color.YELLOW);
  238. pane.setBackground(aux);
  239. colorf = aux;
  240. }else if (modo == 2){
  241. ColorLetra(aux);
  242. colorl = aux;
  243. }else
  244. ColorCuadricula(aux);
  245. }
  246. }
  247. public void mouseClicked(MouseEvent e) {
  248. JLabel label = (JLabel)e.getSource();
  249. label.setBackground(Color.YELLOW);
  250. int posX, posY,pose;
  251. pose = label.getName().indexOf("e");
  252. posX = Integer.parseInt(label.getName().substring(0,pose));
  253. posY = Integer.parseInt(label.getName().substring(pose+1));
  254. //frmInsertaPalabra frm = new frmInsertaPalabra(sopa,posX,posY);
  255. frmInsertaPalabra frm = new frmInsertaPalabra(sopa,posX,posY,JMIPinta.getState());
  256. frm.setLocationRelativeTo(this); //Centrar en formulario
  257. frm.setVisible(true);
  258. }
  259.  
  260. public void mousePressed(MouseEvent e) {
  261. //throw new UnsupportedOperationException("Not supported yet.");
  262. }
  263.  
  264. public void mouseReleased(MouseEvent e) {
  265. //throw new UnsupportedOperationException("Not supported yet.");
  266. }
  267.  
  268. public void mouseEntered(MouseEvent e) {
  269. JLabel label = (JLabel)e.getSource();
  270. color = label.getBackground();
  271. label.setBackground(Color.YELLOW);
  272. //this.setTitle(this.getWidth() + " * " + this.getHeight());
  273. }
  274.  
  275. public void mouseExited(MouseEvent e) {
  276. JLabel label = (JLabel)e.getSource();
  277. label.setBackground(color);
  278. if(JMIPinta.getState()){
  279. int posX, posY,pose;
  280. pose = label.getName().indexOf("e");
  281. posX = Integer.parseInt(label.getName().substring(0,pose));
  282. posY = Integer.parseInt(label.getName().substring(pose+1));
  283. if(sopa.lockedPosition(posX,posY))
  284. label.setBackground(Color.YELLOW);
  285. }
  286. }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement