Puzzle
It's fun making games. Even this simple. Wish I had rights for this awesome background pic.
Main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | package mhgpuzzletest; import java.awt.BorderLayout; import java.awt.Cursor; import java.awt.Image; import java.awt.LayoutManager; import java.awt.Point; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JButton; import javax.swing.JFrame; /** * * @author djordje gavrilovic */ public class Main extends JFrame { static JButton reset; static Main frame; static Cursor cursor; public Main() throws IOException { // custom-cursor. not full color on Linux Toolkit tk = Toolkit.getDefaultToolkit(); Image img = ImageIO.read(getClass().getResource("/rocket_curs3.png")); Point point = new Point(0, 0); cursor = tk.createCustomCursor(img, point, "rocketcursor"); } static void setGUI() throws IOException { frame = new Main(); LayoutManager lay = new BorderLayout(); frame.setLayout(lay); frame.setCursor(cursor); frame.setTitle("Puzzle"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.getContentPane().add(new GamePane()); frame.getContentPane().add(reset, BorderLayout.PAGE_END); frame.setSize(1000, 600); frame.setResizable(false); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) throws IOException { reset = new JButton("Reset"); reset.addActionListener((ActionEvent e) -> { frame.dispose(); try { setGUI(); } catch (IOException ex) { System.out.println("ALARM! ALARM!: " + ex.getMessage()); } }); setGUI(); } } |
GamePane.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | package mhgpuzzletest; /** * * @author djordje gavrilovic */ import java.awt.BorderLayout; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.LayoutManager; import java.awt.Point; import java.util.List; import java.awt.Polygon; import java.awt.Rectangle; import java.util.ArrayList; import javax.swing.JPanel; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.IOException; import java.io.InputStream; import java.util.Collections; import java.util.Random; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JLabel; import sun.audio.AudioPlayer; import sun.audio.AudioStream; public class GamePane extends JPanel { //grid frame Polygon gridPol = new Polygon(new int[]{375,625,625,375}, new int[]{175,175,425,425},4); //grid private List<Rectangle> gridRect; //shapes Polygon shape1 = new Polygon(new int[]{200,300,300,250,250,200}, new int[]{300,300,450,450,350,350}, 6); Polygon shape2 = new Polygon(new int[]{700,800,800,750,750,700}, new int[]{100,100,250,250,150,150}, 6); Polygon shape3 = new Polygon(new int[]{850,900,900,800,800,850}, new int[]{250,250,450,450,400,400}, 6); Polygon square = new Polygon(new int[]{50, 150, 150, 50}, new int[]{250, 250, 350, 350}, 4); Polygon rect = new Polygon(new int[]{350, 550, 550, 350}, new int[]{100, 100, 150, 150}, 4); Polygon t = new Polygon(new int[]{50,100,100,150,150,100,100,50}, new int[]{50,50,100,100,150,150,200,200}, 8); private List<Polygon> shapes; //selected shape private Polygon hitBox; //colors and background private List<Color> shapeColors; Color ran_color; Image background; public GamePane() throws IOException { this.background = ImageIO.read(getClass().getResource("/back8.jpg")); gridRect = new ArrayList<>(); shapes = new ArrayList<>(); Collections.addAll(shapes,shape1,shape2,shape3,square,rect,t); shapeColors = new ArrayList<>(); Collections.addAll(shapeColors, Color.CYAN, Color.GRAY, Color.GREEN, Color.ORANGE, Color.WHITE); Random randomGenerator = new Random(); int index = randomGenerator.nextInt(shapeColors.size()); ran_color = shapeColors.get(index); MouseAdapter handler; handler = new MouseAdapter() { Point startDrag, endDrag; // start i end positions @Override public void mousePressed(MouseEvent e) { startDrag = new Point(e.getX(), e.getY()); endDrag = startDrag; //if click is inside a shape for (Polygon p : shapes) { if (p.contains(startDrag)) { hitBox = p; //moves selected shape on top while (shapes.indexOf(hitBox) != 0) { int i = shapes.indexOf(hitBox); Collections.swap(shapes, i, i - 1); } Collections.reverse(shapes); break; } } repaint(); } @Override public void mouseReleased(MouseEvent e) { endDrag = null; startDrag = null; hitBox = null; //first vicory combination if(shape1.getBounds().x == 475 && shape1.getBounds().y == 225 && shape2.getBounds().x == 425 && shape2.getBounds().y == 275 && shape3.getBounds().x == 525 && shape3.getBounds().y == 225 && square.getBounds().x == 375 && square.getBounds().y == 325 && rect.getBounds().x == 425 && rect.getBounds().y == 175 && t.getBounds().x == 375 && t.getBounds().y == 175){ InputStream inputStream = getClass().getResourceAsStream("/sound.wav"); AudioStream audioStream = null; try { audioStream = new AudioStream(inputStream); } catch (IOException ex) { Logger.getLogger(GamePane.class.getName()).log(Level.SEVERE, null, ex); } AudioPlayer.player.start(audioStream); JFrame win = new JFrame(); JLabel juhu = new JLabel("Congratulations YOU WON!"); juhu.setHorizontalAlignment(JLabel.CENTER); LayoutManager lay = new BorderLayout(); win.setLayout(lay); win.setTitle("VICTORY!"); win.setBackground(Color.DARK_GRAY); win.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); win.getContentPane().add(juhu, BorderLayout.CENTER); win.setSize(400, 200); win.setResizable(false); win.setLocationRelativeTo(null); win.setVisible(true); } //second victory combination if(shape2.getBounds().x == 475 && shape2.getBounds().y == 225 && shape1.getBounds().x == 425 && shape1.getBounds().y == 275 && shape3.getBounds().x == 525 && shape3.getBounds().y == 225 && square.getBounds().x == 375 && square.getBounds().y == 325 && rect.getBounds().x == 425 && rect.getBounds().y == 175 && t.getBounds().x == 375 && t.getBounds().y == 175){ InputStream inputStream = getClass().getResourceAsStream("/sound.wav"); AudioStream audioStream = null; try { audioStream = new AudioStream(inputStream); } catch (IOException ex) { Logger.getLogger(GamePane.class.getName()).log(Level.SEVERE, null, ex); } AudioPlayer.player.start(audioStream); JFrame win = new JFrame(); JLabel juhu = new JLabel("Congratulations YOU WON!"); juhu.setHorizontalAlignment(JLabel.CENTER); LayoutManager lay = new BorderLayout(); win.setLayout(lay); win.setTitle("VICTORY!"); win.setBackground(Color.DARK_GRAY); win.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); win.getContentPane().add(juhu, BorderLayout.CENTER); win.setSize(400, 200); win.setResizable(false); win.setLocationRelativeTo(null); win.setVisible(true); } } @Override public void mouseDragged(MouseEvent e) { endDrag = new Point(e.getX(), e.getY()); int xtrans; int ytrans; if (hitBox != null) { //click inside shape if(hitBox.getBounds().x <= 0){ //if left edge is touched startDrag = new Point(1, e.getY()); xtrans = 1 + startDrag.x; ytrans = endDrag.y - startDrag.y; hitBox.translate(xtrans, ytrans); //shape moving repaint(); startDrag = endDrag; } else if(hitBox.getBounds().x + hitBox.getBounds().width >= 1000){ //if right edge is touched ytrans = endDrag.y - startDrag.y; hitBox.translate(-1, ytrans); repaint(); startDrag = endDrag; } else if(hitBox.getBounds().y <= 0){ //upper edge touched startDrag = new Point(e.getX(), 1); xtrans = endDrag.x - startDrag.x; ytrans = 1 + startDrag.y; hitBox.translate(xtrans, ytrans); repaint(); startDrag = endDrag; } else if(hitBox.getBounds().y + hitBox.getBounds().height >= 550){ //bottom edge touchedu xtrans = endDrag.x - startDrag.x; hitBox.translate(xtrans, -1); repaint(); startDrag = endDrag; } else if(gridPol.contains(endDrag)){ //moving inside grid for(Rectangle r : gridRect) { if(r.contains(endDrag)) { hitBox.translate(r.getBounds().x - hitBox.getBounds().x, r.getBounds().y - hitBox.getBounds().y); } } repaint(); startDrag = endDrag; } else { //moving inside window, outside grid xtrans = endDrag.x - startDrag.x; ytrans = endDrag.y - startDrag.y; hitBox.translate(xtrans, ytrans); repaint(); startDrag = endDrag; } } } }; addMouseListener(handler); addMouseMotionListener(handler); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; //painting background and grid frame g2d.drawImage(background, 0, 0, this); g2d.setColor(Color.DARK_GRAY); g2d.fillPolygon(gridPol); //painting grid int x=375; int y=175; int n=50; g2d.setColor(Color.YELLOW); for(int i=0; i<5; i++) { for(int j=0; j<5; j++) { g2d.drawRect(x,y,n,n); gridRect.add(new Rectangle(x,y,n,n)); x+=50; } x=375; y+=50; } //painting shapes g2d.setColor(ran_color); g2d.fillPolygon(shape1); g2d.fillPolygon(shape2); g2d.fillPolygon(shape3); g2d.fillPolygon(square); g2d.fillPolygon(rect); g2d.fillPolygon(t); g2d.dispose(); } } |
Comments
Post a Comment