0

I'm making a code where I use a image and move it around the screen, so far i can make it move on all 4 axis and also rotate it, but i need to make it move to where is pointing once it rotate. I'm pretty amateur about coding in java, so far i found a way to make de code work, but i dont know how to implement the code to make it move to where it's pointing

So far this is what i got, this is de class that put de panel for de code

import javax.swing.JFrame;
import javax.swing.JPanel;

public class prueba {
    public static void main(String[] arg) {
        Game panel = new Game();
        panel.x=-100;
        panel.y=-100;
        
        
        JFrame frame= new JFrame("Welecome to JavaTutorial.net");    
        frame.getContentPane().add(panel);
        frame.setSize(600, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(true);
    }
}

And this one is where i control all about the ship

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Game extends JPanel implements ActionListener, KeyListener {

    /**
     * 
     */
    double incY;
    double incX;
    double FaceAngle;
    private static final long serialVersionUID = 1L;
    Timer t = new Timer(5, this);
    int x = 0, y = 0, velX = 0, velY = 0;
    double rotation;
    double delta_x;
    double delta_y;
    double angle;
    private double currentAngle;
    BufferedImage image;
    Point movement = new Point(1, 0);

    public Game(){
        setBackground(Color.WHITE);
        t.start();
        addKeyListener(this);
        this.setFocusable(true);
        setFocusTraversalKeysEnabled(false);
        image = loadImage();
        
    }

    
    public double calcAngleMoveX(double angle){
        return delta_x = Math.sin(angle * Math.PI/180.0);
    }
    
    public double calcAngleMoveY(double angle){
        return delta_y = -Math.cos(angle * Math.PI/180.0);
    }
    
 
    private BufferedImage loadImage(){
        URL imagePath = getClass().getResource("/resources/nave/nave.jpg.jpg");
        BufferedImage result = image;
        try {
            result = ImageIO.read(imagePath);
        } catch (IOException e) {
            System.err.println("Error, imagen no encontrada");
        }

        return result;
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Dimension size = getSize();
        double rotationRequired = Math.toRadians(rotation);
        int locationX = image.getWidth() / 2;
        int locationY=image.getHeight() / 2;
        AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
        AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
        g.drawImage(op.filter(image, null), x, y, null);
        }
        
    public void actionPerformed(ActionEvent e){
        repaint();
        x += velX;
        y += velY;

        if(x<0){
            velX = 0;
            x = 0;
        }

        if(x>750){
            velX = 0;
            x = 750;
        }

        if(y<0){
            velY = 0;
            y = 0;
        }

        if(y>550){
            velY = 0;
            y = 550;
        }
    }

    public void up(){
        velY = -1;
        velX = 0;
    }


    public void down(){
        velY = 1;
        velX = 0;
    }

    public void left(){
        velX = -1;
        velY = 0;
    }

    public void right(){
        velX = 1;
        velY = 0;
    }
    
    public void rotate(){
    double rotate;
    currentAngle+=5.0;
     if (currentAngle >= 360.0) {
       currentAngle = 0;
     }
    repaint();
    }
    
    public void verticalmove(){
        int delta_ix =(int) delta_x;
        int delta_iy =(int) delta_y;
        velX = delta_ix;
        velY = delta_iy;
    }
    
    public void keyPressed(KeyEvent e){
        int code = e.getKeyCode();

        if (code == KeyEvent.VK_W){
            up();
        }

        if (code == KeyEvent.VK_S){
            down();
        }

        if (code == KeyEvent.VK_A){
            left();
        }

        if (code == KeyEvent.VK_D){
            right();
        }
        
        if (code==KeyEvent.VK_E){
            rotation += 5;
        }
        
        if (code==KeyEvent.VK_Q){
            rotation -= 5;
        }
        
        if (code==KeyEvent.VK_R){
            verticalmove();
        }
        }

    public void keyTyped(KeyEvent e){}

    public void keyReleased(KeyEvent e){
        int code = e.getKeyCode();

        if (code == KeyEvent.VK_W){
            velY = 0;
        }
        if (code == KeyEvent.VK_S){
            velY = 0;
        }
        if (code == KeyEvent.VK_A){
            velX = 0;
        }
        if (code == KeyEvent.VK_D){
            velX = 0;
        }
        if (code == KeyEvent.VK_E){
        rotate();
        }
        if (code == KeyEvent.VK_P){
        rotate();
        }
        if (code==KeyEvent.VK_R){
            verticalmove();
        }
    }

}
  • You've defined `calcAngleMoveX` and `calcAngleMoveY`, but you never call them. – tgdavies Nov 12 '22 at 00:09
  • Welcome to wonderful world of trig - can you feel my pain. Basically, you have start point and start direction, so technically, you could do a "path" following approach (literally draw a line and walk it that line). [This](https://stackoverflow.com/questions/72411743/how-to-move-paint-graphics-along-slope/72412859#72412859) will basically do it, but is using a linear time progression to make it happen; [this](https://stackoverflow.com/questions/30433074/jpanel-image-flies-from-the-screen/30433207#30433207) is using a time based approach – MadProgrammer Nov 12 '22 at 00:11
  • [This](https://stackoverflow.com/questions/32392095/how-to-rotate-a-rectangle-after-reaching-specified-position/32397121#32397121) is a "path" based animation, like I said, draw a virtual line and walk it – MadProgrammer Nov 12 '22 at 00:18
  • @tgdavies i know i have to use them but i dont know how, thats why i never call them – Marcelo Morales Nov 12 '22 at 00:26
  • @MadProgrammer so i have to do it in any of those ways in order to make it work? – Marcelo Morales Nov 12 '22 at 00:27
  • @MarceloMorales You have to research your problem and find the solution(s) which best match your needs, I've presented at least two difference approaches – MadProgrammer Nov 12 '22 at 01:02

0 Answers0