Basically what I want to achieve is to make the triangle I have (Obviously create a class to create the triangle), is to shoot a bullet, if you want to see a better perspective more or less like the game of "Asteroids "
public class NaveClass extends JPanel implements KeyListener{
private AffineTransform tr,tr1,tr1bala;
private Shape t,b;
private Shape[] balas,balas2;
private Timer tim;
private double[] n;
private double[] m;
private double a=0;
private double c=0;
private boolean bV=false,der=false,izq=false;
private AffineTransform[] tr2,tr3;
public NaveClass(){
this.addKeyListener(this);
this.setFocusable(true);
tr = new AffineTransform();
tr2= new AffineTransform[10];
tr3= new AffineTransform[10];
tr1=new AffineTransform();
tr1bala = new AffineTransform();
balas= new Shape[10];
t= new Triangulo(200,200,100,100);
b= new Ellipse2D.Double(t.getBounds2D().getCenterX()-5,t.getBounds2D().getCenterY()-50,10,10);
for(int i=0;i<10;i++){
balas[i]= new Ellipse2D.Double(t.getBounds2D().getCenterX()-5,t.getBounds2D().getCenterY()-50,10,10);
}
tim = new Timer(10, new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
if(bV){
b= tr1.createTransformedShape(b);
tr1.setToTranslation(0, -1);
}
repaint();
}
});
tim.start();
this.setBackground(Color.gray);
this.setPreferredSize(new Dimension(500, 500));
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.fill(t);
g2.fill(b);
}
@Override
public void keyTyped(KeyEvent ke) {
}
@Override
public void keyPressed(KeyEvent ke) {
if(ke.getKeyCode()== KeyEvent.VK_RIGHT){
tr.setToRotation(Math.toRadians(5),250,250);
t = tr.createTransformedShape(t);
tr1bala.setToRotation(Math.toRadians(a+5),250,250);
b = tr1bala.createTransformedShape(b);
}
if(ke.getKeyCode()== KeyEvent.VK_LEFT){
tr.setToRotation(Math.toRadians(-5),250,250);
t = tr.createTransformedShape(t);
tr1bala.setToRotation(Math.toRadians(a-5),250,250);
b = tr1bala.createTransformedShape(b);
}
repaint();
if(ke.getKeyCode()==KeyEvent.VK_SPACE){
bV=true;
}
}
@Override
public void keyReleased(KeyEvent ke) {
}}
If you have doubts about my triangle that believes in another class, it is.
public class Triangulo implements Shape {
private GeneralPath ruta;
public Triangulo(double x, double y, double w, double h){
ruta=new GeneralPath();
ruta.moveTo(x+0.5*w, y);
ruta.lineTo(x+w, y+h);
ruta.lineTo(x, y+h);
ruta.clone();
}
@Override
public Rectangle getBounds() {
return ruta.getBounds();
}
@Override
public Rectangle2D getBounds2D() {
return ruta.getBounds2D();
}
@Override
public boolean contains(double d, double d1) {
return ruta.contains(d, d1);
}
@Override
public boolean contains(Point2D pd) {
return ruta.contains(pd);
}
@Override
public boolean intersects(double d, double d1, double d2, double d3) {
return ruta.intersects(d, d1, d2, d3);
}
@Override
public boolean intersects(Rectangle2D rd) {
return ruta.intersects(rd);
}
@Override
public boolean contains(double d, double d1, double d2, double d3) {
return ruta.contains(d, d1);
}
@Override
public boolean contains(Rectangle2D rd) {
return ruta.contains(rd);
}
@Override
public PathIterator getPathIterator(AffineTransform at) {
return ruta.getPathIterator(at);
}
@Override
public PathIterator getPathIterator(AffineTransform at, double d) {
return ruta.getPathIterator(at);
}}