Player added
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
package control;
|
package control;
|
||||||
|
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
import java.awt.event.KeyListener;
|
import java.awt.event.KeyListener;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
@@ -7,18 +9,23 @@ import java.awt.event.MouseListener;
|
|||||||
import java.awt.event.WindowEvent;
|
import java.awt.event.WindowEvent;
|
||||||
import java.awt.event.WindowFocusListener;
|
import java.awt.event.WindowFocusListener;
|
||||||
|
|
||||||
|
import javax.swing.Timer;
|
||||||
|
|
||||||
import model.GameModel;
|
import model.GameModel;
|
||||||
import view.GameView;
|
import view.GameView;
|
||||||
|
|
||||||
public class GameControl implements MouseListener, KeyListener, WindowFocusListener{
|
public class GameControl implements MouseListener, KeyListener, WindowFocusListener, ActionListener{
|
||||||
|
|
||||||
GameModel model;
|
GameModel model;
|
||||||
GameView view;
|
GameView view;
|
||||||
|
Timer update;
|
||||||
|
|
||||||
public GameControl(GameModel model, GameView view)
|
public GameControl(GameModel model, GameView view)
|
||||||
{
|
{
|
||||||
this.model = model;
|
this.model = model;
|
||||||
this.view = view;
|
this.view = view;
|
||||||
|
update = new Timer(1000/10, this);
|
||||||
|
update.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void keyPressed(KeyEvent e) {}
|
public void keyPressed(KeyEvent e) {}
|
||||||
@@ -45,4 +52,9 @@ public class GameControl implements MouseListener, KeyListener, WindowFocusListe
|
|||||||
public void windowGainedFocus(WindowEvent e) {}
|
public void windowGainedFocus(WindowEvent e) {}
|
||||||
|
|
||||||
public void windowLostFocus(WindowEvent e) {}
|
public void windowLostFocus(WindowEvent e) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
model.update();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package image;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
|
import main.Main;
|
||||||
|
|
||||||
|
public class Images{
|
||||||
|
|
||||||
|
public static ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();
|
||||||
|
|
||||||
|
public Images(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static{
|
||||||
|
try{
|
||||||
|
images.add(ImageIO.read(Main.class.getResource("/image/player.png")));
|
||||||
|
|
||||||
|
}catch(IOException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BufferedImage getImage(ImageType img)
|
||||||
|
{
|
||||||
|
return images.get(img.ordinal());
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum ImageType
|
||||||
|
{
|
||||||
|
player
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 8.1 KiB |
+10
-1
@@ -2,12 +2,21 @@ package model;
|
|||||||
|
|
||||||
import view.GameView;
|
import view.GameView;
|
||||||
|
|
||||||
public class GameModel {
|
public class GameModel{
|
||||||
|
|
||||||
GameView view;
|
GameView view;
|
||||||
|
Player player;
|
||||||
|
|
||||||
|
|
||||||
public GameModel(GameView view)
|
public GameModel(GameView view)
|
||||||
{
|
{
|
||||||
this.view = view;
|
this.view = view;
|
||||||
|
player = new Player(1920/2, 1080/2);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void update(){
|
||||||
|
player.update();
|
||||||
|
view.setPlayer(player);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package model;
|
||||||
|
|
||||||
|
import image.Images;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.Graphics2D;
|
||||||
|
import java.awt.geom.AffineTransform;
|
||||||
|
import java.awt.geom.Point2D;
|
||||||
|
import java.awt.geom.Rectangle2D;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
|
||||||
|
public class Player {
|
||||||
|
|
||||||
|
private Point2D middlePoint;
|
||||||
|
private int index = 2,width,height;
|
||||||
|
private AffineTransform transform;
|
||||||
|
private BufferedImage img;
|
||||||
|
|
||||||
|
public Player(int x, int y){
|
||||||
|
middlePoint = new Point2D.Double(x, y);
|
||||||
|
transform = new AffineTransform();
|
||||||
|
img = Images.getImage(Images.ImageType.player);
|
||||||
|
width = img.getWidth();
|
||||||
|
height = img.getHeight();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void draw(Graphics2D g2){//
|
||||||
|
g2.drawImage(img, transform, null);
|
||||||
|
g2.setPaint(Color.RED);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void update(){
|
||||||
|
transform = new AffineTransform();
|
||||||
|
transform.rotate(Math.toRadians(index*45),middlePoint.getX(),middlePoint.getY());
|
||||||
|
transform.translate(middlePoint.getX() - width/2, middlePoint.getY() - height*2);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,9 +8,16 @@ import java.awt.event.ActionListener;
|
|||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
import javax.swing.Timer;
|
import javax.swing.Timer;
|
||||||
|
|
||||||
|
import model.Player;
|
||||||
|
|
||||||
public class GameView extends JPanel implements ActionListener{
|
public class GameView extends JPanel implements ActionListener{
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = 1939480784205689618L;
|
||||||
Timer t;
|
Timer t;
|
||||||
|
Player player;
|
||||||
|
|
||||||
public GameView()
|
public GameView()
|
||||||
{
|
{
|
||||||
@@ -26,5 +33,12 @@ public class GameView extends JPanel implements ActionListener{
|
|||||||
{
|
{
|
||||||
super.paintComponent(g);
|
super.paintComponent(g);
|
||||||
Graphics2D g2d = (Graphics2D) g;
|
Graphics2D g2d = (Graphics2D) g;
|
||||||
|
|
||||||
|
if(player != null)
|
||||||
|
player.draw(g2d);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPlayer(Player player){
|
||||||
|
this.player = player;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user