Showing posts with label Java Graphics. Show all posts
Showing posts with label Java Graphics. Show all posts

Thursday, July 16, 2015

Memory Puzzle game in Java

Memory puzzle is a simple puzzle game of flipping and discovering pairs of similar cards. The player flips inverted cards one by one. If two cards flipped are same, they stay in that state, else they are flipped back to their original inverted state. The game ends when all the cards are flipped in pairs successfully.

Screenshot

[Image: memorypuzzle.png]

Source Code

Cards.java

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;


public class Cards {

private static BufferedImage card[] = new BufferedImage[9];
private static BufferedImage cardBack;

private static String cardFileName[] = {"1.jpg", "2.jpg", "3.jpg", "4.jpg",
"5.jpg", "6.jpg", "7.jpg", "8.jpg", "9.jpg"};
private static String cardBackName = "back.jpg";

static {
for(int i = 0; i < cardFileName.length; i++) {
try {
card[i] = ImageIO.read(Cards.class.getResourceAsStream(cardFileName[i]));
}
catch(Exception e) {
e.printStackTrace();
}
}

try {
cardBack = ImageIO.read(Cards.class.getResourceAsStream(cardBackName));
}
catch(Exception e) {
e.printStackTrace();
}
}

public static BufferedImage get(int v) {
return card[v];
}
public static BufferedImage getBack() {
return cardBack;
}
}

CardSlot.java

import java.awt.image.BufferedImage;


public class CardSlot {
private BufferedImage image;
private boolean visible, pairedUp;
private int value;

public CardSlot(int card) {
value = card;
image = Cards.get(card);
visible = false;
pairedUp = false;
}

public boolean isVisible() {
return visible;
}
public boolean flipCard() {
if(!pairedUp) {
visible = !visible;
return true;
}
return false;
}

public boolean isPairedUp() {
return pairedUp;
}
public void pairUp() {
pairedUp = true;
}

public BufferedImage getImage() {
if(isVisible())
return image;
else
return Cards.getBack();
}

public int getValue() {
return value;
}
}

Game.java

import java.awt.event.KeyEvent;
import java.util.Random;


public class Game {
private int width;
private int difficulty;
private int[][] board;
private int zX, zY;
private int forbidden;

public Game(int width, int difficulty) {
this.width = width;
this.difficulty = difficulty;

board = new int[width][width];

int ctr = 1;
for(int i = 0; i < width; i++) {
for(int j = 0; j < width; j++) {
board[j][i] = ctr++;
}
}
board[width - 1][width - 1] = 0;
zX = width - 1;
zY = width - 1;
forbidden = 2;

shuffle();
}

private void shuffle() {
Random random = new Random();
int i = 0;
while(i < difficulty) {
int v = random.nextInt(4);

switch(v) {
case 1:
if(up() && forbidden != 1) {
i++;
forbidden = 2;
}
break;
case 2:
if(down() && forbidden != 2) {
i++;
forbidden = 1;
}
break;
case 3:
if(left() && forbidden != 3) {
i++;
forbidden = 4;
}
break;
case 4:
if(right() && forbidden != 4) {
i++;
forbidden = 3;
}
break;
}
}
}

private boolean up() {
if(zY > 0) {
board[zX][zY] = board[zX][zY - 1];
board[zX][zY - 1] = 0;
zY--;
return true;
}
return false;
}
private boolean down() {
if(zY < width - 1) {
board[zX][zY] = board[zX][zY + 1];
board[zX][zY + 1] = 0;
zY++;
return true;
}
return false;
}
private boolean left() {
if(zX > 0) {
board[zX][zY] = board[zX - 1][zY];
board[zX - 1][zY] = 0;
zX--;
return true;
}
return false;
}
private boolean right() {
if(zX < width - 1) {
board[zX][zY] = board[zX + 1][zY];
board[zX + 1][zY] = 0;
zX++;
return true;
}
return false;
}

public boolean isCorrect() {
int ctr = 1;
for(int i = 0; i < width; i++) {
for(int j = 0; j < width; j++) {
if(board[j][i] == ctr++ || (i == width - 1 && j == width - 1))
continue;
else
return false;
}
}
return true;
}

public int getValueAt(int x, int y) {
if(x >= 0  && x <= width - 1 && y >= 0 && y <= width-1) {
return board[x][y];
}
return 0;
}

public void keyPressed(int key) {
switch(key) {
case KeyEvent.VK_UP:
up();
break;
case KeyEvent.VK_DOWN:
down();
break;
case KeyEvent.VK_LEFT:
left();
break;
case KeyEvent.VK_RIGHT:
right();
break;
}
}
}

SlidingPuzzlePanel.java

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class SlidingPuzzlePanel extends JPanel {

private static final long serialVersionUID = 4501938941435763747L;

private static final int SIDE = 4;
private static final int FONT_SIZE = 40;
private static int LEVEL = 10;

private Game game;
private Font font;
private Color background, foreground, borderColor;

private int slotWidth, slotHeight, slotXOffset, slotYOffset;

private int inX, inY, xOffset, yOffset;
private boolean dragged;

public SlidingPuzzlePanel() {
game = new Game(SIDE, LEVEL);
font = new Font(Font.SERIF, Font.BOLD, FONT_SIZE);

slotWidth = SlidingPuzzle.WIDTH / SIDE;
slotHeight = SlidingPuzzle.HEIGHT / SIDE;
slotXOffset = slotWidth / 2 - FONT_SIZE / 4;
slotYOffset = slotHeight / 2 + FONT_SIZE / 3;

background = new Color(123, 155, 232);
foreground = new Color(123, 255, 132);
borderColor = new Color(223, 255, 132);
dragged = false;

setFocusable(true);
requestFocus();

addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
if(dragged) {
inX = e.getXOnScreen() - xOffset;
inY = e.getYOnScreen() - yOffset;
SlidingPuzzle.frame.setLocation(inX, inY);
}
else {
dragged = true;
xOffset = e.getX();
yOffset = e.getY();
}
}

@Override
public void mouseMoved(MouseEvent e) {
dragged = false;
}
});

addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch(key) {
case KeyEvent.VK_ESCAPE:
SlidingPuzzle.frame.dispose();
break;
default:
game.keyPressed(key);
repaint();
if(game.isCorrect()) {
JOptionPane.showMessageDialog(SlidingPuzzle.frame, "You Win!");
LEVEL += 10;
game = new Game(SIDE, LEVEL);
repaint();
}
}
}
});
}

@Override
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.setFont(font);
g2d.setColor(background);
g2d.fillRect(0, 0, getWidth(), getHeight());
drawBorders(g2d);

g2d.setColor(foreground);
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
if(game.getValueAt(j, i) != 0)
g2d.drawString(""+game.getValueAt(j, i), slotXOffset + j * slotWidth,
slotYOffset + i * slotHeight);
}
}
}
private void drawBorders(Graphics2D g2d) {
g2d.setColor(borderColor);
for(int i = 0; i < 3; i++)
g2d.drawRect(i, i, SlidingPuzzle.WIDTH - 1 - 2 * i, SlidingPuzzle.HEIGHT - 1 - 2 * i);

for(int  i = 1; i < 4; i++) {
int level = i * slotHeight;
g2d.drawLine(0, level, SlidingPuzzle.WIDTH, level);
g2d.drawLine(0, level + 1, SlidingPuzzle.WIDTH, level + 1);
g2d.drawLine(0, level + 2, SlidingPuzzle.WIDTH, level + 2);

g2d.drawLine(level, 0, level, SlidingPuzzle.HEIGHT);
g2d.drawLine(level + 1, 0, level + 1, SlidingPuzzle.HEIGHT);
g2d.drawLine(level + 2, 0, level + 2, SlidingPuzzle.HEIGHT);
}
}
}

SlidingPuzzle.java

import javax.swing.JFrame;
import javax.swing.SwingUtilities;


public class SlidingPuzzle {
public static final int WIDTH = 512;
public static final int HEIGHT = 512;

public static JFrame frame;

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
frame = new JFrame("Sliding Puzzle");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setUndecorated(true);
frame.setSize(WIDTH, HEIGHT);

SlidingPuzzlePanel spp = new SlidingPuzzlePanel();
frame.add(spp);

frame.setVisible(true);
}
});
}
}


Download Runnable Jar:- Memory Puzzle 
Credits for Game:- Solixious

Continue Reading →

Wednesday, July 15, 2015

Sliding Puzzle Game in Java

Sliding Puzzle is a puzzle game in which numbers are arranged in an unordered fashion in a 4x4 grid along with one slot missing. You have to use the four arrow keys to slide the adjacent grid value into the blank grid and ultimately getting the grid in order.

Screenshot

[Image: slidingpuzzle.png]

Source Code 


Game.Java
 
SlidingPuzzlePanel.java
 
SlidingPuzzle.java
Credits for Game :- Solixious


Continue Reading →

Space Warrior Game in Java

The game simply consists of a ship that travels through space encountering asteroids and villains in the way. The game ends when the final boss is destroyed. FYI, I myself could not complete this game, except in the testing stage where I manipulated code to call boss very early.

Screenshots:

[Image: sw1.png]

[Image: sw2.png] 

[Image: sw3.png] 

Runnable JAR : space_warrior.jar
Java 8 or higher is recommended for execution of the runnable JAR file.

Github Link : Space Warrior

Credits for the sprite images go to Ari Feldman.

Credits for this Game :- Solixious

 
Continue Reading →

Wednesday, July 8, 2015

Picture Puzzle Game in Java

Picture Puzzle is game where a picture is broken into different pieces and all these pieces are scattered all over the screen. Your objective in the game is to arrange these scattered pieces in correct order using your mouse.

A default picture is provided in the runnable jar whose link I'll give at end of this thread. You can also select your own picture by pressing SPACE after the game starts and choosing the image file of your choice.

[Image: pic_puzzle.png] 

Source Code

PicturePuzzle.java
PicturePuzzlePanel.java
Game.java
PuzzlePiece.java
PuzzlePic.java

Download Runnable Jar :  Click Here to Download
Continue Reading →

Wednesday, July 1, 2015

Create Vector images using Bit Arrays in Java

Hello Everyone,


Today I will show you how to create a simple image by using code in Java in Applet. I have used Netbeans IDE 7.3 beta 2 and Java 7. You must have a bit knowledge about java and Applets for you to understand this code properly.

We will create a simple smiley and a hut. To create a simple hut or smiley we have to create a pixel array of the image which we will use to create a image. We will make a pixel Array of 16 X 16 bit as shown in the image below..

 

[Image: create_image_zps27f07944.png]
[Image: tongue_zps23d8d0a2.png]



If you use an IDE like Netbeans or eclipse then you can see the image as an IDE traces out similar variable and highlights them and so it will be easier for you to create them.

we create two object of image class in which the image will be stored or created.

Image smiley;
Image house;


Next we instantiate the colors we want to use for displaying the image

protected static final int w = Color.white.getRGB();
protected static final int y = Color.yellow.getRGB();
protected static final int b = Color.black.getRGB();
protected static final int r = Color.red.getRGB();
protected static final int g = Color.green.getRGB();


Here are the pixel arrays for hut and smiley1 = :) and Smiley2 = :P

protected static final int imageData[] = {
        w, w, w, y, y, y, y, y, y, y, y, y, y, w, w, w,
        w, w, y, y, y, y, y, y, y, y, y, y, y, y, w, w,
        w, y, y, y, y, y, y, y, y, y, y, y, y, y, y, w,
        w, y, y, y, b, b, y, y, y, y, b, b, y, y, y, w,
        y, y, y, y, b, b, y, y, y, y, b, b, y, y, y, y,
        y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y,
        y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y,
        y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y,
        y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y,
        y, y, y, b, y, y, y, y, y, y, y, y, b, y, y, y,
        y, y, y, y, b, y, y, y, y, y, y, b, y, y, y, y,
        y, y, y, y, y, b, b, y, y, b, b, y, y, y, y, y,
        w, y, y, y, y, y, y, b, b, y, y, y, y, y, y, w,
        w, y, y, y, y, y, y, y, y, y, y, y, y, y, y, w,
        w, w, y, y, y, y, y, y, y, y, y, y, y, y, w, w,
        w, w, w, y, y, y, y, y, y, y, y, y, y, w, w, w
    };

protected static final int imageData2[] = {
        w, w, w, b, b, b, b, b, b, b, b, b, b, w, w, w,
        w, w, b, g, g, g, g, g, g, g, g, g, g, b, w, w,
        w, b, g, g, g, g, g, g, g, g, g, g, g, g, b, w,
        b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b,
        b, y, y, y, y, y, y, y, y, y, y, y, y, y, y, b,
        b, y, y, y, y, y, y, y, y, y, y, y, y, y, y, b,
        b, y, y, b, b, y, y, r, r, y, y, b, b, y, y, b,
        b, y, y, b, b, y, y, r, r, y, y, b, b, y, y, b,
        b, y, y, y, y, y, y, r, r, y, y, y, y, y, y, b,
        b, y, y, y, y, y, y, r, r, y, y, y, y, y, y, b,
        b, y, y, y, y, y, y, r, r, y, y, y, y, y, y, b,
        b, b, b, b, b, b,  b, b, b, b, b, b, b,  b, b, b,
        w, w, w, w, w, w, w, w, w, w, w, w, w, w, w, w,
        w, w, w, w, w, w, w, w, w, w, w, w, w, w, w, w,
        w, w, w, w, w, w, w, w, w, w, w, w, w, w, w, w,
        w, w, w, w, w, w, w, w, w, w, w, w, w, w, w, w
};

protected static final int imageData3[] = {
        w, w, w, y, y, y, y, y, y, y, y, y, y, w, w, w,
        w, w, y, y, y, y, y, y, y, y, y, y, y, y, w, w,
        w, y, y, y, y, y, y, y, y, y, y, y, y, y, y, w,
        w, y, y, y, b, b, y, y, y, y, b, b, y, y, y, w,
        y, y, y, y, b, b, y, y, y, y, b, b, y, y, y, y,
        y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y,
        y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y,
        y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y,
        y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y,
        y, y, y, b, y, y, y, y, y, y, y, y, b, y, y, y,
        y, y, y, y, b, y, y, y, y, y, y, b, r, y, y, y,
        y, y, y, y, y, b, b, y, y, b, b, r, r, y, y, y,
        w, y, y, y, y, y, y, b, b, r, r, r, r, y, y, w,
        w, y, y, y, y, y, y, y, y, r, r, r, r, y, y, w,
        w, w, y, y, y, y, y, y, y, r, r, r, r, y, w, w,
        w, w, w, y, y, y, y, y, y, y, r, r, y, w, w, w
};


Here's the complete code :-


Here is the resulting output image :-

[Image: newsmiley_zpsfdbadd83.png]

How this will help you ?

This will help you in many ways. Suppose that you are writing some kind of game and with this you can make your own gaming vectors like arrows, pointers, small cars or human figures. Even of you wan then you can colorize these, it depends upon the programmer on how will he/she is able to apply a concept.


Have fun coding.

Thank you,
Sincerely,
Psycho_Coder

Continue Reading →

Monday, June 15, 2015

L-Systems Dragon Curve Fractal in Java

I just love generative art and fractals. I made another fractal called Dragon Curve in Java. In this post I will share the code for the fractal.

Here's the code

Image generated

[Image: vpmPoOv.png] 

I simply modified it a bit again and got something like following :- 

[Image: jN1OGL7l.png] 

Code for the modified one is as follows :- 

Continue Reading →

Saturday, June 13, 2015

L-Systems Tree Fractal in Java

Tree Fractal example code.
[Image: lsys_zps4b1319f7.png]I

I made it a bit more shiny. I modified the above code a bit and got the following. [Code for modified fractal given below]

[Image: ZNhtqWk.png]




Share this post on your social networks if you like this post.

Thank you.
Continue Reading →

Follow Me!

Followers

Visitor Map