Optimized code, everything is buffered in a buffered image now
This commit is contained in:
@@ -2,6 +2,7 @@ package control;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.TimerTask;
|
||||
|
||||
import javax.swing.Timer;
|
||||
|
||||
@@ -12,7 +13,7 @@ import control.button.ButtonListener;
|
||||
import control.joystick.JoystickEvent;
|
||||
import control.joystick.JoystickListener;
|
||||
|
||||
public class GameControl implements JoystickListener, ButtonListener,ActionListener {
|
||||
public class GameControl implements JoystickListener, ButtonListener, ActionListener {
|
||||
|
||||
private long lastTime = System.currentTimeMillis();
|
||||
GameModel model;
|
||||
@@ -20,12 +21,13 @@ public class GameControl implements JoystickListener, ButtonListener,ActionListe
|
||||
GameStateManager gsm;
|
||||
Timer update;
|
||||
|
||||
public GameControl(GameModel model, GameView view,GameStateManager gsm)
|
||||
public GameControl(final GameModel model, final GameView view,GameStateManager gsm)
|
||||
{
|
||||
this.model = model;
|
||||
this.view = view;
|
||||
this.gsm = gsm;
|
||||
update = new Timer(1000/60, this);
|
||||
view.setIgnoreRepaint(true);
|
||||
update = new Timer(1000/60,this);
|
||||
update.start();
|
||||
}
|
||||
|
||||
@@ -51,6 +53,7 @@ public class GameControl implements JoystickListener, ButtonListener,ActionListe
|
||||
long currentTime = System.currentTimeMillis();
|
||||
model.update(currentTime - lastTime);
|
||||
lastTime = currentTime;
|
||||
view.repaint();
|
||||
view.repaint();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ public class GameStateManager {
|
||||
private List<GameState> gamestates;
|
||||
public GameState currentState;
|
||||
private int index = 0;
|
||||
public int fps;
|
||||
|
||||
public enum State {
|
||||
TITLE_STATE,
|
||||
@@ -44,5 +45,6 @@ public class GameStateManager {
|
||||
|
||||
public void update(float factor){
|
||||
currentState.update(factor);
|
||||
fps = (int) (60/(factor/10));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
package control;
|
||||
|
||||
public class JoyStick {
|
||||
|
||||
}
|
||||
@@ -73,26 +73,4 @@ public class LedHandler {
|
||||
err.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void strobo(){
|
||||
boolean on = true;
|
||||
while(true){
|
||||
if(on){
|
||||
for(int i = 1; i < 66; i++){
|
||||
setLed(i, 0, 0, 0);
|
||||
}
|
||||
}else{
|
||||
for(int i = 1; i < 66; i++){
|
||||
setLed(i, 255, 255, 255);
|
||||
}
|
||||
}
|
||||
on = !on;
|
||||
show();
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+38
-6
@@ -1,5 +1,8 @@
|
||||
package image;
|
||||
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.GraphicsConfiguration;
|
||||
import java.awt.GraphicsEnvironment;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -19,12 +22,12 @@ public class Images {
|
||||
|
||||
static {
|
||||
try {
|
||||
images.add(ImageIO.read(Main.class.getResource("/image/player.png")));
|
||||
images.add(ImageIO.read(Main.class.getResource("/image/player2.png")));
|
||||
images.add(ImageIO.read(Main.class.getResource("/image/pressstart.png")));
|
||||
images.add(ImageIO.read(Main.class.getResource("/image/colorstrike.png")));
|
||||
images.add(ImageIO.read(Main.class.getResource("/image/background.png")));
|
||||
images.add(ImageIO.read(Main.class.getResource("/image/aanwijzers4sho.png")));
|
||||
images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/player.png"))));
|
||||
images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/player2.png"))));
|
||||
images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/pressstart.png"))));
|
||||
images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/colorstrike.png"))));
|
||||
images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/background.png"))));
|
||||
images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/aanwijzers4sho.png"))));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -48,4 +51,33 @@ public class Images {
|
||||
|
||||
return bf;
|
||||
}
|
||||
|
||||
public static BufferedImage toCompatibleImage(BufferedImage image)
|
||||
{
|
||||
// obtain the current system graphical settings
|
||||
GraphicsConfiguration gfx_config = GraphicsEnvironment.
|
||||
getLocalGraphicsEnvironment().getDefaultScreenDevice().
|
||||
getDefaultConfiguration();
|
||||
|
||||
/*
|
||||
* if image is already compatible and optimized for current system
|
||||
* settings, simply return it
|
||||
*/
|
||||
if (image.getColorModel().equals(gfx_config.getColorModel()))
|
||||
return image;
|
||||
|
||||
// image is not optimized, so create a new image that is
|
||||
BufferedImage new_image = gfx_config.createCompatibleImage(
|
||||
image.getWidth(), image.getHeight(), image.getTransparency());
|
||||
|
||||
// get the graphics context of the new image to draw the old image on
|
||||
Graphics2D g2d = (Graphics2D) new_image.getGraphics();
|
||||
|
||||
// actually draw the image and dispose of context no longer needed
|
||||
g2d.drawImage(image, 0, 0, null);
|
||||
g2d.dispose();
|
||||
|
||||
// return the new optimized image
|
||||
return new_image;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,14 +9,11 @@ public class GameModel{
|
||||
|
||||
public static Color[] colors = {Color.GREEN,Color.YELLOW,Color.RED,Color.MAGENTA,Color.CYAN,Color.WHITE};
|
||||
private GameStateManager gsm;
|
||||
private SongHandler sh;
|
||||
|
||||
public GameModel(SongHandler sh, GameStateManager gsm)
|
||||
{
|
||||
this.gsm = gsm;
|
||||
|
||||
this.sh = sh;
|
||||
|
||||
for(int i = 1; i < ButtonHandler.getButtons().size(); i++){
|
||||
ButtonHandler.getButtons().get(i).setColor(colors[i-1]);;
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ public abstract class GameState {
|
||||
|
||||
public abstract void init();
|
||||
public abstract void update(float factor);
|
||||
public abstract void draw(Graphics2D g2);
|
||||
public abstract void draw(Graphics2D g2);
|
||||
public abstract void buttonPressed(ButtonEvent e);
|
||||
public abstract void buttonReleased(ButtonEvent e);
|
||||
public abstract void onJoystickMoved(JoystickEvent e);
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.awt.Font;
|
||||
import java.awt.GradientPaint;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Polygon;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -30,14 +31,14 @@ public class MenuState extends GameState {
|
||||
private ArrayList<DifficultyButton> buttons2;
|
||||
private int selected, oldselected;
|
||||
private List<Song> songs;
|
||||
private Polygon triangle,triangle2;
|
||||
|
||||
private int animationcounter;
|
||||
private boolean subscreen, startanimation;
|
||||
private BufferedImage mainScreenBackground;
|
||||
|
||||
int yPosDiffButton = 900;
|
||||
private int difSelect=0;
|
||||
|
||||
Font textFont2 = new Font("OCR A Extended", Font.BOLD, 50);
|
||||
BufferedImage aanwijzers = Images.getImage(ImageType.aanwijzers);
|
||||
int index = 0;
|
||||
|
||||
@@ -55,6 +56,8 @@ public class MenuState extends GameState {
|
||||
buttons.add(new MenuButton(-600, 350, 1.7, 30, Color.yellow,selectedToSong(selected+1)));
|
||||
buttons.add(new MenuButton(-600, 450, 1.7, 30, Color.WHITE,selectedToSong(selected+2)));
|
||||
buttons.get(2).setSelected(true);
|
||||
generateMainScreenBackground();
|
||||
|
||||
|
||||
}
|
||||
@Override
|
||||
@@ -99,47 +102,11 @@ public class MenuState extends GameState {
|
||||
|
||||
@Override
|
||||
public void draw(Graphics2D g2) {
|
||||
|
||||
|
||||
g2.setColor(Color.BLACK);
|
||||
Font textFont2 = new Font("OCR A Extended", Font.BOLD, 50);
|
||||
g2.setFont(textFont2);
|
||||
|
||||
|
||||
if(!subscreen) {
|
||||
GradientPaint gp = new GradientPaint(0, 0, new Color(0,0,1, 0.6f),1280,1024 ,new Color(0,0,1, 0.2f));
|
||||
g2.setPaint(gp);
|
||||
g2.fillRect(0, 0, 1280, 1024);
|
||||
|
||||
|
||||
|
||||
triangle2 = new Polygon();
|
||||
triangle2.addPoint(0, 0);
|
||||
triangle2.addPoint(0, 1024/4+50);
|
||||
triangle2.addPoint(1280/2+50, 0);
|
||||
|
||||
|
||||
|
||||
triangle = new Polygon();
|
||||
triangle.addPoint(0, 0);
|
||||
triangle.addPoint(0, 1024/4);
|
||||
triangle.addPoint(1280/2, 0);
|
||||
|
||||
|
||||
g2.drawImage(mainScreenBackground, 0, 0, 1280,1024,null);
|
||||
for(MenuButton b:buttons){
|
||||
b.draw(g2);
|
||||
}
|
||||
|
||||
|
||||
GradientPaint gp2 = new GradientPaint(0, 0, new Color(1,1,0, 0.6f),1280,1024 ,new Color(0,0,1, 0.2f));
|
||||
g2.setPaint(gp2);
|
||||
g2.fillPolygon(triangle2);
|
||||
|
||||
g2.setColor(Color.ORANGE);
|
||||
g2.fillPolygon(triangle);
|
||||
|
||||
g2.setColor(Color.BLACK);
|
||||
g2.drawString("Main Menu", 30, 60);
|
||||
}
|
||||
|
||||
if(subscreen) {
|
||||
@@ -164,14 +131,11 @@ public class MenuState extends GameState {
|
||||
for(DifficultyButton b : buttons2){
|
||||
b.draw(g2);
|
||||
}
|
||||
|
||||
|
||||
int y = (index/5)*75;
|
||||
int x = (index%5)*75;
|
||||
index%=25;
|
||||
BufferedImage subImg = aanwijzers.getSubimage(x, y, 75, 75);
|
||||
g2.drawImage(subImg, 825,900 - difSelect*100,75,75,null);
|
||||
|
||||
g2.drawImage(subImg, 825,900 - difSelect*100,75,75,null);
|
||||
}
|
||||
|
||||
|
||||
@@ -208,14 +172,11 @@ public class MenuState extends GameState {
|
||||
if(difSelect < 0){
|
||||
difSelect += buttons2.size();
|
||||
}
|
||||
// System.out.println(difSelect);
|
||||
}else if(e.getJoystick().getPos() == Joystick.Position.UP){
|
||||
difSelect++;
|
||||
if(difSelect > buttons2.size()-1){
|
||||
difSelect = 0;
|
||||
}
|
||||
|
||||
// System.out.println(difSelect);
|
||||
}
|
||||
sh.set(sh.getCurrentSong().getSongs().get(difSelect));
|
||||
|
||||
@@ -233,6 +194,36 @@ public class MenuState extends GameState {
|
||||
}
|
||||
}
|
||||
|
||||
public void generateMainScreenBackground(){
|
||||
mainScreenBackground = new BufferedImage(1280, 1024, BufferedImage.TYPE_4BYTE_ABGR);
|
||||
Graphics2D g2 = mainScreenBackground.createGraphics();
|
||||
RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
g2.setRenderingHints(rh);
|
||||
g2.setFont(textFont2);
|
||||
g2.setPaint(new GradientPaint(0, 0, new Color(0,0,1, 0.6f),1280,1024 ,new Color(0,0,1, 0.2f)));
|
||||
g2.fillRect(0, 0, 1280, 1024);
|
||||
g2.setPaint(new GradientPaint(0, 0, new Color(1,1,0, 0.6f),1280,1024 ,new Color(0,0,1, 0.2f)));
|
||||
|
||||
|
||||
Polygon triangle2 = new Polygon();
|
||||
triangle2.addPoint(0, 0);
|
||||
triangle2.addPoint(0, 1024/4+50);
|
||||
triangle2.addPoint(1280/2+50, 0);
|
||||
g2.fillPolygon(triangle2);
|
||||
|
||||
g2.setColor(Color.ORANGE);
|
||||
Polygon triangle = new Polygon();
|
||||
triangle.addPoint(0, 0);
|
||||
triangle.addPoint(0, 1024/4);
|
||||
triangle.addPoint(1280/2, 0);
|
||||
g2.fillPolygon(triangle);
|
||||
|
||||
g2.setColor(Color.BLACK);
|
||||
g2.drawString("Main Menu", 30, 60);
|
||||
mainScreenBackground.createGraphics();
|
||||
mainScreenBackground = Images.toCompatibleImage(mainScreenBackground);
|
||||
}
|
||||
|
||||
public void buttonInAnimation(int button){
|
||||
if(button >= 0 && button < buttons.size() ){
|
||||
buttons.get(button).setX(buttons.get(button).getX()+40);
|
||||
@@ -269,4 +260,5 @@ public class MenuState extends GameState {
|
||||
return songs.get(s);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -134,7 +134,6 @@ public class PlayState extends GameState {
|
||||
|
||||
@Override
|
||||
public void buttonPressed(ButtonEvent e) {
|
||||
// System.out.println(e.getButton().getColor());
|
||||
Iterator<Enemy> enemysInPath = area.paths.get(player.getIndex())
|
||||
.getEnemysInPath().iterator();
|
||||
while (enemysInPath.hasNext()) {
|
||||
|
||||
@@ -7,30 +7,34 @@ import java.awt.Color;
|
||||
import java.awt.Font;
|
||||
import java.awt.GradientPaint;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
import control.joystick.JoystickEvent;
|
||||
import model.SongHandler;
|
||||
import control.GameStateManager;
|
||||
import control.GameStateManager.State;
|
||||
import control.button.ButtonEvent;
|
||||
import control.joystick.JoystickEvent;
|
||||
|
||||
public class TitleState extends GameState {
|
||||
|
||||
BufferedImage pressStart = Images.getImage(ImageType.pressstart);
|
||||
BufferedImage colorStrike = Images.getImage(ImageType.colorstrike);
|
||||
BufferedImage background = Images.getImage(ImageType.background);
|
||||
|
||||
BufferedImage background;
|
||||
Font textFont = new Font("OCR A Extended", Font.BOLD, 15);
|
||||
Font textFont2 = new Font("OCR A Extended", Font.BOLD, 130);
|
||||
GradientPaint gp = new GradientPaint(300, 0, new Color(0,0,1, 0.6f),980,1024 ,new Color(0,0,1, 0.2f));
|
||||
|
||||
int index = 0;
|
||||
int varx = 0;
|
||||
int frame;
|
||||
|
||||
public TitleState(GameStateManager gsm, SongHandler sh){
|
||||
super(gsm, sh);
|
||||
createBackground();
|
||||
}
|
||||
@Override
|
||||
public void init() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@@ -42,50 +46,9 @@ public class TitleState extends GameState {
|
||||
|
||||
@Override
|
||||
public void draw(Graphics2D g2) {
|
||||
|
||||
|
||||
g2.setColor(new Color(1,1,1, 0.3f));
|
||||
g2.fillRect(0,0,1280,1024);
|
||||
|
||||
g2.setColor(new Color(0,1,0, 0.7f));
|
||||
g2.fillRect(0,0,100,1024);
|
||||
|
||||
g2.setColor(new Color(1,1,0, 0.7f));
|
||||
g2.fillRect(100,0,100,1024);
|
||||
|
||||
g2.setColor(new Color(1,0,0, 0.7f));
|
||||
g2.fillRect(200,0,100,1024);
|
||||
|
||||
g2.setColor(new Color(0,1,0, 0.7f));
|
||||
g2.fillRect(1180,0,100,1024);
|
||||
|
||||
g2.setColor(new Color(1,1,0, 0.7f));
|
||||
g2.fillRect(1080,0,100,1024);
|
||||
|
||||
g2.setColor(new Color(1,0,0, 0.7f));
|
||||
g2.fillRect(980,0,100,1024);
|
||||
|
||||
GradientPaint gp = new GradientPaint(300, 0, new Color(0,0,1, 0.6f),980,1024 ,new Color(0,0,1, 0.2f));
|
||||
g2.setPaint(gp);
|
||||
g2.fillRect(300, 0, 680, 1024);
|
||||
|
||||
g2.translate(640, 512);
|
||||
|
||||
g2.drawImage(background, 0, 0, 1280, 1024, null);
|
||||
int image_x = ((frame / 6) % 6) * 49;
|
||||
BufferedImage subImg = pressStart.getSubimage(image_x, 0, 49, 26);
|
||||
g2.drawImage(subImg, - 25*5, 200, 49*5, 26*5, null);
|
||||
|
||||
Font textFont = new Font("OCR A Extended", Font.BOLD, 15);
|
||||
g2.setFont(textFont);
|
||||
g2.setColor(Color.WHITE);
|
||||
g2.drawString("�2015 Team Hamtaro", - 18*5, 500);
|
||||
|
||||
g2.setColor(Color.RED);
|
||||
Font textFont2 = new Font("OCR A Extended", Font.BOLD, 130);
|
||||
g2.setFont(textFont2);
|
||||
g2.drawString("Color", -215, -300);
|
||||
g2.drawString("Strike", -250, -170);
|
||||
|
||||
g2.drawImage(pressStart.getSubimage(image_x, 0, 49, 26), 640-122, 512, 245, 130, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -93,7 +56,6 @@ public class TitleState extends GameState {
|
||||
|
||||
switch(e.getButton().getButtonID()){
|
||||
case 0:
|
||||
//gsm.next();
|
||||
gsm.setState(State.MENU_STATE);
|
||||
break;
|
||||
}
|
||||
@@ -106,8 +68,41 @@ public class TitleState extends GameState {
|
||||
}
|
||||
@Override
|
||||
public void onJoystickMoved(JoystickEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
}
|
||||
|
||||
public void createBackground(){
|
||||
background = new BufferedImage(1280, 1024, BufferedImage.TYPE_4BYTE_ABGR);
|
||||
Graphics2D g2 = background.createGraphics();
|
||||
RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
g2.setRenderingHints(rh);
|
||||
g2.setColor(new Color(1,1,1, 0.3f));
|
||||
g2.fillRect(0,0,1280,1024);
|
||||
|
||||
g2.setColor(new Color(0,1,0, 0.7f));
|
||||
g2.fillRect(0,0,100,1024);
|
||||
g2.fillRect(1180,0,100,1024);
|
||||
|
||||
g2.setColor(new Color(1,1,0, 0.7f));
|
||||
g2.fillRect(100,0,100,1024);
|
||||
g2.fillRect(1080,0,100,1024);
|
||||
|
||||
g2.setColor(new Color(1,0,0, 0.7f));
|
||||
g2.fillRect(200,0,100,1024);
|
||||
g2.fillRect(980,0,100,1024);
|
||||
|
||||
g2.setPaint(gp);
|
||||
g2.fillRect(300, 0, 680, 1024);
|
||||
|
||||
g2.setFont(textFont);
|
||||
g2.setColor(Color.WHITE);
|
||||
g2.drawString("©2015 Team Hamtaro", 550, 1012);
|
||||
|
||||
g2.setColor(Color.RED);
|
||||
g2.setFont(textFont2);
|
||||
g2.drawString("Color", 385, 212);
|
||||
g2.drawString("Strike", 390, 342);
|
||||
background.createGraphics();
|
||||
background = Images.toCompatibleImage(background);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
package model.objects;
|
||||
|
||||
import image.Images;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Font;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
import model.gameState.PlayState;
|
||||
|
||||
@@ -12,10 +16,12 @@ public class InfoPanel {
|
||||
private int lifePercent;
|
||||
private int upgradeScore = 0;
|
||||
private int x, y;
|
||||
private BufferedImage infoPanel;
|
||||
|
||||
public InfoPanel(int x, int y){
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
generateInfoPanel();
|
||||
updateIPanel();
|
||||
}
|
||||
|
||||
@@ -26,14 +32,19 @@ public class InfoPanel {
|
||||
lifePercent =+ PlayState.lifePoints;
|
||||
|
||||
if(0 <= PlayState.currentScore && PlayState.currentScore <=100) {
|
||||
upgradeScore = PlayState.currentScore;
|
||||
}
|
||||
|
||||
if(PlayState.currentScore == 100) {
|
||||
if(upgradeScore != PlayState.currentScore){
|
||||
upgradeScore = PlayState.currentScore;
|
||||
generateInfoPanel();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g2){
|
||||
private void generateInfoPanel(){
|
||||
infoPanel = new BufferedImage(1280, 1024, BufferedImage.TYPE_INT_ARGB);
|
||||
Graphics2D g2 = infoPanel.createGraphics();
|
||||
RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
g2.setRenderingHints(rh);
|
||||
g2.setColor(Color.BLACK);
|
||||
g2.fillRect(x, y, 256, 1024);
|
||||
Font scoreFont = new Font("OCR A Extended", Font.BOLD, 30);
|
||||
g2.setFont(scoreFont);
|
||||
@@ -46,11 +57,11 @@ public class InfoPanel {
|
||||
|
||||
g2.setColor(Color.YELLOW);
|
||||
g2.fillRect(25, 1000 - 7 * upgradeScore, 200, 0 + 7 * upgradeScore);
|
||||
|
||||
g2.setColor(Color.BLACK);
|
||||
infoPanel.createGraphics();
|
||||
infoPanel = Images.toCompatibleImage(infoPanel);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void draw(Graphics2D g2){
|
||||
g2.drawImage(infoPanel, 0, 0, 1280,1024,null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,23 +6,22 @@ import java.awt.Font;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.LinearGradientPaint;
|
||||
import java.awt.Paint;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.geom.GeneralPath;
|
||||
import java.awt.geom.Point2D;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import audio.Song;
|
||||
|
||||
public class MenuButton {
|
||||
|
||||
private ArrayList<GeneralPath> buttonparts;
|
||||
private GeneralPath border, line;
|
||||
private ArrayList<Color> colors;
|
||||
private Paint gradient;
|
||||
private int x, y, rounding;
|
||||
private double scalefactor;
|
||||
private Color buttoncolor;
|
||||
|
||||
private boolean selected;
|
||||
private int fadecounter;
|
||||
private BufferedImage background;
|
||||
|
||||
private Song song;
|
||||
|
||||
@@ -31,61 +30,45 @@ public class MenuButton {
|
||||
this.y = y;
|
||||
this.scalefactor = scale;
|
||||
this.rounding = rounding;
|
||||
calculateButton();
|
||||
this.buttoncolor = c0;
|
||||
setSong(song);
|
||||
|
||||
colors = new ArrayList<Color>();
|
||||
|
||||
Color c1 = c0.darker();
|
||||
Color c2 = c0.darker().darker();
|
||||
Color c3 = c0.darker().darker().darker();
|
||||
calculateButton();
|
||||
}
|
||||
|
||||
public void calculateButton(){
|
||||
Color c1 = buttoncolor.darker();
|
||||
Color c2 = buttoncolor.darker().darker();
|
||||
Color c3 = buttoncolor.darker().darker().darker();
|
||||
|
||||
c1 = new Color((int)(c1.getRed()*0.8), (int)(c1.getGreen()*0.8),(int)(c1.getBlue()*0.8));
|
||||
c2 = new Color((int)(c2.getRed()*0.8), (int)(c2.getGreen()*0.8),(int)(c2.getBlue()*0.8));
|
||||
c3 = new Color((int)(c3.getRed()*0.8), (int)(c3.getGreen()*0.8),(int)(c3.getBlue()*0.8));
|
||||
|
||||
colors.add(c2);
|
||||
colors.add(c3);
|
||||
colors.add(c2);
|
||||
colors.add(c0);
|
||||
colors.add(c1);
|
||||
colors.add(c0);
|
||||
|
||||
gradient = new LinearGradientPaint(
|
||||
new Point2D.Double((-100)*scalefactor,(512)*scalefactor),
|
||||
new Point2D.Double((600)*scalefactor,(512)*scalefactor),
|
||||
new float[]{0.0f, 1.0f},
|
||||
new Color[]{new Color(c1.getRed(), c1.getGreen(), c1.getBlue(), 10), c0});
|
||||
}
|
||||
|
||||
public void calculateButton(){
|
||||
buttonparts = new ArrayList<GeneralPath>();
|
||||
buttonparts.add(arrayToGeneralpath(new int[][]{{449, 1}, {449, 68},{113, 103}, {106, 35}})); //background right
|
||||
buttonparts.add(arrayToGeneralpath(new int[][]{{113, 103}, {106, 35},{70, 12+rounding}, {77, 88+rounding}})); //background middle
|
||||
buttonparts.add(arrayToGeneralpath(new int[][]{{70, 12+rounding}, {77, 88+rounding},{4, 104+(int)(rounding*0.5)}, {-4, 32+(int)(rounding*0.5)}})); //background left
|
||||
buttonparts.add(arrayToGeneralpath(new int[][]{{449, 15}, {449, 54},{118, 88}, {114, 48}})); //foreground right
|
||||
buttonparts.add(arrayToGeneralpath(new int[][]{{118, 88}, {114, 48},{78, 25+rounding}, {82, 73+rounding}})); //foreground middle
|
||||
buttonparts.add(arrayToGeneralpath(new int[][]{{78, 25+rounding}, {82, 73+rounding},{15, 88+(int)(rounding*0.5)}, {10, 43+(int)(rounding*0.5)}})); //foreground left
|
||||
|
||||
border = arrayToGeneralpath(new int[][]{{449,15}, {114,48},{78,25+rounding},{10,43+(int)(rounding*0.5)},{15,88+(int)(rounding*0.5)},{82,73+rounding},{118,88},{449,54}});
|
||||
line = arrayToGeneralpath(new int[][]{{-11,55},{-40,60},{-38,78},{-358,170-rounding*3},{-358,174-rounding*3},{-37,87},{-35,102},{-5,95}});
|
||||
}
|
||||
|
||||
public GeneralPath arrayToGeneralpath(int block[][]){
|
||||
GeneralPath polyline = new GeneralPath(GeneralPath.WIND_EVEN_ODD, block.length);
|
||||
polyline.moveTo ((block[0][0]+x)*scalefactor, (block[0][1]+y)*scalefactor);
|
||||
for (int index = 1; index < block.length; index++) {
|
||||
polyline.lineTo((block[index][0]+x)*scalefactor, (block[index][1]+y)*scalefactor);
|
||||
};
|
||||
polyline.closePath();
|
||||
return polyline;
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g2d){
|
||||
for(int i = 0; i < buttonparts.size(); i++){ //fill every part of the button with a specific color
|
||||
g2d.setColor(colors.get(i));
|
||||
g2d.fill((buttonparts.get(i)));
|
||||
}
|
||||
GeneralPath border = arrayToGeneralpath(new int[][]{{449,15}, {114,48},{78,25+rounding},{10,43+(int)(rounding*0.5)},{15,88+(int)(rounding*0.5)},{82,73+rounding},{118,88},{449,54}});
|
||||
GeneralPath line = arrayToGeneralpath(new int[][]{{-11,55},{-40,60},{-38,78},{-358,170-rounding*3},{-358,174-rounding*3},{-37,87},{-35,102},{-5,95}});
|
||||
Paint gradient = new LinearGradientPaint( new Point2D.Double((-100)*scalefactor,(512)*scalefactor),
|
||||
new Point2D.Double((600)*scalefactor,(512)*scalefactor),
|
||||
new float[]{0.0f, 1.0f},
|
||||
new Color[]{new Color(c1.getRed(), c1.getGreen(), c1.getBlue(), 10), buttoncolor});
|
||||
//Draw the generated button to the buffered image
|
||||
background = new BufferedImage(1280, 1024, BufferedImage.TYPE_4BYTE_ABGR);
|
||||
Graphics2D g2d = background.createGraphics();
|
||||
RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
g2d.setRenderingHints(rh);
|
||||
|
||||
g2d.setColor(c2);
|
||||
g2d.fill(arrayToGeneralpath(new int[][]{{449, 1}, {449, 68},{113, 103}, {106, 35}}));
|
||||
g2d.setColor(c3);
|
||||
g2d.fill(arrayToGeneralpath(new int[][]{{113, 103}, {106, 35},{70, 12+rounding}, {77, 88+rounding}}));
|
||||
g2d.setColor(c2);
|
||||
g2d.fill(arrayToGeneralpath(new int[][]{{70, 12+rounding}, {77, 88+rounding},{4, 104+(int)(rounding*0.5)}, {-4, 32+(int)(rounding*0.5)}}));
|
||||
g2d.setColor(buttoncolor);
|
||||
g2d.fill(arrayToGeneralpath(new int[][]{{449, 15}, {449, 54},{118, 88}, {114, 48}}));
|
||||
g2d.setColor(c1);
|
||||
g2d.fill(arrayToGeneralpath(new int[][]{{118, 88}, {114, 48},{78, 25+rounding}, {82, 73+rounding}}));
|
||||
g2d.setColor(buttoncolor);
|
||||
g2d.fill(arrayToGeneralpath(new int[][]{{78, 25+rounding}, {82, 73+rounding},{15, 88+(int)(rounding*0.5)}, {10, 43+(int)(rounding*0.5)}}));
|
||||
|
||||
if(selected){
|
||||
g2d.setStroke(new BasicStroke(4));
|
||||
@@ -94,36 +77,40 @@ public class MenuButton {
|
||||
}
|
||||
g2d.setPaint(gradient);
|
||||
g2d.fill(line);
|
||||
|
||||
|
||||
//draw text
|
||||
g2d.setColor(Color.BLACK);
|
||||
Font textFont = new Font("OCR A Extended", Font.BOLD, (int)(30*scalefactor));
|
||||
g2d.setFont(textFont);
|
||||
g2d.translate((x+160)*scalefactor, (y+74)*scalefactor);
|
||||
g2d.setFont(new Font("OCR A Extended", Font.BOLD, (int)(30*scalefactor)));
|
||||
g2d.translate((160+358)*scalefactor, (74)*scalefactor);
|
||||
g2d.rotate(-0.1);
|
||||
g2d.drawString(song.getTitle(), 0, 0);
|
||||
g2d.rotate(0.1);
|
||||
g2d.translate(-(x+160)*scalefactor, -(y+74)*scalefactor);
|
||||
|
||||
background.createGraphics();
|
||||
}
|
||||
|
||||
public GeneralPath arrayToGeneralpath(int block[][]){
|
||||
GeneralPath polyline = new GeneralPath(GeneralPath.WIND_EVEN_ODD, block.length);
|
||||
polyline.moveTo ((block[0][0]+358)*scalefactor, (block[0][1])*scalefactor);
|
||||
for (int index = 1; index < block.length; index++) {
|
||||
polyline.lineTo((block[index][0]+358)*scalefactor, (block[index][1])*scalefactor);
|
||||
};
|
||||
polyline.closePath();
|
||||
return polyline;
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g2d){
|
||||
g2d.drawImage(background, (int)((x-320)*scalefactor), (int) ((y)*scalefactor), 1280, 1024, null);
|
||||
}
|
||||
|
||||
public void update(){
|
||||
if(selected && fadecounter < 5){
|
||||
x += 8;
|
||||
y -= 8;
|
||||
scalefactor += 0.04;
|
||||
fadecounter++;
|
||||
calculateButton();
|
||||
}else if(!selected && fadecounter >0){
|
||||
x += 8;
|
||||
y += 8;
|
||||
fadecounter--;
|
||||
scalefactor -= 0.04;
|
||||
calculateButton();
|
||||
}
|
||||
}
|
||||
|
||||
public void setSelected(boolean selected) {
|
||||
this.selected = selected;
|
||||
x += 100;
|
||||
y -= 40;
|
||||
scalefactor += 0.2;
|
||||
calculateButton();
|
||||
}
|
||||
|
||||
public boolean isSelected(){
|
||||
@@ -131,7 +118,6 @@ public class MenuButton {
|
||||
}
|
||||
public void setX(int x){
|
||||
this.x = x;
|
||||
calculateButton();
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
package model.objects;
|
||||
|
||||
import image.Images;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Polygon;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.geom.Line2D;
|
||||
import java.awt.geom.Point2D;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -16,6 +21,7 @@ public class PlayArea {
|
||||
public List<Path> paths;
|
||||
private Polygon octagon,hitArea;
|
||||
public Rectangle2D[] hitAreas; //de area voor elk path die de enemy moet raken
|
||||
private BufferedImage background;
|
||||
|
||||
public PlayArea(double xToRight,double heightOfGameScreen,double widthOfGameScreen, int sizeOctagon) {
|
||||
super();
|
||||
@@ -90,22 +96,26 @@ public class PlayArea {
|
||||
paths.add(new Path(hitArea.xpoints[4] - Enemy.distanceToOctagon,hitArea.ypoints[4] ,hitArea.xpoints[4],hitArea.ypoints[4]));//left
|
||||
paths.add(new Path(hitArea.xpoints[5] - angleX,hitArea.ypoints[5] - angleY ,hitArea.xpoints[5],hitArea.ypoints[5]));//left -top
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g2){
|
||||
Line2D current;
|
||||
background = new BufferedImage(1280, 1024, BufferedImage.TYPE_INT_ARGB);
|
||||
Graphics2D g2 = background.createGraphics();
|
||||
RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
g2.setRenderingHints(rh);
|
||||
Line2D current;
|
||||
int index;
|
||||
g2.setColor(Color.BLACK);
|
||||
for(int i = 0; i < paths.size(); i++){
|
||||
current = paths.get(i);
|
||||
index = (i+14)%8;
|
||||
g2.drawLine((int)current.getX1(), (int)current.getY1(), octagon.xpoints[index],octagon.ypoints[index]);
|
||||
}
|
||||
g2.draw(octagon);
|
||||
g2.draw(hitArea);
|
||||
// for(Rectangle2D hit : hitAreas){
|
||||
// g2.fill(hit);
|
||||
// }
|
||||
g2.draw(hitArea);
|
||||
background.createGraphics();
|
||||
background = Images.toCompatibleImage(background);
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g2){
|
||||
g2.drawImage(background, 0, 0, 1280, 1024, null);
|
||||
}
|
||||
|
||||
public Line2D getLine(int index){
|
||||
|
||||
+8
-4
@@ -1,15 +1,14 @@
|
||||
package view;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Font;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.Timer;
|
||||
|
||||
import control.GameStateManager;
|
||||
import control.LedHandler;
|
||||
@@ -24,12 +23,13 @@ public class GameView extends JPanel{
|
||||
|
||||
LedHandler led;
|
||||
GameStateManager gsm;
|
||||
Font fpsfont = new Font("OCR A Extended", Font.BOLD, 60);
|
||||
|
||||
public GameView(LedHandler led,GameStateManager gsm)
|
||||
{
|
||||
this.led=led;
|
||||
this.gsm = gsm;
|
||||
|
||||
this.
|
||||
setPreferredSize(new Dimension(1280,1024));
|
||||
}
|
||||
|
||||
@@ -44,6 +44,10 @@ public class GameView extends JPanel{
|
||||
RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
g2d.setRenderingHints(rh);
|
||||
gsm.currentState.draw(g2d);
|
||||
|
||||
g2d.setColor(Color.RED);
|
||||
g2d.setFont(fpsfont);
|
||||
g2d.drawString(gsm.fps + "fps", 1000, 40);
|
||||
Toolkit.getDefaultToolkit().sync();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user