Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.53 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package controlador;
  7.  
  8. import Auxiliares.Canon;
  9. import Auxiliares.Casilla;
  10. import Auxiliares.ContenedorFinal;
  11. import Vistas.*;
  12. import Vistas.VistaCanon;
  13. import java.awt.Color;
  14. import java.awt.Dimension;
  15. import java.awt.FlowLayout;
  16. import java.awt.*;
  17. import java.awt.Graphics;
  18. import java.awt.Graphics2D;
  19. import java.awt.Image;
  20. import java.awt.Rectangle;
  21. import java.awt.event.ActionEvent;
  22. import java.awt.event.ActionListener;
  23. import java.awt.event.KeyAdapter;
  24. import java.awt.event.KeyEvent;
  25. import java.awt.geom.AffineTransform;
  26. import java.awt.geom.Line2D;
  27. import java.awt.image.AffineTransformOp;
  28. import java.awt.image.BufferedImage;
  29. import java.io.IOException;
  30. import java.text.DecimalFormat;
  31. import java.util.ArrayList;
  32. import java.util.logging.Level;
  33. import java.util.logging.Logger;
  34. import javax.imageio.ImageIO;
  35. import javax.swing.*;
  36. import javax.swing.plaf.basic.BasicProgressBarUI;
  37.  
  38. /**
  39. *
  40. * @author ignacioaranguren
  41. */
  42. public class Controlador extends JFrame {
  43.  
  44. private JPanel contenedorFinal;
  45. private JProgressBar barra;
  46. private ActionListener ac;
  47. private Timer t;
  48. private int puntuacion = 0;
  49. private DecimalFormat timeFormatter;
  50. private JLabel etiquetaFin;
  51. private JButton finalizar;
  52. private Graphics2D offGraphics;
  53. private Image offImage;
  54. private Modelo modelo;
  55. private VistaCanon vistaCanon;
  56. private VistaBala vistaBala;
  57. private VistaCasillas vistaCasillas;
  58. private VistaCorazon vistaCorazon;
  59. private VistaHaCaidoAgua vistaHaCaidoAgua;
  60. private VistaSegundero vistaSegundero;
  61. private VistaPuntuacion vistaPuntuacion;
  62.  
  63. public Controlador() {
  64.  
  65. setLayout(null);
  66. modelo = new Modelo();
  67. vistaCanon = new VistaCanon(modelo);
  68.  
  69. modelo.addObserver(vistaCanon);
  70.  
  71. modelo.initComponents();
  72. timeFormatter = new DecimalFormat("00");
  73. etiquetaFin = new JLabel("Juego finalizado");
  74. etiquetaFin.setFont(new Font("Arial", Font.PLAIN, 35));
  75. etiquetaFin.setBounds(250, 330, 300, 100);
  76. etiquetaFin.setVisible(false);
  77. finalizar = new JButton("Menu principal");
  78. finalizar.setBounds(300, 410, 150, 30);
  79. finalizar.addActionListener(new ActionListener() {
  80. @Override
  81. public void actionPerformed(ActionEvent e) {
  82. System.exit(0);
  83. }
  84. });
  85. finalizar.setVisible(false);
  86. contenedorFinal = new ContenedorFinal();
  87. contenedorFinal.setBackground(Color.red);
  88. contenedorFinal.setVisible(false);
  89. contenedorFinal.setBounds(0, 0, 940, 900);
  90.  
  91. barra = new JProgressBar(JProgressBar.HORIZONTAL, 0, 100);
  92. barra.setBounds(450, 730, 200, 40);
  93. barra.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2, true));
  94. barra.setUI(new BasicProgressBarUI() {
  95. @Override
  96. protected void paintDeterminate(Graphics g, JComponent c) {
  97. Graphics2D g2d = (Graphics2D) g;
  98. int ancho = barra.getWidth();
  99. int alto = barra.getHeight();
  100.  
  101. int espacioAncho = ancho;
  102. int espacioAlto = alto;
  103. barra.setPreferredSize(new Dimension(200, 40));
  104. double porcentajeProgres = barra.getPercentComplete();
  105.  
  106. espacioAncho = (int) (espacioAncho * porcentajeProgres);
  107. if (porcentajeProgres <= 0.25) {
  108. g2d.setColor(Color.GREEN);
  109. } else if (porcentajeProgres > 0.25 && porcentajeProgres <= 0.5) {
  110. g2d.setColor(Color.yellow);
  111. } else if (porcentajeProgres > 0.5 && porcentajeProgres <= 0.75) {
  112. g2d.setColor(Color.orange);
  113. } else {
  114. g2d.setColor(Color.red);
  115. }
  116. Rectangle rec1 = new Rectangle(0, 0, espacioAncho, espacioAlto);
  117. g2d.fill(rec1);
  118. }
  119.  
  120. });
  121. ac = new ActionListener() {
  122. @Override
  123. public void actionPerformed(ActionEvent e) {
  124. modelo.BarraProgreso();
  125. }
  126.  
  127. };
  128.  
  129. modelo.setBarra(barra);
  130. modelo.setBotonFinalizar(finalizar);
  131. modelo.setContenedorFinal(contenedorFinal);
  132. modelo.setEtiquetaFin(etiquetaFin);
  133. modelo.setTimerFormater(timeFormatter);
  134.  
  135. t = new Timer(35, ac);
  136. t.start();
  137.  
  138. addKeyListener(new KeyAdapter() {
  139. @Override
  140. public void keyPressed(KeyEvent evento) {
  141. switch (evento.getKeyCode()) {
  142. case KeyEvent.VK_LEFT:
  143. modelo.anguloRotacion(1);
  144. break;
  145. case KeyEvent.VK_RIGHT:
  146. modelo.anguloRotacion(2);
  147. break;
  148. case KeyEvent.VK_ENTER:
  149. modelo.anguloRotacion(3);
  150. break;
  151. case KeyEvent.VK_ESCAPE:
  152. repaint();
  153. modelo.salirJuego(JOptionPane.showConfirmDialog(null, "¿Volver al menú principal?", "Juego", JOptionPane.YES_NO_OPTION));
  154. break;
  155. case KeyEvent.VK_H:
  156. JOptionPane.showMessageDialog(null, "-Pulsa las teclas izquierda y derecha para mover el cañón.n-Selecciona una "
  157. + "intensidad mediante la barra de fuerza:n -Rojo = Disparar fuerte.n -Amarillo = disparar medio fuerte"
  158. + ". n -Verde = disparar suave.n-Pulsa Enter para disparar. n-Durante el disparo puedes modificar su trayectoria mediante la teclas izquierda y derecha."
  159. + "n-La puntuación: n -Los cangrejos (+1). n -Los tesoros (+2). n -Las botellas de ron (-1) vidas.", "Cómo jugar", JOptionPane.QUESTION_MESSAGE);
  160. break;
  161. }
  162. requestFocus();
  163. }
  164. });
  165.  
  166. addWindowListener(new java.awt.event.WindowAdapter() {
  167. @Override
  168. public void windowClosing(java.awt.event.WindowEvent evt) {
  169. close();
  170. }
  171. });
  172.  
  173. setSize(940, 900);
  174. setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
  175. add(finalizar);
  176. add(etiquetaFin);
  177. setResizable(false);
  178. add(contenedorFinal);
  179. add(barra);
  180. setVisible(true);
  181.  
  182. }
  183.  
  184. @Override
  185. public void paint(Graphics g) {
  186. Graphics2D g2d = (Graphics2D) g;
  187. Image fondo = new ImageIcon(getClass().getResource("/imagenes/fondo.jpeg")).getImage();
  188. g2d.drawImage(fondo, 0, 0, null);
  189. g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
  190. RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
  191. g2d.setFont(new Font("Marker Felt", Font.PLAIN, 30));
  192. g2d.setColor(Color.white);
  193. g2d.drawString("Cómo jugar, Pulsa H", 710, 330);
  194. g2d.drawString("Pulsa ESC para salir", 710, 380);
  195. Image img2 = new ImageIcon(getClass().getResource("/imagenes/fondoMapa.png")).getImage();
  196. g2d.drawImage(img2, 30, 30, this);
  197. }
  198.  
  199. private void close() {
  200. if (JOptionPane.showConfirmDialog(rootPane, "¿Volver al menú principal?",
  201. "Juego", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
  202. System.exit(0);
  203. }
  204. }
  205.  
  206. }
  207.  
  208. package controlador;
  209.  
  210. import Auxiliares.Canon;
  211. import Auxiliares.Casilla;
  212. import java.awt.Color;
  213. import java.awt.Font;
  214. import java.awt.Graphics2D;
  215. import java.awt.Image;
  216. import java.awt.RenderingHints;
  217. import java.awt.event.ActionEvent;
  218. import java.awt.event.ActionListener;
  219. import java.awt.image.BufferedImage;
  220. import java.io.IOException;
  221. import java.text.DecimalFormat;
  222. import java.util.ArrayList;
  223. import java.util.Observable;
  224. import java.util.logging.Level;
  225. import java.util.logging.Logger;
  226. import javax.imageio.ImageIO;
  227. import javax.swing.ImageIcon;
  228. import javax.swing.JButton;
  229. import javax.swing.JLabel;
  230. import javax.swing.JPanel;
  231. import javax.swing.JProgressBar;
  232. import javax.swing.Timer;
  233.  
  234. /**
  235. *
  236. * @author ignacio
  237. */
  238. public class Modelo extends Observable {
  239.  
  240. private double anguloRotacion, anguloMin, anguloMax;
  241. private boolean teclaPresionada, flag, flagBala, pintarCasilla, balaAgua;
  242. private JProgressBar barra;
  243. private double vX, vY, x1, x2, x1Anterior, x2Anterior, alfa, escala, i;
  244. private Timer animacion, timer;
  245. private Canon canon;
  246. private int progreso, x, y, puntuacion, vidas, posVidaX, contadorCasilla;
  247. private Casilla[][] casilla;
  248. private double posX[], posY[];
  249. private BufferedImage imgB;
  250. private Image img2, fondo, fondoMapa, vida, img;
  251. private ArrayList<Casilla> arrayCasilla;
  252. private JLabel etiquetaFin;
  253. private JButton finalizar;
  254. private JPanel contenedorFinal;
  255. private byte seconds;
  256. private short minutes;
  257. private DecimalFormat timeformater;
  258.  
  259.  
  260. public void setBarra(JProgressBar b){
  261. this.barra = b;
  262. }
  263.  
  264. public void setEtiquetaFin(JLabel f){
  265. this.etiquetaFin = f;
  266. }
  267.  
  268. public void setTimerFormater(DecimalFormat d){
  269. timeformater = d;
  270. }
  271.  
  272. public void setContenedorFinal(JPanel p){
  273. contenedorFinal = p;
  274. }
  275.  
  276. public void setBotonFinalizar(JButton b){
  277. this.finalizar = b;
  278. }
  279.  
  280.  
  281. public void initComponents(){
  282. seconds = 00;
  283. minutes = 03;
  284.  
  285. timer = new Timer(1000, new ActionListener() {
  286. @Override
  287. public void actionPerformed(ActionEvent e) {
  288.  
  289. if (seconds == 0 && minutes == 0) {
  290. timer.stop();
  291. etiquetaFin.setVisible(true);
  292. finalizar.setVisible(true);
  293. contenedorFinal.setVisible(true);
  294.  
  295. } else if (seconds > 0) {
  296. seconds--;
  297. setChanged();
  298. notifyObservers();
  299.  
  300. } else if (minutes > 0) {
  301. minutes--;
  302. seconds = 59;
  303. setChanged();
  304. notifyObservers();
  305. }
  306.  
  307. }
  308. });
  309. timer.start();
  310.  
  311. posX = new double[]{30, 79, 128.0, 177.0, 226.0, 275.0, 324.0, 373.0, 422.0, 471.0, 520.0, 569.0, 618};
  312. posY = new double[]{30, 83.0, 136.0, 189.0, 242.0, 295.0, 348.0, 401.0, 454.0, 507.0, 560.0, 613.0, 613};
  313. casilla = new Casilla[13][13];
  314. inicializarCasillas();
  315. anguloRotacion = Math.PI / 2;
  316. vidas = 3;
  317. anguloMax = 0.81986726449;
  318. anguloMin = anguloMax = 2.32172538679;
  319. teclaPresionada = false;
  320. flag = false;
  321. flagBala = false;
  322. pintarCasilla = false;
  323. balaAgua = false;
  324. i = 0;
  325. progreso = 0;
  326. this.canon = canon = new Canon();
  327. this.barra = barra;
  328. arrayCasilla = new ArrayList<>();
  329. llenarCasillas();
  330. }
  331.  
  332. public void cargarImagenes() {
  333. try {
  334. img2 = new ImageIcon(getClass().getResource("/imagenes/fondoMapa.png")).getImage();
  335. fondo = new ImageIcon(getClass().getResource("/imagenes/fondo.jpeg")).getImage();
  336. vida = new ImageIcon(getClass().getResource("/imagenes/vida.png")).getImage();
  337. img = new ImageIcon(getClass().getResource("/imagenes/BalaCanon.png")).getImage();
  338. imgB = ImageIO.read(getClass().getResource("/imagenes/canon.png"));
  339. } catch (IOException ex) {
  340. Logger.getLogger(Modelo.class.getName()).log(Level.SEVERE, null, ex);
  341. }
  342.  
  343. }
  344.  
  345. public void anguloRotacion(int direccion) {
  346. switch (direccion) {
  347. case 1:
  348. if (anguloRotacion > anguloMin && anguloRotacion < anguloMax) {
  349. anguloRotacion = anguloRotacion + 0.05;
  350. } else {
  351. anguloRotacion = anguloRotacion - 0.05;
  352. }
  353. setChanged();
  354. notifyObservers();
  355. break;
  356. case 2:
  357. if (anguloRotacion > anguloMin && anguloRotacion < anguloMax) {
  358. anguloRotacion = anguloRotacion - 0.05;
  359. } else {
  360. anguloRotacion = anguloRotacion + 0.05;
  361. }
  362. setChanged();
  363. notifyObservers();
  364. break;
  365.  
  366. default:
  367. break;
  368. }
  369.  
  370. }
  371. }
  372.  
  373. package Vistas;
  374.  
  375. import controlador.Modelo;
  376. import java.awt.Canvas;
  377. import java.awt.Color;
  378. import java.awt.Graphics;
  379. import java.awt.Graphics2D;
  380. import java.awt.geom.AffineTransform;
  381. import java.awt.image.AffineTransformOp;
  382. import java.util.Observable;
  383. import java.util.Observer;
  384.  
  385.  
  386. public class VistaCanon extends Canvas implements Observer {
  387.  
  388. private Modelo m;
  389.  
  390. public VistaCanon(Modelo m) {
  391. this.m = m;
  392. }
  393.  
  394. @Override
  395. public void paint(Graphics g) {
  396. super.paint(g);
  397. Graphics2D g2d = (Graphics2D) g;
  398. g2d.setColor(Color.gray);
  399. g2d.fillRect(30, 720, 646, 122);
  400. AffineTransform tx = AffineTransform.getRotateInstance(Math.PI / 2 - m.getAnguloRotacion(), m.getImB().getWidth(this) / 2, m.getImB().getHeight(this) / 2);
  401. AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
  402. tx.rotate(Math.PI / 2 - m.getAnguloRotacion());
  403. System.out.println(tx.toString());
  404.  
  405. g2d.drawImage(op.filter(m.getImB(), null), 360, 740, null);
  406. }
  407.  
  408. @Override
  409. public void update(Observable o, Object arg) {
  410. repaint();
  411. }
  412.  
  413. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement