Java spaceshooter design
Hey all,
i've been making a spaceshooter in java, but now i'm having design problems. my code is apparantly inefficient and lags like crazy on certain comps, but works fine on good comps. i've experimented with threading, but dont know an efficient way to thread. So far i have a thread to update drawing, a thread to updte movement, and a thread to check 4 all collisions. i also have a bunch of random threads that certain objects create, but thats not that big of a deal. alsoi'm using particle effects to create a shot, but thats the one thats causing the greatest lag as it hsato loop through to draw each tiny piece. Anyway i'm confused right now on how to make an efficient design so i'm asking for ideas and help on how to create a design. plz help all u can and plz dont flame me =(
[811 byte] By [
Astrofa] at [2007-10-3 10:57:13]

hmmm aiight. my code is somewhat confusing...wud anyone mind telling me how they wud do it? neway here's my code:
//code from SimpleEnemy.java
EnemyShotPiece p=new EnemyShotPiece((int)Math.round(x), (int)Math.round(y));
pieces.add(p); //pieces is a vector of EnemyShotPiece
int length=pieces.size();
for(int i=0; i<length; i++)
{
if(pieces.get(i).radius()><=0)
{
pieces.remove(i);
i-=1;
}
length=pieces.size();
}
//out.println((" "+pieces.size()));
for(EnemyShotPiece ep:pieces)
{
ep.move();
ep.draw(g);
}
public class EnemyShotPiece extends MovingObject
{
double radius;
int step=0;
public EnemyShotPiece(int row, int col)
{
super(row, col);
//this.direction=direction;
//this.speed=speed;
//out.println(direction*(180.0/Math.PI));
radius=10;
//player=p;
}
public void draw(Graphics g)
{
//move();
//out.println(row.get()+" "+col.get()+" "+radius);
//g.drawArc(row.get(), col.get(), (int)Math.round(radius), (int)Math.round(radius), 0, 360);
Graphics2D gg=(Graphics2D)g;
//Color coll=gg.getColor();
//gg.setColor(new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256)));
Paint p=gg.getPaint();
final GradientPaint gp=new GradientPaint(3, 3, Color.orange, width()/2.0f, height()/2.0f, Color.red, true);
gg.setPaint(gp);
Ellipse2D e=new Ellipse2D.Double(row.get()+radius/2, col.get(), radius, radius);
gg.fill(e);
//gg.setPaint(Color.green);
//gg.setStroke(new BasicStroke());
//gg.draw(e);
//gg.setPaint(gp);
gg.draw(e);
//e.setFrame()
//gg.setColor(Color.ORANGE);
//gg.drawOval((int)Math.round(row.get()+radius/2), (int)Math.round(col.get()), (int)Math.ceil(radius), (int)Math.ceil(radius));
//gg.fillOval((int)Math.round(row.get()+radius/2), (int)Math.round(col.get()), (int)Math.ceil(radius), (int)Math.ceil(radius));
//gg.setColor(coll);
gg.setPaint(p);
//g.fillOval(row.get(), col.get(), (int)Math.round(radius), (int)Math.round(radius));
}
public void setImage()
{
}
public int radius()
{
return (int)Math.round(radius);
}
public void move() //object doesnt move itself, only radius gets decremented
{
radius-=.5;
}
}
sorry if the code is knda funky, i had a lot of test statements in there.
Ok, calls to "new
" are extremely expensive. You should try to avoid them as much as possible in methods that are being called many many times in your program.
The draw(Graphics g)
method in your EnemyShotPiece class makes two new objects.
Lets try to remove both of those.
1. GradientPaint- You have to consider if you want to sacrifice the speed of your program, just for the sake of making it a little prettier. If I were you, I would not do a gradient OR i would make the gradient a static final variable. This way, you would only have to create it once.
2. Ellipse2D- Can't you just use g.drawOval(x,y,width,height)
?
Tell me how it goes.
woah turns out my method was horribly off.....i used an eclipse because i saw on an online gradient tut that it used eclipse....but i didnt know u cud just fill oval and it wud do the same thing.....
here's my simplified code:
public void draw(Graphics g)
{
(int)Math.round(radius), 0, 360);
Graphics2D gg=(Graphics2D)g;
Paint p=gg.getPaint();
gg.setPaint(gp);
gg.drawOval((int)Math.round(row.get()+radius/2), (int)Math.round(col.get()), (int)Math.ceil(radius), (int)Math.ceil(radius));
gg.fillOval((int)Math.round(row.get()+radius/2), (int)Math.round(col.get()), (int)Math.ceil(radius), (int)Math.ceil(radius));
}