Optimized code, everything is buffered in a buffered image now

This commit is contained in:
jancoow
2015-06-07 14:22:56 +02:00
parent 396769317c
commit 750f525c2d
15 changed files with 237 additions and 233 deletions
+1 -1
View File
@@ -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);
+38 -46
View File
@@ -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);
}
}
}
-1
View File
@@ -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()) {
+44 -49
View File
@@ -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);
}
}