use static images to speed up rendering

master
shark 6 years ago
parent caa061955d
commit 643e49df2e

@ -9,27 +9,74 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Scanner; import java.util.Scanner;
/* /*
* File Name: Cell.java * File Name: Cell.java
* Created: Nov 7, 2018 * Created: Nov 7, 2018
* Author: * Author:
*/ */
public class Cell extends Object public class Cell extends Object
{ {
private int xval; private int xval;
private int yval; private int yval;
private Color myColor;
private ArrayList<Integer> stages = new ArrayList<Integer>(); private ArrayList<Integer> stages = new ArrayList<Integer>();
private int currentStage; private int currentStage;
private int currentTime; private int currentTime;
public Cell(int x, int y, Color C) private static BufferedImage[] p1 = new BufferedImage[12];
private static final BufferedImage p2;
private static final BufferedImage p3;
private static final BufferedImage p4;
static
{
for (int count = 0; count < p1.length; count++)
{
BufferedImage g1 = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);
Graphics gg1 = g1.getGraphics();
gg1.setColor(new Color(255, 0, 0, 127));
int width;
int pos;
width = (int) ((count + 1) * 2 + 22.5);
pos = (50 - width) / 2;
gg1.fillOval(pos, pos, width, width);
gg1.setColor(Color.BLACK);
gg1.drawOval(pos, pos, width, width);
gg1.drawString("G1", 18, 30);
p1[count] = g1;
}
BufferedImage s = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);
Graphics gs = s.getGraphics();
gs.setColor(new Color(255, 255, 0, 127));
gs.fillOval(0, 0, 50, 50);
gs.setColor(Color.BLACK);
gs.drawOval(0, 0, 50, 50);
gs.drawString("S", 22, 30);
p2 = s;
BufferedImage g2 = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);
Graphics gg2 = g2.getGraphics();
gg2.setColor(new Color(0, 255, 0, 127));
gg2.fillOval(0, 0, 50, 50);
gg2.setColor(Color.BLACK);
gg2.drawOval(0, 0, 50, 50);
gg2.drawString("G2", 18, 30);
p3 = g2;
BufferedImage m = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);
Graphics gm = m.getGraphics();
gm.setColor(new Color(0, 0, 255, 127));
gm.fillOval(0, 0, 50, 50);
gm.setColor(Color.BLACK);
gm.drawOval(0, 0, 50, 50);
gm.drawString("M", 22, 30);
p4 = m;
}
public Cell(int x, int y)
{ {
xval = x; xval = x;
yval = y; yval = y;
myColor = C;
stages.add(10 + (int) (Math.random() * 3 - 1)); stages.add(10 + (int) (Math.random() * 3 - 1));
stages.add(6 + (int) (Math.random() * 3 - 1)); stages.add(6 + (int) (Math.random() * 3 - 1));
stages.add(3 + (int) (Math.random() * 3 - 1)); stages.add(3 + (int) (Math.random() * 3 - 1));
@ -37,11 +84,6 @@ public class Cell extends Object
currentTime = 0; currentTime = 0;
currentStage = 0; currentStage = 0;
} }
@Override
public String toString()
{
return "(" + xval + ", " + yval + ")";
}
public int getX() public int getX()
{ {
@ -53,87 +95,70 @@ public class Cell extends Object
return yval; return yval;
} }
public Color getColor() /*public Color getColor()
{ {
return myColor; return myColor;
} }*/
public Cell performTime()
public int moveTo(int x, int y)
{ {
xval = x; this.currentTime++;
yval = y;
return 0;
}
public Cell performTime() {
this.currentTime ++;
Cell newcell = this; Cell newcell = this;
//System.out.println("Increased Current Time to:" + this.currentTime); //System.out.println("Increased Current Time to:" + this.currentTime);
if(this.currentTime > stages.get(this.currentStage)) { if (this.currentTime > stages.get(this.currentStage))
if(currentStage == 3) { {
if (currentStage == 3)
{
// SPLIT // SPLIT
int width = (int) (-50 + Math.random()*101); int width = (int) (-50 + Math.random() * 101);
int height = (int) (-50 + Math.random()*101); int height = (int) (-50 + Math.random() * 101);
while (width < 20 && width > -20) width = (int) (-50 + Math.random()*101); while (width < 20 && width > -20)
while (height < 20 && height > -20) height = (int) (-50 + Math.random()*101); {
newcell = new Cell(xval + width, yval + height, new Color(255, 0, 0)); width = (int) (-50 + Math.random() * 101);
}
while (height < 20 && height > -20)
{
height = (int) (-50 + Math.random() * 101);
}
newcell = new Cell(xval + width, yval + height);
//stages = new ArrayList<Integer>(); //stages = new ArrayList<Integer>();
//stages.add(10 + (int) Math.random() * 3 - 1); //stages.add(10 + (int) Math.random() * 3 - 1);
//stages.add(7 + (int) Math.random() * 3 - 1); //stages.add(7 + (int) Math.random() * 3 - 1);
//stages.add(3 + (int) Math.random() * 3 - 1); //stages.add(3 + (int) Math.random() * 3 - 1);
//stages.add(1 + (int) Math.random() * 2); //stages.add(1 + (int) Math.random() * 2);
xval -= width; xval -= width;
yval -= height; yval -= height;
currentStage = 0; currentStage = 0;
currentTime = 1; currentTime = 1;
myColor = new Color(255, 0, 0);
} }
else { else
this.currentStage ++; {
this.currentTime = 0; this.currentStage++;
if(currentStage == 1) myColor = new Color(255, 255, 0); this.currentTime = 0;
if(currentStage == 2) myColor = new Color(0, 255, 0); //if(currentStage == 1) myColor = new Color(255, 255, 0, 100);
if(currentStage == 3) myColor = new Color(0, 0, 255); //if(currentStage == 2) myColor = new Color(0, 255, 0, 100);
//if(currentStage == 3) myColor = new Color(0, 0, 255, 100);
} }
} }
return newcell; return newcell;
} }
public Image getImage() public Image getImage()
{ {
Image canvas = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB); if (currentStage == 0)
Graphics g = canvas.getGraphics(); {
return p1[currentTime];
g.setColor(this.getColor());
int width = 50;
int pos = 0;
if (currentStage == 0) {
width = (int) ((currentTime + 1) * 2 + 22.5);
pos = (50 - width) / 2;
} }
g.fillOval(pos, pos, width, width); if (currentStage == 1)
g.setColor(Color.BLACK); {
g.drawOval(pos, pos, width, width); return p2;
StringBuilder x = new StringBuilder(""); }
if(currentStage == 0) x.append("G1"); if (currentStage == 2)
if(currentStage == 1) x.append("S"); {
if(currentStage == 2) x.append("G2"); return p3;
if(currentStage == 3) x.append("M"); }
g.drawString(x.toString(), 20, 30); return p4;
return canvas;
} }
public Cell clone(int x, int y)
{
Cell out = new Cell(x, y, getColor());
return out;
}
public Cell combine(Cell in) {
int r = (this.getColor().getRed() + in.getColor().getRed()) / 2;
int g = (this.getColor().getGreen() + in.getColor().getGreen()) / 2;
int b = (this.getColor().getBlue() + in.getColor().getBlue()) / 2;
Cell out = new Cell((in.getX() + this.getX())/2, (in.getY() + this.getY())/2, new Color(r, g, b));
return out;
}
} }

Loading…
Cancel
Save