Advertisement
josemf

Untitled

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