Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.event.*;
- import java.io.*;
- import javax.imageio.*;
- import java.awt.image.*;
- import java.net.URL;
- public class RunUI extends JFrame implements ActionListener, MouseListener, KeyListener, ChangeListener {
- private JPanel panel;
- private BufferedImage[] tiles;
- private BufferedImage emptyTile, flaggedTile;
- private boolean controlsEnabled = false;
- int offsetX = 60;
- int offsetY = 100;
- int big = 120;
- private JButton genButton, shuffleButton, nextButton;
- private JLabel[][] images;
- private JTextArea scoreCard;
- private int score = 0;
- private Board board;
- public RunUI() {
- super("Electric Runner");
- tiles = new BufferedImage[9];
- for (int i = 0; i < 8; i++) {
- URL path = RunUI.class.getResource(i + ".png");
- File f = new File(path.getFile());
- tiles[i] = loadImage(f);
- }
- URL path = RunUI.class.getResource("empty.png");
- File f = new File(path.getFile());
- emptyTile = loadImage(f);
- path = RunUI.class.getResource("flagged.png");
- f = new File(path.getFile());
- flaggedTile = loadImage(f);
- setupBoard(5, 5);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- repaint();
- }
- public void setBoard(Board b) {
- board = b;
- repaint(board);
- }
- public BufferedImage loadImage(File f) {
- BufferedImage i = null;
- boolean success = true;
- //System.out.println(f.getPath());
- try {
- i = ImageIO.read(f);
- } catch (IOException e) {
- //System.err.println("Load failed.");
- success = false;
- }
- if (success) {
- //System.out.println("Load successful.");
- }
- return i;
- }
- public void repaint(Board b) {
- int w = b.getDimensions()[0];
- int h = b.getDimensions()[1];
- //setupBoard(w, h);
- for (int x = 0; x < w; x++) {
- for (int y = 0; y < h; y++) {
- if (b.getTile(x, y).isPresent()) {
- if (b.getTile(x, y).isBolted() && b.getTile(x, y).getType() < 3) {
- BufferedImage i = copyImage(tiles[b.getTile(x, y).getType()+5], b.getTile(x, y).getOrientation());
- images[x][y].setIcon(new ImageIcon(i));
- } else {
- //System.out.println("Placing tile at: " + x + ":" + y);
- BufferedImage i = copyImage(tiles[b.getTile(x, y).getType()], b.getTile(x, y).getOrientation());
- images[x][y].setIcon(new ImageIcon(i));
- }
- } else {
- if (b.getTile(x, y).isFlagged()) {
- BufferedImage i = copyImage(flaggedTile, 0);
- images[x][y].setIcon(new ImageIcon(i));
- } else {
- BufferedImage i = copyImage(emptyTile, 0);
- images[x][y].setIcon(new ImageIcon(i));
- }
- }
- }
- }
- pack();
- panel.requestFocus();
- panel.repaint();
- }
- public void repaint() {
- pack();
- panel.requestFocus();
- panel.repaint();
- }
- private BufferedImage copyImage(BufferedImage i, int rotation) {
- int h = i.getHeight();
- int w = i.getWidth();
- BufferedImage o = new BufferedImage(i.getWidth(), i.getHeight(), i.getType());
- if (rotation == 0) {
- for (int y = 0; y < h; y++) {
- for (int x = 0; x < w; x++) {
- o.setRGB(x, y, i.getRGB(x, y));
- }
- }
- } else if (rotation == 3) {
- for (int y = 0; y < h; y++) {
- for (int x = 0; x < w; x++) {
- o.setRGB(x, y, i.getRGB(w-y-1, x));
- }
- }
- } else if (rotation == 2) {
- for (int y = 0; y < h; y++) {
- for (int x = 0; x < w; x++) {
- o.setRGB(x, y, i.getRGB(w-x-1, h-y-1));
- }
- }
- } else if (rotation == 1) {
- for (int y = 0; y < h; y++) {
- for (int x = 0; x < w; x++) {
- o.setRGB(x, y, i.getRGB(y, h-x-1));
- }
- }
- }
- return o;
- }
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == genButton) {
- Board b = new Board(5, 5, 0);
- setBoard(b);
- //System.out.println("New board set.");
- controlsEnabled = true;
- board.shuffle(300);
- repaint(board);
- shuffleButton.setEnabled(true);
- } else if (e.getSource() == shuffleButton) {
- board.shuffle(100);
- repaint(board);
- } else if (e.getSource() == nextButton) {
- nextButton.setEnabled(false);
- Board b = new Board(5, 5, 0);
- setBoard(b);
- controlsEnabled = true;
- board.shuffle(300);
- repaint(board);
- }
- }
- public void stateChanged(ChangeEvent e) {
- }
- public void mousePressed(MouseEvent e) {
- }
- public void mouseReleased(MouseEvent e) {
- }
- public void mouseEntered(MouseEvent e) {
- //JLabel source = (JLabel)e.getSource();
- //source.setOpaque(true);
- //System.out.println("entered");
- //repaint();
- }
- public void mouseExited(MouseEvent e) {
- //JLabel source = (JLabel)e.getSource();
- //System.out.println("exited");
- //source.setOpaque(false);
- //repaint();
- }
- boolean clickState = false;
- int storedX;
- int storedY;
- public void mouseClicked(MouseEvent e) {
- if (e.getXOnScreen() > offsetX && e.getXOnScreen() < offsetX+big*5 && e.getYOnScreen() > offsetY && e.getYOnScreen() < offsetY+big*5 && controlsEnabled) {
- int x = (e.getXOnScreen()-offsetX)/big;
- int y = (e.getYOnScreen()-offsetY)/big;
- //System.out.println(e.getXOnScreen() + ":" + e.getYOnScreen());
- if (board.getTile(y, x).getType() < 3 && !board.getTile(y, x).isBolted()) {
- /*System.out.println("Clicked");
- System.out.println(y + ":" + x);
- System.out.println("Type: " + board.getTile(y, x).getType());*/
- if (clickState) {
- if (board.getTile(y, x).isFlagged()) {
- board.getTile(y, x).copyProperties(board.getTile(storedY, storedX));
- board.getTile(storedY, storedX).setPresent(false);
- }
- board.clearFlags();
- if (board.checkWin()) {
- controlsEnabled = false;
- score += 100 + board.getExcessTiles()*25;
- //System.out.println(board.getExcessTiles()*25);
- updateScore();
- nextButton.setEnabled(true);
- } else {
- nextButton.setEnabled(false);
- }
- clickState = false;
- } else {
- if (board.getTile(y, x).isPresent()) {
- storedX = x;
- storedY = y;
- clickState = true;
- int fringe = board.floodFill(x, y);
- if (fringe == 0) {
- clickState = false;
- board.clearFlags();
- }
- }
- }
- } else {
- board.clearFlags();
- clickState = false;
- }
- }
- repaint(board);
- }
- public void updateScore() {
- //scoreCard.replaceRange("" + score, 7, 6 + Integer.toString(score).length());
- panel.remove(scoreCard);
- scoreCard = new JTextArea("Score: " + score);
- panel.add(scoreCard);
- scoreCard.setBounds(540, 10, 100, 30);
- repaint();
- }
- public void keyTyped(KeyEvent e) {
- //System.out.println("Key pressed!");
- }
- public void keyPressed(KeyEvent e) {
- }
- public void keyReleased(KeyEvent e) {
- }
- public void setupBoard(int w, int h) {
- panel = new JPanel() {
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- Graphics2D g2 = (Graphics2D) g;
- g2.setStroke(new BasicStroke(4));
- for (int x = 0; x < w; x++) {
- for (int y = 0; y < h; y++) {
- g2.drawRect(offsetX-2+big*x, offsetY-2+big*y, big, big);
- }
- }
- }
- };
- panel.addKeyListener(this);
- panel.addMouseListener(this);
- URL path = RunUI.class.getResource("empty.png");
- File f = new File(path.getFile());
- BufferedImage i = loadImage(f);
- images = new JLabel[w][h];
- for (int x = 0; x < w; x++) {
- for (int y = 0; y < h; y++) {
- images[x][y] = new JLabel(new ImageIcon(i));
- panel.add(images[x][y]);
- images[x][y].setBounds(offsetX-2+big*y, offsetY-2+big*x, big, big);
- }
- }
- //define buttons and sliders and stuff//
- scoreCard = new JTextArea("Score: 0");
- panel.add(scoreCard);
- scoreCard.setBounds(560, 10, 100, 30);
- genButton = new JButton("Generate");
- genButton.addActionListener(this);
- panel.add(genButton);
- genButton.setBounds(10, 10, 100, 30);
- shuffleButton = new JButton("Shuffle");
- shuffleButton.addActionListener(this);
- panel.add(shuffleButton);
- shuffleButton.setBounds(110, 10, 100, 30);
- shuffleButton.setEnabled(false);
- nextButton = new JButton("Next");
- nextButton.addActionListener(this);
- panel.add(nextButton);
- nextButton.setBounds(210, 10, 100, 30);
- nextButton.setEnabled(false);
- panel.addKeyListener(this);
- Dimension dim = new Dimension((int)(120*((double)w+.5))+offsetX, (int)(120*((double)h+.5))+offsetY);
- this.setSize(dim);
- panel.setLayout(null);
- panel.setPreferredSize(dim);
- pack();
- getContentPane().add(panel);
- //getRootPane().setDefaultButton(startButton);
- panel.setVisible(true);
- }
- public void init() {
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- setVisible(true);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement