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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
package com.game;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import com.map.Map;
import com.personnage.Hero;
public class Game extends JFrame{
private static final long serialVersionUID = 1L;
JLayeredPane container = new JLayeredPane();
private static Hero perso;
private Map map;
JScrollPane scroll;
JDesktopPane jdp = new JDesktopPane();
//private int nbCellH;
//private int nbCellV;
private final void windowConfiguration(){
//nbCellH = (int)(getSize().width / 32);
//nbCellV = (int)(getSize().height / 32);
//container.add(jdp);
this.setUndecorated(true);
this.setExtendedState(Game.MAXIMIZED_BOTH);
this.setBackground(Color.BLACK);
this.setContentPane(container);
this.addKeyListener(new KeyBoardListener());
this.setLayout(new BorderLayout(0, 0));
this.setVisible(true);
}
private final void index(){
container.removeAll();
this.map = new Map("main");
map();
container.add(scroll, BorderLayout.NORTH, new Integer(0));
container.add(state(), BorderLayout.SOUTH, new Integer(0));
container.add(hero(), 2, 0); // Works but there is a bug in the console
scroll.revalidate();
scroll.repaint();
container.revalidate();
container.repaint();
}
public Game(Hero hero){
Game.perso = hero;
windowConfiguration();
index();
}
@SuppressWarnings("serial")
private final JPanel hero(){
JPanel hero = new JPanel(){
public void paintComponent(Graphics g){
try{
Image img = ImageIO.read(new File("bin/com/media/hero.png"));
g.drawImage(img, 0, 0, null);
}catch(IOException e){
e.printStackTrace();
System.exit(1);
}
}
};
hero.setBounds(getSize().width / 2, (getSize().height - 64) / 2, 32, 32);
return hero;
}
/**
* Move the camera with 1px/15milliseconds
* @param direction
*/
private final void move(String direction){
if(direction.equals("west") && map.canWalk(perso.getPosX() + 1, perso.getPosY())){
final JScrollBar bar = scroll.getHorizontalScrollBar();
for(int i = 0; i < 32; i++){
bar.setValue(bar.getValue() + 1);
try{
Thread.sleep(15);
}catch(InterruptedException e1){
e1.printStackTrace();
}
}
perso.addPosX();
}else if(direction.equals("south") && map.canWalk(perso.getPosX(), perso.getPosY() + 1)){
final JScrollBar bar = scroll.getVerticalScrollBar();
for(int i = 0; i < 32; i++){
bar.setValue(bar.getValue() + 1);
try{
Thread.sleep(15);
}catch(InterruptedException e1){
e1.printStackTrace();
}
}
perso.addPosY();
}else if(direction.equals("east") && map.canWalk(perso.getPosX() - 1, perso.getPosY())){
final JScrollBar bar = scroll.getHorizontalScrollBar();
for(int i = 0; i < 32; i++){
bar.setValue(bar.getValue() - 1);
try{
Thread.sleep(15);
}catch(InterruptedException e1){
e1.printStackTrace();
}
}
perso.subtractPosX();
}else if(direction.equals("north") && map.canWalk(perso.getPosX(), perso.getPosY() - 1)){
final JScrollBar bar = scroll.getVerticalScrollBar();
for(int i = 0; i < 32; i++){
bar.setValue(bar.getValue() - 1);
try{
Thread.sleep(15);
}catch(InterruptedException e1){
e1.printStackTrace();
}
}
perso.subtractPosY();
}
}
/**
* Initializes the JPanel map with the scroll
*/
private final void map(){
JPanel map = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.NONE;
// Put all JPanel for the map
for(int i = 0; i < this.map.getMap().size(); i++){
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.gridy = i;
for(int ii = 0; ii < this.map.getMap().get(i).size(); ii++){
gbc.gridx = ii;
CellPanel square = new CellPanel(this.map.getBackground(i, ii));
if(ii == this.map.getMap().get(i).size() - 1){
gbc.gridwidth = GridBagConstraints.REMAINDER;
}
map.add(square, gbc);
}
gbc.gridwidth = GridBagConstraints.REMAINDER;
}
// Make the scroll
JScrollPane scrol = new JScrollPane(map);
scrol.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrol.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
scrol.setMinimumSize(new Dimension(getSize().width, getSize().height - 64));
scrol.setPreferredSize(new Dimension(getSize().width, getSize().height - 64));
scrol.setSize(new Dimension(getSize().width, getSize().height - 64));
scrol.setMaximumSize(new Dimension(getSize().width, getSize().height - 64));
final JScrollBar barH = scrol.getHorizontalScrollBar();
final JScrollBar barV = scrol.getVerticalScrollBar();
barH.setValue(perso.getPosX() * 32 - getSize().width / 2);
barV.setValue(perso.getPosY() * 32 - getSize().height / 2);
this.scroll = scrol;
}
/**
* Return JLabel which contains the Hero's life / mana / gold / xp
* @return JPanel
*/
private final JPanel state(){
JPanel state = new JPanel();
JPanel life = new JPanel();
JPanel mana = new JPanel();
JPanel gold = new JPanel();
JPanel xp = new JPanel();
state.setPreferredSize(new Dimension(getSize().width, 64));
life.setPreferredSize(new Dimension(280, 28));
mana.setPreferredSize(new Dimension(280, 28));
gold.setPreferredSize(new Dimension(280, 28));
xp.setPreferredSize(new Dimension(280, 28));
JLabel nbGold = new JLabel(Double.toString(perso.getGold()));
nbGold.setFont(new Font("Arial", Font.BOLD, 14));
nbGold.setPreferredSize(new Dimension(236, 28));
life.add(bar("red", perso.getAllMaxLife(), perso.getLife()));
mana.add(bar("blue", perso.getAllMaxMana(), perso.getMana()));
gold.add(nbGold);
xp.add(bar("purple", perso.getXpMax(), perso.getXp()));
state.add(life);
state.add(mana);
state.add(gold);
state.add(xp);
return state;
}
/**
* Return a bar to show the Hero's life / mana
* @param color blue / red
* @param max
* @param cur
*/
@SuppressWarnings("serial")
private final JPanel bar(final String color, final int max, final int cur){
JPanel container = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
final Color c;
if(color.equals("blue")){
c = new Color(0, 0, 255);
}else if(color.equals("red")){
c = new Color(255, 0, 0);
}else if(color.equals("purple")){
c = new Color(255, 0, 255);
}else{
c = new Color(255, 255, 255);
}
JPanel barContainer = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
JPanel bar = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0)){
public void paintComponent(Graphics g){
g.setColor(c);
try{
g.fillRect(0, 0, (int)(236 * cur / max), 25);
}catch(ArithmeticException e){
System.err.println("ERROR max = 0, division by 0 in com.game.Game.java - bar()");
}
}
};
JPanel image = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0)){
public void paintComponent(Graphics g){
try{
if(color.equals("red")){
Image img = ImageIO.read(new File("bin/com/media/heart.png"));
g.drawImage(img, 0, 0, this);
}else if(color.equals("purple")){
Image img = ImageIO.read(new File("bin/com/media/xp.png"));
g.drawImage(img, 0, 0, this);
}else if(color.equals("blue")){
Image img = ImageIO.read(new File("bin/com/media/mana.png"));
g.drawImage(img, 0, 0, this);
}
}catch (IOException e){
e.printStackTrace();
}
}
};
JLabel write = new JLabel(cur + " / " + max);
image.setPreferredSize(new Dimension(25, 25));
barContainer.setPreferredSize(new Dimension(236, 40));
bar.setPreferredSize(new Dimension(236, 32));
barContainer.setBorder(BorderFactory.createLineBorder(Color.black, 1));
bar.setBackground(c);
write.setFont(new Font("Arial", Font.BOLD, 14));
write.setPreferredSize(new Dimension(226, 20));
write.setHorizontalAlignment(JLabel.CENTER);
bar.add(write);
barContainer.add(bar);
container.add(image);
container.add(barContainer);
return container;
}
/**
* Save the game with serialize
*/
private final void save(){
ObjectOutputStream oos;
File file = new File("C:/Users/Portable 14/workspace/World of Chaos/saves/" + perso.getName().toLowerCase() + ".txt");
try{
oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
oos.writeObject(perso);
oos.close();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
private class KeyBoardListener implements KeyListener{
private long lastMove = 0L; // fix a little bug
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == 27){ // ECHAP
jdp.add(new Options());
container.revalidate();container.repaint();
}else if(e.getKeyCode() == 68 && System.currentTimeMillis() - lastMove > 9){ // Q
move("west");
lastMove = System.currentTimeMillis();
}else if(e.getKeyCode() == 83 && System.currentTimeMillis() - lastMove > 9){ // S
move("south");
lastMove = System.currentTimeMillis();
}else if(e.getKeyCode() == 81 && System.currentTimeMillis() - lastMove > 9){ // D
move("east");
lastMove = System.currentTimeMillis();
}else if(e.getKeyCode() == 90 && System.currentTimeMillis() - lastMove > 9){ // Z
move("north");
lastMove = System.currentTimeMillis();
}
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
}
/**
* Doesn't work right now but doesn't matter
*/
private class Options extends JInternalFrame{
private static final long serialVersionUID = 3L;
public Options(){
this.setTitle("Options");
this.setClosable(true);
this.setResizable(false);
this.setSize(200, 400);
this.setLocation(200, 200);
this.setVisible(true);
JButton saveAndQuit = new JButton("Save & quit");
saveAndQuit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
save();
System.exit(0);
}
});
JButton quit = new JButton("Quit");
quit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
this.add(saveAndQuit);
this.add(quit);
}
}
} |
Partager