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
+21 -10
View File
@@ -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);
}
}
+59 -73
View File
@@ -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() {
+19 -9
View File
@@ -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){