Grafics and animation update

This commit is contained in:
iryupnik 2020-03-02 13:31:22 +02:00
parent 4621e60fc6
commit 961f5a3b0e
13 changed files with 165 additions and 77 deletions

View File

@ -1,32 +1,23 @@
import java.awt.*; import java.awt.*;
import java.util.Random;
public class Block extends Cell { public class Block extends Cell {
// private Random random;
public Block() { public Block() {
super(-1, -1, WindowOfFreeMove.CELL_SIZE, Color.BLUE); super(-1, -1, WindowOfFreeMove.CELL_SIZE, Color.BLUE);
// this.random = new Random();
} }
public Block(int x, int y) { public Block(int x, int y) {
super(x, y, WindowOfFreeMove.CELL_SIZE, Color.BLUE); super(x, y, WindowOfFreeMove.CELL_SIZE, Color.BLUE);
// this.random = new Random();
} }
public boolean isInBlock(int x, int y) { public boolean isInBlock(int x, int y) {
if ((this. return (this.
getX() == x) && (this.getY() == y)) getX() == x) && (this.getY() == y);
return true;
return false;
} }
// public void randomAppear() { @Override
// int x, y; public void paint(Graphics2D g) {
// do { super.paint(g);
// x = random.nextInt(WindowOfFreeMove.CANVAS_HEIGHT); g.fillOval(getX() * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE);
// y = random.nextInt(WindowOfFreeMove.CANVAS_WIDTH); }
// } while (WindowOfFreeMove.getMap().isBusyCell(x, y));
// set(x, y);
// }
} }

View File

@ -26,6 +26,5 @@ public class Cell {
public void paint(Graphics2D g) { public void paint(Graphics2D g) {
g.setColor(color); g.setColor(color);
g.fillOval(x * size, y * size, size, size);
} }
} }

View File

@ -42,6 +42,7 @@ public class EventMoveToCell extends Cell {
@Override @Override
public void paint(Graphics2D g) { public void paint(Graphics2D g) {
super.paint(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); g.drawString(Integer.toString(getId()), getX()*WindowOfFreeMove.CELL_SIZE, getY()*WindowOfFreeMove.CELL_SIZE);
} }
} }

View File

@ -1,5 +1,5 @@
public class Game { public class Game {
public static void main(String[] args) { public static void main(String[] args) {
new WindowOfFreeMove().start(); new WindowOfFreeMove().start(); }
}
} }

View 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;
}
}

View File

@ -50,12 +50,12 @@ public class Map {
type = TypeOfCell.EMPTY; type = TypeOfCell.EMPTY;
for (int i = 0; i < mapArray.length; i++) { 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[0][j] = type;
mapArray[mapArray.length - 1][j] = type; mapArray[mapArray.length - 1][j] = type;
} }
mapArray[i][0] = type; mapArray[i][0] = type;
mapArray[i][mapArray.length - 1] = type; mapArray[i][mapArray[0].length - 1] = type;
} }
} }

View File

@ -1,49 +1,65 @@
import java.awt.*; import java.awt.*;
public class Person extends Cell { public class Person extends Cell {
private int direction; private ImagePerson imagePerson;
private int directionMove;
private int directionSight;
public Person() { public Person() {
super(-1, -1, WindowOfFreeMove.CELL_SIZE, Color.RED); super(-1, -1, WindowOfFreeMove.CELL_SIZE, Color.RED);
this.direction = 0; this.directionMove = 0;
this.directionSight = 3;
} }
public Person(int x, int y) { public Person(int x, int y) {
super(x, y, WindowOfFreeMove.CELL_SIZE, Color.RED); super(x, y, WindowOfFreeMove.CELL_SIZE, Color.RED);
this.direction = 0; this.directionMove = 0;
this.directionSight = 3;
} }
public void setDirection(int direction) { public void setDirectionMove(int directionMove) {
if ((direction >= WindowOfFreeMove.KEY_LEFT) && (direction <= WindowOfFreeMove.KEY_DOWN)) if ((directionMove >= WindowOfFreeMove.KEY_LEFT) && (directionMove <= WindowOfFreeMove.KEY_DOWN))
this.direction = direction; 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() { public void move() {
int x = this.getX(); int x = this.getX();
int y = this.getY(); int y = this.getY();
switch (direction) { switch (directionMove) {
case WindowOfFreeMove.KEY_LEFT: case WindowOfFreeMove.KEY_LEFT:
directionSight = 1;
x--; x--;
if (x < 0) if (x < 0)
x = WindowOfFreeMove.CANVAS_WIDTH - 1; x = WindowOfFreeMove.CANVAS_WIDTH - 1;
break; break;
case WindowOfFreeMove.KEY_RIGHT: case WindowOfFreeMove.KEY_RIGHT:
directionSight = 3;
x++; x++;
if (x == WindowOfFreeMove.CANVAS_WIDTH) if (x == WindowOfFreeMove.CANVAS_WIDTH)
x = 0; x = 0;
break; break;
case WindowOfFreeMove.KEY_UP: case WindowOfFreeMove.KEY_UP:
directionSight = 2;
y--; y--;
if (y < 0) if (y < 0)
y = WindowOfFreeMove.CANVAS_HEIGHT - 1; y = WindowOfFreeMove.CANVAS_HEIGHT - 1;
break; break;
case WindowOfFreeMove.KEY_DOWN: case WindowOfFreeMove.KEY_DOWN:
directionSight = 0;
y++; y++;
if (y == WindowOfFreeMove.CANVAS_HEIGHT) if (y == WindowOfFreeMove.CANVAS_HEIGHT)
y = 0; y = 0;
break; break;
} }
this.direction = 0; this.directionMove = 0;
if (WindowOfFreeMove.getMap().isCellMoveTo(x, y)) { if (WindowOfFreeMove.getMap().isCellMoveTo(x, y)) {
WindowOfFreeMove.getMap().findEventByXY(x, y).moveToMap(); WindowOfFreeMove.getMap().findEventByXY(x, y).moveToMap();
@ -56,14 +72,30 @@ public class Person extends Cell {
} }
public boolean isInPerson(int x, int y) { public boolean isInPerson(int x, int y) {
if (this.getX() == x && this.getY() == y) return this.getX() == x && this.getY() == y;
return true;
return false;
} }
@Override @Override
public void paint(Graphics2D g) { public void paint(Graphics2D g) {
super.paint(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;
}
}
} }
} }

View File

@ -4,9 +4,9 @@ import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
public class WindowOfFreeMove extends JFrame { public class WindowOfFreeMove extends JFrame {
static final int CELL_SIZE = 40; static final int CELL_SIZE = 50;
static final int CANVAS_WIDTH = 20; static final int CANVAS_WIDTH = 10;
static final int CANVAS_HEIGHT = 20; static final int CANVAS_HEIGHT = 10;
private static boolean canPersonMove; private static boolean canPersonMove;
@ -64,7 +64,7 @@ public class WindowOfFreeMove extends JFrame {
@Override @Override
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
if (canPersonMove) if (canPersonMove)
map.getPerson().setDirection(e.getKeyCode()); map.getPerson().setDirectionMove(e.getKeyCode());
} }
}); });
@ -74,64 +74,78 @@ public class WindowOfFreeMove extends JFrame {
setTitle("Ahoj"); setTitle("Ahoj");
setDefaultCloseOperation(EXIT_ON_CLOSE); 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); setLocationRelativeTo(null);
setResizable(true); setResizable(true);
setVisible(true); setVisible(true);
} }
public Canvas getCanvas() {
return canvas;
}
public void start() { 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); Map m1 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, true);
m1.getPerson().setImagePerson("mainpng.png", rec);
m1.getPerson().setAnimation(strings);
m1.setName("1"); m1.setName("1");
m1.setSpawnPoint(1, 1); m1.setSpawnPoint(1, 1);
Map m2 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, false); Map m2 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, false);
m2.getPerson().setImagePerson("mainpng.png", rec);
m2.setName("2"); m2.setName("2");
Map m3 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, true); Map m3 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, true);
m3.getPerson().setImagePerson("mainpng.png", rec);
m3.setName("3"); m3.setName("3");
Map m4 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, false); Map m4 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, false);
m4.getPerson().setImagePerson("mainpng.png", rec);
m4.setName("4"); 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)); m2.addMoveToEvent(new EventMoveToCell(2, 1, 8, 8, m3));
m1.addMoveToEvent(new EventMoveToCell(4, 3, 18, 1, m4)); m2.addMoveToEvent(new EventMoveToCell(1, 3, 8, 1, m1));
m2.addMoveToEvent(new EventMoveToCell(2, 1, 18, 18, m3)); m3.addMoveToEvent(new EventMoveToCell(3, 1, 8, 8, m4));
m2.addMoveToEvent(new EventMoveToCell(1, 3, 18, 1, m1)); m3.addMoveToEvent(new EventMoveToCell(2, 3, 8, 1, m2));
m3.addMoveToEvent(new EventMoveToCell(3, 1, 18, 18, m4)); m4.addMoveToEvent(new EventMoveToCell(4, 1, 9, 9, m1));
m3.addMoveToEvent(new EventMoveToCell(2, 3, 18, 1, m2)); m4.addMoveToEvent(new EventMoveToCell(3, 3, 9, 1, m3));
m4.addMoveToEvent(new EventMoveToCell(4, 1, 18, 18, m1));
m4.addMoveToEvent(new EventMoveToCell(3, 3, 18, 1, m3));
setMap(m1); setMap(m1);
map.createMap(); map.createMap();
//
lp.remove(dialogBar); // lp.remove(dialogBar);
//
//
JPanel jf = new JPanel() { // JPanel jf = new JPanel() {
Font f = new Font("Times New Roman", Font.ITALIC, 50); // Font f = new Font("Times New Roman", Font.ITALIC, 50);
//
@Override // @Override
public void paint(Graphics g) { // public void paint(Graphics g) {
super.paint(g); // super.paint(g);
g.setColor(Color.green); // g.setColor(Color.green);
g.setFont(f); // g.setFont(f);
g.drawString("> Test", 10, 50); // 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.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.setBackground(Color.BLACK);
jf.setVisible(false); // jf.setVisible(false);
//
setDialogBar(jf); // setDialogBar(jf);
dialogBar.setVisible(true); // dialogBar.setVisible(true);
lp.add(jf, JLayeredPane.POPUP_LAYER); //setCanPersonMove(false);
// lp.add(jf, JLayeredPane.POPUP_LAYER);
while (true) { while (true) {
map.getPerson().move(); map.getPerson().move();

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB