Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: cant rotate image to mouse position

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default cant rotate image to mouse position

    Hi, i am currentlly trying to make 2d shooter and im having problem with rotating player to face the mouse. Here is my code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.MouseInfo;
    import java.awt.Point;
    import java.awt.PointerInfo;
    import java.awt.Toolkit;
    import java.awt.event.KeyEvent;
     
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.geom.AffineTransform;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.JPanel;
     
     
    public class game extends JPanel implements Runnable, KeyListener, MouseMotionListener,MouseListener {
     
        private int posX, posY;
        private Thread thread;
     
     
    public int cx;
    public int cy;
    Char p =new Char();
             public int x;
             public int y;  
              public int wx;
              public int wy;
             BufferedImage grass, map;
        public BufferedImage tree;
            public BufferedImage player;
            public int L;
            public int U;
            public int in;
            public int CameraX;
            public int CameraY;
            int R = 64;
            int D = 64;
     
    int centerX = 800 / 2;
    int centerY = 800 / 2;
    float xDistance = cx - centerX;
    float yDistance = cy - centerY;
    double angle = Math.toDegrees(Math.atan2(yDistance, xDistance));
      public void load(){
     
                          BufferedImage walkable = null;
          try {
             grass = ImageIO.read(new File("grass.png"));
             tree = ImageIO.read(new File("tree.png"));
             player = ImageIO.read(new File("player.png"));
     
          } catch(IOException e) {
             System.out.println("failed");
          }
             }
        public game() {
     
     
                        load();
                        addKeyListener(this);
                        addMouseListener(this);
                        addMouseMotionListener(this);
                        requestFocusInWindow();
                        this.setFocusable(true);
     
        }
        public void addNotify(){
            super.addNotify();
            thread = new Thread(this);
            thread.start();
        }
        public void paint(Graphics g){
     
            super.paint(g);
    map m = new map();
     
            Graphics2D g2 = (Graphics2D)g;
          Graphics2D world = (Graphics2D)g;
          //Graphics2D ob = (Graphics2D)g;
          world.drawImage(map, wx,wy, this);
            // DRAW HERO
     
    for (int y =0 ; y <= 19; y++){
        for (int x = 0; x <= 19; x++){
     
            int L = (x*64)+CameraX;
            int U = (y*64)+CameraY;
            int R = 64;
            int D = 64;
               /// g2d.rotate(Math.toRadians(-90));
     
            if (m.map[y][x] == 0){
                //g2d.setColor(Color.green);
                world.drawImage(grass,L,U,R,D,this);
               // g2d.setColor(Color.gray);
               // g2d.fillRect(L+64, U, 8, 64);
     
            }
            if (m.map[y][x] == 2){
                world.setColor(Color.green);
                world.drawImage(grass,L,U,R,D,this);
                world.setColor(Color.green);
                world.drawImage(tree,L,U,R,D,this);
     
            }
     
     
        }
     
     
    ///g2d1.rotate(10);
    }
           /// g2d.drawLine(100, 100, 50, 50);
           AffineTransform trans = new AffineTransform();
            AffineTransform identity = new AffineTransform();
            trans.setTransform(identity);
            //trans.rotate(Math.toRadians(angle) );
            trans.setToRotation(Math.toRadians(angle));
     
            g2.translate(centerX, centerY);
            g2.drawImage(player, trans,this);
     
            g2.setColor(Color.black);
            g2.drawString("x = " + cx + " , y = " + cy, centerX-700, centerY-700);
            Toolkit.getDefaultToolkit().sync();
            g.dispose();
        }
        public void run() {
            while(true){
                repaint();
     
     
              }    
        }
        @Override
        public void keyTyped(KeyEvent e) {
     
        }
       @Override
        public void keyPressed(KeyEvent e) {
    map m = new map();
      int keyCode = e.getKeyCode();
        switch( keyCode ) { 
            case KeyEvent.VK_W:
     
                CameraY+=1;
                break;
            case KeyEvent.VK_D:
                CameraX-=1;
     
                break;
            case KeyEvent.VK_A:
                CameraX+=1;
     
     
                break;
            case KeyEvent.VK_S:
     
                CameraY-=1;
     
                break;
     
         }
        }
     
        @Override
        public void keyReleased(KeyEvent ke) {
     
        }
     
     
        public void mouseClicked(MouseEvent e) {
        // Point mc = new Point(cx=e.getX(), cy=e.getY());
        }
     
     
        public void mousePressed(MouseEvent e) {
       // Point mc = new Point(cx=e.getX(), cy=e.getY());
        }
     
     
        public void mouseReleased(MouseEvent me) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     
     
        public void mouseEntered(MouseEvent e) {
       //  cy=e.getX();
        // cy=e.getY();
        }
        public void mouseMoved(MouseEvent e){
          Point mc = new Point(cx=e.getX(), cy=e.getY());
        }
     
     
        public void mouseExited(MouseEvent me) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     
        @Override
        public void mouseDragged(MouseEvent me) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     
     
    }
    I pasted all of it, all variables are after the public class thing and the code to rotate is after tile map drawing loop in paint method. sorry for so much code im just starting to learn java so i dont know how to shorten it.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: cant rotate image to mouse position

    The code does not compile without errors.
    Also I don't see a main() method for executing when the errors are fixed.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: cant rotate image to mouse position

    Errors are probablly becouse of char and map classes. Isnt it? Becouse it compiles fine for me. Also i run this from another class. I create frame and the do game board = new game(); frame.add(board);

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: cant rotate image to mouse position

    Without all the missing parts, the code can't be compiled and executed for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: cant rotate image to mouse position

    You have your angle down and I see you are using the AffineTransformation but this code is far too large and unclean to read. If you can identify parts of code you have singled out errors in, then we can further assist you.

  6. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: cant rotate image to mouse position

    game.java
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.MouseInfo;
    import java.awt.Point;
    import java.awt.PointerInfo;
    import java.awt.Toolkit;
    import java.awt.event.KeyEvent;
     
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.geom.AffineTransform;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.JPanel;
     
     
    public class game extends JPanel implements Runnable, KeyListener, MouseMotionListener,MouseListener {
     
        private int posX, posY;
        private Thread thread;
     
     
    public int cx;
    public int cy;
     
             public int x;
             public int y;  
              public int wx;
              public int wy;
             BufferedImage grass, map;
        public BufferedImage tree;
            public BufferedImage player;
            public int L;
            public int U;
            public int in;
            public int CameraX;
            public int CameraY;
            int R = 64;
            int D = 64;
     
    int centerX = 800 / 2;
    int centerY = 800 / 2;
    float xDistance = cx - centerX;
    float yDistance = cy - centerY;
    double angle = Math.toDegrees(Math.atan2(yDistance, xDistance));
      public void load(){
     
                          BufferedImage walkable = null;
          try {
             grass = ImageIO.read(new File("grass.png"));
             tree = ImageIO.read(new File("tree.png"));
             player = ImageIO.read(new File("player.png"));
     
          } catch(IOException e) {
             System.out.println("failed");
          }
             }
        public game() {
     
     
                        load();
                        addKeyListener(this);
                        addMouseListener(this);
                        addMouseMotionListener(this);
                        requestFocusInWindow();
                        this.setFocusable(true);
     
        }
        public void addNotify(){
            super.addNotify();
            thread = new Thread(this);
            thread.start();
        }
        public void paint(Graphics g){
     
            super.paint(g);
    map m = new map();
     
            Graphics2D g2 = (Graphics2D)g;
          Graphics2D world = (Graphics2D)g;
          //Graphics2D ob = (Graphics2D)g;
          world.drawImage(map, wx,wy, this);
     
    for (int y =0 ; y <= 19; y++){
        for (int x = 0; x <= 19; x++){
     
            int L = (x*64)+CameraX;
            int U = (y*64)+CameraY;
            int R = 64;
            int D = 64;
               /// g2d.rotate(Math.toRadians(-90));
     
            if (m.map[y][x] == 0){
                //g2d.setColor(Color.green);
                world.drawImage(grass,L,U,R,D,this);
               // g2d.setColor(Color.gray);
               // g2d.fillRect(L+64, U, 8, 64);
     
            }
            if (m.map[y][x] == 2){
                world.setColor(Color.green);
                world.drawImage(grass,L,U,R,D,this);
                world.setColor(Color.green);
                world.drawImage(tree,L,U,R,D,this);
     
            }
     
     
        }
     
     
    ///g2d1.rotate(10);
    }
           /// g2d.drawLine(100, 100, 50, 50);
           AffineTransform trans = new AffineTransform();
            AffineTransform identity = new AffineTransform();
            trans.setTransform(identity);
            //trans.rotate(Math.toRadians(angle) );
            trans.setToRotation(Math.toRadians(angle));
     
            g2.translate(centerX, centerY);
            g2.drawImage(player, trans,this);
     
            g2.setColor(Color.black);
            g2.drawString("x = " + cx + " , y = " + cy, centerX-700, centerY-700);
            Toolkit.getDefaultToolkit().sync();
            g.dispose();
        }
        public void run() {
            while(true){
                repaint();
     
     
              }    
        }
        @Override
        public void keyTyped(KeyEvent e) {
     
        }
       @Override
        public void keyPressed(KeyEvent e) {
    map m = new map();
      int keyCode = e.getKeyCode();
        switch( keyCode ) { 
            case KeyEvent.VK_W:
     
                CameraY+=1;
                break;
            case KeyEvent.VK_D:
                CameraX-=1;
     
                break;
            case KeyEvent.VK_A:
                CameraX+=1;
     
     
                break;
            case KeyEvent.VK_S:
     
                CameraY-=1;
     
                break;
     
         }
        }
     
        @Override
        public void keyReleased(KeyEvent ke) {
     
        }
     
     
        public void mouseClicked(MouseEvent e) {
        // Point mc = new Point(cx=e.getX(), cy=e.getY());
        }
     
     
        public void mousePressed(MouseEvent e) {
       // Point mc = new Point(cx=e.getX(), cy=e.getY());
        }
     
     
        public void mouseReleased(MouseEvent me) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     
     
        public void mouseEntered(MouseEvent e) {
       //  cy=e.getX();
        // cy=e.getY();
        }
        public void mouseMoved(MouseEvent e){
          Point mc = new Point(cx=e.getX(), cy=e.getY());
        }
     
     
        public void mouseExited(MouseEvent me) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     
        @Override
        public void mouseDragged(MouseEvent me) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     
     
    }
    map.java
     
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JFrame;
    import static javax.swing.JFrame.EXIT_ON_CLOSE;
     
    /**
     *
     * @author User
     */
    public class map extends JFrame{
     
    public int mapy=19;
     
    public int x;
    public int y;
    public static int[][] map =    {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
                                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                                    {1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                                    {1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                                    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                                    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}};
     
        public map(){
     
        }
         public void paint(Graphics g){
     
     
         }
     
    }
    frame.java(main)
     
    import java.awt.BorderLayout;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.WindowConstants;
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     * @author User
     */
    class frame {
     
        public frame() {
            JFrame frame = new JFrame();
            game board = new game();
            frame.add(board);
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.setSize(800, 800);
            frame.setVisible(true);
     
        }
     
         public static void main(String[] args) {
            new frame();
        }
    }
    Here is 3 files needed for this to run.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: cant rotate image to mouse position

    java.lang.UnsupportedOperationException: Not supported yet.
    The code has traps in it that need to be removed so it can be executed without these exceptions.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Nov 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: cant rotate image to mouse position

    I think the problem is i have bad angle...

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: cant rotate image to mouse position

    Yes, that sounds like it could be where some of the problems are.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. cant rotate image
    By deivisxm in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 21st, 2013, 04:09 PM
  2. Replies: 4
    Last Post: August 30th, 2013, 12:57 PM
  3. Replies: 3
    Last Post: May 5th, 2012, 08:33 AM
  4. How to get Mouse Position even if it is not within our application?
    By Freaky Chris in forum Java Programming Tutorials
    Replies: 2
    Last Post: January 4th, 2012, 10:57 AM
  5. How to get Mouse Position even if it is not within our application?
    By Freaky Chris in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 29th, 2009, 12:41 PM