Advertisement
CaptainSpaceCat

ElectricRunner - RunUI

Apr 19th, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.49 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.event.*;
  5. import java.io.*;
  6. import javax.imageio.*;
  7. import java.awt.image.*;
  8. import java.net.URL;
  9.  
  10. public class RunUI extends JFrame implements ActionListener, MouseListener, KeyListener, ChangeListener {
  11.  
  12.   private JPanel panel;
  13.   private BufferedImage[] tiles;
  14.   private BufferedImage emptyTile, flaggedTile;
  15.  
  16.   private boolean controlsEnabled = false;
  17.  
  18.   int offsetX = 60;
  19.   int offsetY = 100;
  20.   int big = 120;
  21.  
  22.   private JButton genButton, shuffleButton, nextButton;
  23.   private JLabel[][] images;
  24.  
  25.   private JTextArea scoreCard;
  26.   private int score = 0;
  27.  
  28.   private Board board;
  29.  
  30.   public RunUI() {
  31.     super("Electric Runner");
  32.     tiles = new BufferedImage[9];
  33.    
  34.     for (int i = 0; i < 8; i++) {
  35.       URL path = RunUI.class.getResource(i + ".png");
  36.       File f = new File(path.getFile());
  37.       tiles[i] = loadImage(f);
  38.     }
  39.    
  40.     URL path = RunUI.class.getResource("empty.png");
  41.     File f = new File(path.getFile());
  42.     emptyTile = loadImage(f);
  43.     path = RunUI.class.getResource("flagged.png");
  44.     f = new File(path.getFile());
  45.     flaggedTile = loadImage(f);
  46.    
  47.     setupBoard(5, 5);
  48.     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  49.     repaint();
  50.   }
  51.  
  52.   public void setBoard(Board b) {
  53.     board = b;
  54.     repaint(board);
  55.   }
  56.  
  57.   public BufferedImage loadImage(File f) {
  58.     BufferedImage i = null;
  59.     boolean success = true;
  60.     //System.out.println(f.getPath());
  61.     try {
  62.       i = ImageIO.read(f);
  63.     } catch (IOException e) {
  64.       //System.err.println("Load failed.");
  65.       success = false;
  66.     }
  67.     if (success) {
  68.       //System.out.println("Load successful.");
  69.     }
  70.     return i;
  71.   }
  72.  
  73.   public void repaint(Board b) {
  74.     int w = b.getDimensions()[0];
  75.     int h = b.getDimensions()[1];
  76.     //setupBoard(w, h);
  77.    
  78.     for (int x = 0; x < w; x++) {
  79.       for (int y = 0; y < h; y++) {
  80.         if (b.getTile(x, y).isPresent()) {
  81.           if (b.getTile(x, y).isBolted() && b.getTile(x, y).getType() < 3) {
  82.             BufferedImage i = copyImage(tiles[b.getTile(x, y).getType()+5], b.getTile(x, y).getOrientation());
  83.             images[x][y].setIcon(new ImageIcon(i));
  84.           } else {
  85.             //System.out.println("Placing tile at: " + x + ":" + y);
  86.             BufferedImage i = copyImage(tiles[b.getTile(x, y).getType()], b.getTile(x, y).getOrientation());
  87.             images[x][y].setIcon(new ImageIcon(i));
  88.           }
  89.         } else {
  90.           if (b.getTile(x, y).isFlagged()) {
  91.             BufferedImage i = copyImage(flaggedTile, 0);
  92.             images[x][y].setIcon(new ImageIcon(i));
  93.           } else {
  94.             BufferedImage i = copyImage(emptyTile, 0);
  95.             images[x][y].setIcon(new ImageIcon(i));
  96.           }
  97.         }
  98.       }
  99.     }
  100.    
  101.     pack();
  102.     panel.requestFocus();
  103.     panel.repaint();
  104.   }
  105.  
  106.   public void repaint() {
  107.     pack();
  108.     panel.requestFocus();
  109.     panel.repaint();
  110.   }
  111.  
  112.   private BufferedImage copyImage(BufferedImage i, int rotation) {
  113.     int h = i.getHeight();
  114.     int w = i.getWidth();
  115.     BufferedImage o = new BufferedImage(i.getWidth(), i.getHeight(), i.getType());
  116.     if (rotation == 0) {
  117.       for (int y = 0; y < h; y++) {
  118.         for (int x = 0; x < w; x++) {
  119.           o.setRGB(x, y, i.getRGB(x, y));
  120.         }
  121.       }
  122.     } else if (rotation == 3) {
  123.       for (int y = 0; y < h; y++) {
  124.         for (int x = 0; x < w; x++) {
  125.           o.setRGB(x, y, i.getRGB(w-y-1, x));
  126.         }
  127.       }
  128.     } else if (rotation == 2) {
  129.       for (int y = 0; y < h; y++) {
  130.         for (int x = 0; x < w; x++) {
  131.           o.setRGB(x, y, i.getRGB(w-x-1, h-y-1));
  132.         }
  133.       }
  134.     } else if (rotation == 1) {
  135.       for (int y = 0; y < h; y++) {
  136.         for (int x = 0; x < w; x++) {
  137.           o.setRGB(x, y, i.getRGB(y, h-x-1));
  138.         }
  139.       }
  140.     }
  141.     return o;
  142.   }
  143.  
  144.   public void actionPerformed(ActionEvent e) {
  145.     if (e.getSource() == genButton) {
  146.       Board b = new Board(5, 5, 0);
  147.       setBoard(b);
  148.       //System.out.println("New board set.");
  149.       controlsEnabled = true;
  150.       board.shuffle(300);
  151.       repaint(board);
  152.       shuffleButton.setEnabled(true);
  153.     } else if (e.getSource() == shuffleButton) {
  154.       board.shuffle(100);
  155.       repaint(board);
  156.     } else if (e.getSource() == nextButton) {
  157.       nextButton.setEnabled(false);
  158.       Board b = new Board(5, 5, 0);
  159.       setBoard(b);
  160.       controlsEnabled = true;
  161.       board.shuffle(300);
  162.       repaint(board);
  163.     }
  164.   }
  165.  
  166.   public void stateChanged(ChangeEvent e) {
  167.    
  168.   }
  169.  
  170.   public void mousePressed(MouseEvent e) {
  171.   }
  172.  
  173.   public void mouseReleased(MouseEvent e) {
  174.   }
  175.  
  176.   public void mouseEntered(MouseEvent e) {
  177.    
  178.     //JLabel source = (JLabel)e.getSource();
  179.     //source.setOpaque(true);
  180.     //System.out.println("entered");
  181.     //repaint();
  182.   }
  183.  
  184.   public void mouseExited(MouseEvent e) {
  185.     //JLabel source = (JLabel)e.getSource();
  186.     //System.out.println("exited");
  187.     //source.setOpaque(false);
  188.     //repaint();
  189.   }
  190.  
  191.   boolean clickState = false;
  192.   int storedX;
  193.   int storedY;
  194.  
  195.   public void mouseClicked(MouseEvent e) {
  196.     if (e.getXOnScreen() > offsetX && e.getXOnScreen() < offsetX+big*5 && e.getYOnScreen() > offsetY && e.getYOnScreen() < offsetY+big*5 && controlsEnabled) {
  197.       int x = (e.getXOnScreen()-offsetX)/big;
  198.       int y = (e.getYOnScreen()-offsetY)/big;
  199.      
  200.       //System.out.println(e.getXOnScreen() + ":" + e.getYOnScreen());
  201.      
  202.       if (board.getTile(y, x).getType() < 3 && !board.getTile(y, x).isBolted()) {
  203.         /*System.out.println("Clicked");
  204.         System.out.println(y + ":" + x);
  205.         System.out.println("Type: " + board.getTile(y, x).getType());*/
  206.         if (clickState) {
  207.           if (board.getTile(y, x).isFlagged()) {
  208.             board.getTile(y, x).copyProperties(board.getTile(storedY, storedX));
  209.             board.getTile(storedY, storedX).setPresent(false);
  210.           }
  211.           board.clearFlags();
  212.           if (board.checkWin()) {
  213.             controlsEnabled = false;
  214.             score += 100 + board.getExcessTiles()*25;
  215.             //System.out.println(board.getExcessTiles()*25);
  216.             updateScore();
  217.             nextButton.setEnabled(true);
  218.           } else {
  219.             nextButton.setEnabled(false);
  220.           }
  221.           clickState = false;
  222.         } else {
  223.           if (board.getTile(y, x).isPresent()) {
  224.             storedX = x;
  225.             storedY = y;
  226.             clickState = true;
  227.             int fringe = board.floodFill(x, y);
  228.             if (fringe == 0) {
  229.               clickState = false;
  230.               board.clearFlags();
  231.             }
  232.           }
  233.         }
  234.       } else {
  235.         board.clearFlags();
  236.         clickState = false;
  237.       }
  238.     }
  239.     repaint(board);
  240.   }
  241.  
  242.   public void updateScore() {
  243.    
  244.     //scoreCard.replaceRange("" + score, 7, 6 + Integer.toString(score).length());
  245.     panel.remove(scoreCard);
  246.     scoreCard = new JTextArea("Score: " + score);
  247.     panel.add(scoreCard);
  248.     scoreCard.setBounds(540, 10, 100, 30);
  249.     repaint();
  250.   }
  251.  
  252.   public void keyTyped(KeyEvent e) {
  253.     //System.out.println("Key pressed!");
  254.    
  255.   }
  256.  
  257.   public void keyPressed(KeyEvent e) {
  258.   }
  259.  
  260.   public void keyReleased(KeyEvent e) {
  261.   }
  262.  
  263.  
  264.   public void setupBoard(int w, int h) {
  265.    
  266.    
  267.     panel = new JPanel() {
  268.       public void paintComponent(Graphics g) {
  269.         super.paintComponent(g);
  270.         Graphics2D g2 = (Graphics2D) g;
  271.         g2.setStroke(new BasicStroke(4));
  272.         for (int x = 0; x < w; x++) {
  273.           for (int y = 0; y < h; y++) {
  274.             g2.drawRect(offsetX-2+big*x, offsetY-2+big*y, big, big);
  275.           }
  276.         }
  277.        
  278.       }
  279.     };
  280.    
  281.     panel.addKeyListener(this);
  282.     panel.addMouseListener(this);
  283.    
  284.     URL path = RunUI.class.getResource("empty.png");
  285.     File f = new File(path.getFile());
  286.     BufferedImage i = loadImage(f);
  287.    
  288.     images = new JLabel[w][h];
  289.     for (int x = 0; x < w; x++) {
  290.       for (int y = 0; y < h; y++) {
  291.         images[x][y] = new JLabel(new ImageIcon(i));
  292.         panel.add(images[x][y]);
  293.         images[x][y].setBounds(offsetX-2+big*y, offsetY-2+big*x, big, big);
  294.       }
  295.     }
  296.    
  297.     //define buttons and sliders and stuff//
  298.    
  299.     scoreCard = new JTextArea("Score: 0");
  300.     panel.add(scoreCard);
  301.     scoreCard.setBounds(560, 10, 100, 30);
  302.    
  303.     genButton = new JButton("Generate");
  304.     genButton.addActionListener(this);
  305.     panel.add(genButton);
  306.     genButton.setBounds(10, 10, 100, 30);
  307.    
  308.     shuffleButton = new JButton("Shuffle");
  309.     shuffleButton.addActionListener(this);
  310.     panel.add(shuffleButton);
  311.     shuffleButton.setBounds(110, 10, 100, 30);
  312.     shuffleButton.setEnabled(false);
  313.    
  314.     nextButton = new JButton("Next");
  315.     nextButton.addActionListener(this);
  316.     panel.add(nextButton);
  317.     nextButton.setBounds(210, 10, 100, 30);
  318.     nextButton.setEnabled(false);
  319.    
  320.     panel.addKeyListener(this);
  321.     Dimension dim = new Dimension((int)(120*((double)w+.5))+offsetX, (int)(120*((double)h+.5))+offsetY);
  322.     this.setSize(dim);
  323.     panel.setLayout(null);
  324.     panel.setPreferredSize(dim);
  325.    
  326.    
  327.     pack();
  328.     getContentPane().add(panel);
  329.     //getRootPane().setDefaultButton(startButton);
  330.     panel.setVisible(true);
  331.    
  332.   }
  333.  
  334.   public void init() {
  335.     SwingUtilities.invokeLater(new Runnable() {
  336.       public void run() {
  337.         setVisible(true);
  338.       }
  339.     });
  340.   }
  341.  
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement