Grafics and animation update
This commit is contained in:
parent
4621e60fc6
commit
961f5a3b0e
@ -1,32 +1,23 @@
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class Block extends Cell {
|
||||
// private Random random;
|
||||
|
||||
public Block() {
|
||||
super(-1, -1, WindowOfFreeMove.CELL_SIZE, Color.BLUE);
|
||||
// this.random = new Random();
|
||||
}
|
||||
|
||||
public Block(int x, int y) {
|
||||
super(x, y, WindowOfFreeMove.CELL_SIZE, Color.BLUE);
|
||||
// this.random = new Random();
|
||||
}
|
||||
|
||||
public boolean isInBlock(int x, int y) {
|
||||
if ((this.
|
||||
getX() == x) && (this.getY() == y))
|
||||
return true;
|
||||
return false;
|
||||
return (this.
|
||||
getX() == x) && (this.getY() == y);
|
||||
}
|
||||
|
||||
// public void randomAppear() {
|
||||
// int x, y;
|
||||
// do {
|
||||
// x = random.nextInt(WindowOfFreeMove.CANVAS_HEIGHT);
|
||||
// y = random.nextInt(WindowOfFreeMove.CANVAS_WIDTH);
|
||||
// } while (WindowOfFreeMove.getMap().isBusyCell(x, y));
|
||||
// set(x, y);
|
||||
// }
|
||||
@Override
|
||||
public void paint(Graphics2D g) {
|
||||
super.paint(g);
|
||||
g.fillOval(getX() * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE);
|
||||
}
|
||||
}
|
||||
@ -26,6 +26,5 @@ public class Cell {
|
||||
|
||||
public void paint(Graphics2D g) {
|
||||
g.setColor(color);
|
||||
g.fillOval(x * size, y * size, size, size);
|
||||
}
|
||||
}
|
||||
|
||||
@ -42,6 +42,7 @@ public class EventMoveToCell extends Cell {
|
||||
@Override
|
||||
public void paint(Graphics2D g) {
|
||||
super.paint(g);
|
||||
g.fillOval(getX() * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE);
|
||||
g.drawString(Integer.toString(getId()), getX()*WindowOfFreeMove.CELL_SIZE, getY()*WindowOfFreeMove.CELL_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
public class Game {
|
||||
public static void main(String[] args) {
|
||||
new WindowOfFreeMove().start();
|
||||
}
|
||||
new WindowOfFreeMove().start(); }
|
||||
}
|
||||
|
||||
|
||||
51
src/main/java/ImagePerson.java
Normal file
51
src/main/java/ImagePerson.java
Normal file
@ -0,0 +1,51 @@
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ImagePerson {
|
||||
private BufferedImage image;
|
||||
private BufferedImage[] imagesOfDirection;
|
||||
private Image[] animationOfMoving;
|
||||
|
||||
public void setAllImagesOfDirection(Rectangle[] rectangles){
|
||||
imagesOfDirection = new BufferedImage[4];
|
||||
|
||||
if (rectangles.length < 4)
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
for (int i = 0; i < imagesOfDirection.length; i++) {
|
||||
imagesOfDirection[i] = image.getSubimage(rectangles[i].x, rectangles[i].y, rectangles[i].height, rectangles[i].width);
|
||||
}
|
||||
}
|
||||
|
||||
public void setAnimationOfMoving(String[] animationOfMovingNames) {
|
||||
animationOfMoving = new Image[4];
|
||||
|
||||
if (animationOfMovingNames.length < 4)
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
for (int i = 0; i < animationOfMoving.length; i++){
|
||||
animationOfMoving[i] = Toolkit.getDefaultToolkit().createImage(getClass().getResource(animationOfMovingNames[i]));
|
||||
}
|
||||
}
|
||||
|
||||
public ImagePerson(String name, Rectangle[] rectangles) {
|
||||
try {
|
||||
image = ImageIO.read(getClass().getResource(name));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
setAllImagesOfDirection(rectangles);
|
||||
}
|
||||
|
||||
|
||||
public BufferedImage[] getImagesOfDirection() {
|
||||
return imagesOfDirection;
|
||||
}
|
||||
|
||||
public Image[] getAnimationOfMoving() {
|
||||
return animationOfMoving;
|
||||
}
|
||||
}
|
||||
@ -50,12 +50,12 @@ public class Map {
|
||||
type = TypeOfCell.EMPTY;
|
||||
|
||||
for (int i = 0; i < mapArray.length; i++) {
|
||||
for (int j = 0; j < mapArray.length; j++) {
|
||||
for (int j = 0; j < mapArray[0].length; j++) {
|
||||
mapArray[0][j] = type;
|
||||
mapArray[mapArray.length - 1][j] = type;
|
||||
}
|
||||
mapArray[i][0] = type;
|
||||
mapArray[i][mapArray.length - 1] = type;
|
||||
mapArray[i][mapArray[0].length - 1] = type;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,49 +1,65 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class Person extends Cell {
|
||||
private int direction;
|
||||
private ImagePerson imagePerson;
|
||||
private int directionMove;
|
||||
private int directionSight;
|
||||
|
||||
public Person() {
|
||||
super(-1, -1, WindowOfFreeMove.CELL_SIZE, Color.RED);
|
||||
this.direction = 0;
|
||||
this.directionMove = 0;
|
||||
this.directionSight = 3;
|
||||
}
|
||||
|
||||
public Person(int x, int y) {
|
||||
super(x, y, WindowOfFreeMove.CELL_SIZE, Color.RED);
|
||||
this.direction = 0;
|
||||
this.directionMove = 0;
|
||||
this.directionSight = 3;
|
||||
}
|
||||
|
||||
public void setDirection(int direction) {
|
||||
if ((direction >= WindowOfFreeMove.KEY_LEFT) && (direction <= WindowOfFreeMove.KEY_DOWN))
|
||||
this.direction = direction;
|
||||
public void setDirectionMove(int directionMove) {
|
||||
if ((directionMove >= WindowOfFreeMove.KEY_LEFT) && (directionMove <= WindowOfFreeMove.KEY_DOWN))
|
||||
this.directionMove = directionMove;
|
||||
}
|
||||
|
||||
public void setImagePerson(String name, Rectangle[] rectangles) {
|
||||
this.imagePerson = new ImagePerson(name, rectangles);
|
||||
}
|
||||
|
||||
public void setAnimation(String[] names){
|
||||
this.imagePerson.setAnimationOfMoving(names);
|
||||
}
|
||||
|
||||
public void move() {
|
||||
int x = this.getX();
|
||||
int y = this.getY();
|
||||
switch (direction) {
|
||||
switch (directionMove) {
|
||||
case WindowOfFreeMove.KEY_LEFT:
|
||||
directionSight = 1;
|
||||
x--;
|
||||
if (x < 0)
|
||||
x = WindowOfFreeMove.CANVAS_WIDTH - 1;
|
||||
break;
|
||||
case WindowOfFreeMove.KEY_RIGHT:
|
||||
directionSight = 3;
|
||||
x++;
|
||||
if (x == WindowOfFreeMove.CANVAS_WIDTH)
|
||||
x = 0;
|
||||
break;
|
||||
case WindowOfFreeMove.KEY_UP:
|
||||
directionSight = 2;
|
||||
y--;
|
||||
if (y < 0)
|
||||
y = WindowOfFreeMove.CANVAS_HEIGHT - 1;
|
||||
break;
|
||||
case WindowOfFreeMove.KEY_DOWN:
|
||||
directionSight = 0;
|
||||
y++;
|
||||
if (y == WindowOfFreeMove.CANVAS_HEIGHT)
|
||||
y = 0;
|
||||
break;
|
||||
}
|
||||
this.direction = 0;
|
||||
this.directionMove = 0;
|
||||
|
||||
if (WindowOfFreeMove.getMap().isCellMoveTo(x, y)) {
|
||||
WindowOfFreeMove.getMap().findEventByXY(x, y).moveToMap();
|
||||
@ -56,14 +72,30 @@ public class Person extends Cell {
|
||||
}
|
||||
|
||||
public boolean isInPerson(int x, int y) {
|
||||
if (this.getX() == x && this.getY() == y)
|
||||
return true;
|
||||
return false;
|
||||
return this.getX() == x && this.getY() == y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint(Graphics2D g) {
|
||||
super.paint(g);
|
||||
g.drawString(Integer.toString(WindowOfFreeMove.getMap().getIdExit()), getX() * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE);
|
||||
if (directionMove == 0)
|
||||
g.drawImage(imagePerson.getImagesOfDirection()[directionSight], getX()*WindowOfFreeMove.CELL_SIZE,
|
||||
getY()*WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null);
|
||||
else{
|
||||
switch (directionMove){
|
||||
case WindowOfFreeMove.KEY_LEFT:
|
||||
g.drawImage(imagePerson.getAnimationOfMoving()[3], getX()*WindowOfFreeMove.CELL_SIZE, getY()*WindowOfFreeMove.CELL_SIZE, null);
|
||||
break;
|
||||
case WindowOfFreeMove.KEY_RIGHT:
|
||||
g.drawImage(imagePerson.getAnimationOfMoving()[1], getX()*WindowOfFreeMove.CELL_SIZE, getY()*WindowOfFreeMove.CELL_SIZE, null);
|
||||
break;
|
||||
case WindowOfFreeMove.KEY_UP:
|
||||
g.drawImage(imagePerson.getAnimationOfMoving()[0], getX()*WindowOfFreeMove.CELL_SIZE, getY()*WindowOfFreeMove.CELL_SIZE, null);
|
||||
break;
|
||||
case WindowOfFreeMove.KEY_DOWN:
|
||||
g.drawImage(imagePerson.getAnimationOfMoving()[2], getX()*WindowOfFreeMove.CELL_SIZE, getY()*WindowOfFreeMove.CELL_SIZE, null);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,9 +4,9 @@ import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
|
||||
public class WindowOfFreeMove extends JFrame {
|
||||
static final int CELL_SIZE = 40;
|
||||
static final int CANVAS_WIDTH = 20;
|
||||
static final int CANVAS_HEIGHT = 20;
|
||||
static final int CELL_SIZE = 50;
|
||||
static final int CANVAS_WIDTH = 10;
|
||||
static final int CANVAS_HEIGHT = 10;
|
||||
|
||||
private static boolean canPersonMove;
|
||||
|
||||
@ -64,7 +64,7 @@ public class WindowOfFreeMove extends JFrame {
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
if (canPersonMove)
|
||||
map.getPerson().setDirection(e.getKeyCode());
|
||||
map.getPerson().setDirectionMove(e.getKeyCode());
|
||||
}
|
||||
});
|
||||
|
||||
@ -74,64 +74,78 @@ public class WindowOfFreeMove extends JFrame {
|
||||
|
||||
setTitle("Ahoj");
|
||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
setBounds(0, 0, CELL_SIZE * CANVAS_WIDTH, CELL_SIZE * CANVAS_HEIGHT);
|
||||
setBounds(0, 0, CELL_SIZE * CANVAS_WIDTH + 15, CELL_SIZE * CANVAS_HEIGHT + 35);
|
||||
setLocationRelativeTo(null);
|
||||
setResizable(true);
|
||||
setVisible(true);
|
||||
|
||||
}
|
||||
|
||||
public Canvas getCanvas() {
|
||||
return canvas;
|
||||
}
|
||||
|
||||
|
||||
public void start() {
|
||||
String[] strings = {"main-walk-up.gif", "main-walk-right.gif", "main-walk-down.gif", "main-walk-left.gif"};
|
||||
Rectangle[] rec = new Rectangle[4];
|
||||
for (int i = 0; i < rec.length; i++){
|
||||
rec[i] = new Rectangle(40, 40*i, 40, 40);
|
||||
}
|
||||
|
||||
Map m1 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, true);
|
||||
m1.getPerson().setImagePerson("mainpng.png", rec);
|
||||
m1.getPerson().setAnimation(strings);
|
||||
m1.setName("1");
|
||||
m1.setSpawnPoint(1, 1);
|
||||
Map m2 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, false);
|
||||
m2.getPerson().setImagePerson("mainpng.png", rec);
|
||||
m2.setName("2");
|
||||
Map m3 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, true);
|
||||
m3.getPerson().setImagePerson("mainpng.png", rec);
|
||||
m3.setName("3");
|
||||
Map m4 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, false);
|
||||
m4.getPerson().setImagePerson("mainpng.png", rec);
|
||||
m4.setName("4");
|
||||
|
||||
m1.addMoveToEvent(new EventMoveToCell(1, 1, 8, 8, m2));
|
||||
m1.addMoveToEvent(new EventMoveToCell(4, 3, 8, 1, m4));
|
||||
|
||||
m1.addMoveToEvent(new EventMoveToCell(1, 1, 18, 18, m2));
|
||||
m1.addMoveToEvent(new EventMoveToCell(4, 3, 18, 1, m4));
|
||||
m2.addMoveToEvent(new EventMoveToCell(2, 1, 8, 8, m3));
|
||||
m2.addMoveToEvent(new EventMoveToCell(1, 3, 8, 1, m1));
|
||||
|
||||
m2.addMoveToEvent(new EventMoveToCell(2, 1, 18, 18, m3));
|
||||
m2.addMoveToEvent(new EventMoveToCell(1, 3, 18, 1, m1));
|
||||
m3.addMoveToEvent(new EventMoveToCell(3, 1, 8, 8, m4));
|
||||
m3.addMoveToEvent(new EventMoveToCell(2, 3, 8, 1, m2));
|
||||
|
||||
m3.addMoveToEvent(new EventMoveToCell(3, 1, 18, 18, m4));
|
||||
m3.addMoveToEvent(new EventMoveToCell(2, 3, 18, 1, m2));
|
||||
|
||||
m4.addMoveToEvent(new EventMoveToCell(4, 1, 18, 18, m1));
|
||||
m4.addMoveToEvent(new EventMoveToCell(3, 3, 18, 1, m3));
|
||||
m4.addMoveToEvent(new EventMoveToCell(4, 1, 9, 9, m1));
|
||||
m4.addMoveToEvent(new EventMoveToCell(3, 3, 9, 1, m3));
|
||||
|
||||
setMap(m1);
|
||||
map.createMap();
|
||||
|
||||
lp.remove(dialogBar);
|
||||
|
||||
|
||||
JPanel jf = new JPanel() {
|
||||
Font f = new Font("Times New Roman", Font.ITALIC, 50);
|
||||
|
||||
@Override
|
||||
public void paint(Graphics g) {
|
||||
super.paint(g);
|
||||
g.setColor(Color.green);
|
||||
g.setFont(f);
|
||||
g.drawString("> Test", 10, 50);
|
||||
}
|
||||
};
|
||||
|
||||
jf.setBounds(0, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.CANVAS_HEIGHT * 3 / 4, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.CANVAS_WIDTH, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.CANVAS_HEIGHT / 4);
|
||||
jf.setBackground(Color.BLACK);
|
||||
jf.setVisible(false);
|
||||
|
||||
setDialogBar(jf);
|
||||
dialogBar.setVisible(true);
|
||||
lp.add(jf, JLayeredPane.POPUP_LAYER);
|
||||
|
||||
//
|
||||
// lp.remove(dialogBar);
|
||||
//
|
||||
//
|
||||
// JPanel jf = new JPanel() {
|
||||
// Font f = new Font("Times New Roman", Font.ITALIC, 50);
|
||||
//
|
||||
// @Override
|
||||
// public void paint(Graphics g) {
|
||||
// super.paint(g);
|
||||
// g.setColor(Color.green);
|
||||
// g.setFont(f);
|
||||
// g.drawString("> Test", 10, 50);
|
||||
// }
|
||||
// };
|
||||
//
|
||||
// jf.setBounds(0, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.CANVAS_HEIGHT * 3 / 4, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.CANVAS_WIDTH, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.CANVAS_HEIGHT / 4);
|
||||
// jf.setBackground(Color.BLACK);
|
||||
// jf.setVisible(false);
|
||||
//
|
||||
// setDialogBar(jf);
|
||||
// dialogBar.setVisible(true);
|
||||
//setCanPersonMove(false);
|
||||
// lp.add(jf, JLayeredPane.POPUP_LAYER);
|
||||
|
||||
while (true) {
|
||||
map.getPerson().move();
|
||||
|
||||
BIN
src/main/resources/main-walk-down.gif
Normal file
BIN
src/main/resources/main-walk-down.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.9 KiB |
BIN
src/main/resources/main-walk-left.gif
Normal file
BIN
src/main/resources/main-walk-left.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.4 KiB |
BIN
src/main/resources/main-walk-right.gif
Normal file
BIN
src/main/resources/main-walk-right.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.4 KiB |
BIN
src/main/resources/main-walk-up.gif
Normal file
BIN
src/main/resources/main-walk-up.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.5 KiB |
BIN
src/main/resources/mainpng.png
Normal file
BIN
src/main/resources/mainpng.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 24 KiB |
Loading…
x
Reference in New Issue
Block a user