Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By kneitzel

Thread: Rotate an image y-axis

  1. #1
    Grenslasn is offline Member
    Join Date
    Apr 2014
    Posts
    4
    Rep Power
    0

    Default Rotate an image y-axis

    How can I rotate an an image so it looks like it?
    I searched hours for it but I couldnt find anything.

    Rotate an image y-axis-fy4o0g2.pngRotate an image y-axis-j2d8f6q.png

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Rotate an image y-axis

    Well you'd have to:

    a) learn about software 3D projection algorithms (or find a library that implements such routines already)

    or

    b) learn to apply a 3D rendering API, like using LWJGL or JOGL

    or

    c) fake it by not rotating anything but rather drawing a shape that looks like picture 2.

    You're very non-specific in your problem description, so I wouldn't know which option or additional options apply or not. Option c would already not apply if the picture can also be a photo in stead of a filled square.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Grenslasn is offline Member
    Join Date
    Apr 2014
    Posts
    4
    Rep Power
    0

    Default Re: Rotate an image y-axis

    I found this with a rotating triangle, but how can I draw a rotated image instead of the triangle?

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.DisplayMode;
    import java.awt.GraphicsDevice;
    import java.awt.GraphicsEnvironment;
    import java.awt.Point;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    import javax.media.opengl.GL2;
    import javax.media.opengl.GLAutoDrawable;
    import javax.media.opengl.GLCapabilities;
    import javax.media.opengl.GLEventListener;
    import javax.media.opengl.GLProfile;
    import javax.media.opengl.awt.GLCanvas;
    import javax.media.opengl.glu.GLU;
    import javax.swing.AbstractAction;
    import javax.swing.ActionMap;
    import javax.swing.InputMap;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.KeyStroke;
    
    import com.jogamp.opengl.util.FPSAnimator;
    import com.jogamp.opengl.util.texture.Texture;
    import com.jogamp.opengl.util.texture.awt.AWTTextureIO;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import static javax.media.opengl.GL.GL_TEXTURE_2D;
    
    /**
     * @author Beta
     *
     */
    public class Render implements GLEventListener {
    
        private static GraphicsEnvironment graphicsEnviorment;
        private static boolean isFullScreen = false;
        public static DisplayMode dm, dm_old;
        private static Dimension xgraphic;
        private static Point point = new Point(0, 0);
        private static Texture myTexture;
    
        private GLU glu = new GLU();
    
        private float rtri = 0.0f;
    
        @Override
        public void display(GLAutoDrawable drawable) {
            final GL2 gl = drawable.getGL().getGL2();
            gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);     // Clear The Screen And The Depth Buffer
            gl.glLoadIdentity();                       // Reset The View
    
            gl.glTranslatef(0f, 0.0f, -6.0f);                 // Move Left 1.5 Units And Into The Screen 6.0
    
            gl.glRotatef(rtri, 0.0f, 1.0f, 0.0f);//(NEW!)
    
            gl.glEnable(GL_TEXTURE_2D);
            //myTexture.bind(gl);
            
            gl.glBegin(GL2.GL_TRIANGLES);                      // Drawing Using Triangl.gles
            gl.glColor3f(1.0f, 0.0f, 0.0f); //Red
            gl.glVertex3f(0.0f, 1.0f, 0.0f);              // Top
            gl.glColor3f(0.0f, 1.0f, 0.0f); //Green
            gl.glVertex3f(-1.0f, -1.0f, 0.0f);              // Bottom Left
            gl.glColor3f(0.0f, 0.0f, 1.0f); //Blue
            gl.glVertex3f(1.0f, -1.0f, 0.0f);              // Bottom Right
            gl.glEnd();                            // Finished Drawing The Triangl.gle
    
            gl.glLoadIdentity();
            gl.glFlush();
            rtri += 0.2f;
        }
    
        @Override
        public void dispose(GLAutoDrawable drawable) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void init(GLAutoDrawable drawable) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void reshape(GLAutoDrawable drawable, int x, int y, int width,
                int height) {
            // TODO Auto-generated method stub
            final GL2 gl = drawable.getGL().getGL2();
    
            if (height <= 0) {
                height = 1;
            }
            final float h = (float) width / (float) height;
            gl.glViewport(0, 0, width, height);
            gl.glMatrixMode(GL2.GL_PROJECTION);
            gl.glLoadIdentity();
            glu.gluPerspective(45.0f, h, 1.0, 20.0);
            gl.glMatrixMode(GL2.GL_MODELVIEW);
            gl.glLoadIdentity();
        }
        
           public Texture load(BufferedImage img, GLProfile gp) {
            if (img == null) {
                return null;
            }
            return AWTTextureIO.newTexture(gp, img, true);
        }
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            // setUp open GL version 2
            final GLProfile profile = GLProfile.get(GLProfile.GL2);
            GLCapabilities capabilities = new GLCapabilities(profile);
            
            // The canvas 
            final GLCanvas glcanvas = new GLCanvas(capabilities);
            Render r = new Render();
            glcanvas.addGLEventListener(r);
            glcanvas.setSize(400, 400);
            
            ImageStore imageStore = new ImageStore();
            Image image = imageStore.getImage("imageToDraw");
            //myTexture = r.load(Utility.toBufferedImage(image), profile);
            
     
            final FPSAnimator animator = new FPSAnimator(glcanvas, 300, true);
            final JFrame frame = new JFrame("nehe: Lesson 4");
    
            frame.getContentPane().add(glcanvas);
    
            //Shutdown
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    if (animator.isStarted()) {
                        animator.stop();
                    }
                    System.exit(0);
                }
            });
    
            frame.setSize(frame.getContentPane().getPreferredSize());
            /**
             * Centers the screen on start up
             *
             */
            graphicsEnviorment = GraphicsEnvironment.getLocalGraphicsEnvironment();
    
            GraphicsDevice[] devices = graphicsEnviorment.getScreenDevices();
    
            dm_old = devices[0].getDisplayMode();
            dm = dm_old;
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    
            int windowX = Math.max(0, (screenSize.width - frame.getWidth()) / 2);
            int windowY = Math.max(0, (screenSize.height - frame.getHeight()) / 2);
    
            frame.setLocation(windowX, windowY);
            /**
             *
             */
            frame.setVisible(true);
            /*
             * Time to add Button Control
             */
            JPanel p = new JPanel();
            p.setPreferredSize(new Dimension(0, 0));
            frame.add(p, BorderLayout.SOUTH);
    
            keyBindings(p, frame, r);
            animator.start();
        }
    
        private static void keyBindings(JPanel p, final JFrame frame, final Render r) {
    
            ActionMap actionMap = p.getActionMap();
            InputMap inputMap = p.getInputMap();
    
            inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0), "F1");
            actionMap.put("F1", new AbstractAction() {
    
                /**
                 *
                 */
                private static final long serialVersionUID = -6576101918414437189L;
    
                @Override
                public void actionPerformed(ActionEvent drawable) {
                    // TODO Auto-generated method stub
                    fullScreen(frame);
                }
            });
        }
    
        protected static void fullScreen(JFrame f) {
            // TODO Auto-generated method stub
            if (!isFullScreen) {
                f.dispose();
                f.setUndecorated(true);
                f.setVisible(true);
                f.setResizable(false);
                xgraphic = f.getSize();
                point = f.getLocation();
                f.setLocation(0, 0);
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                f.setSize((int) screenSize.getWidth(), (int) screenSize.getHeight());
                isFullScreen = true;
            } else {
                f.dispose();
                f.setUndecorated(false);
                f.setResizable(true);
                f.setLocation(point);
                f.setSize(xgraphic);
                f.setVisible(true);
    
                isFullScreen = false;
            }
        }
    
    }

  4. #4
    BinaryDigit09 is offline Member
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default Re: Rotate an image y-axis

    AffineTransform.

  5. #5
    Grenslasn is offline Member
    Join Date
    Apr 2014
    Posts
    4
    Rep Power
    0

    Default Re: Rotate an image y-axis

    I thought AffineTransform would be this:
    c) fake it by not rotating anything but rather drawing a shape that looks like picture 2.
    But i want to do this effect with jogl? Does someone know how to do this please?

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Rotate an image y-axis

    Well, that pretty much is how rotation is done. You just successively update various sets of coordinates and redisplay the image. This gives the illusion that something is rotating. The trick is in the calculations, taking care of hidden line problems, etc. There are packages out there that do that stuff for you as has been previously suggested.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    Grenslasn is offline Member
    Join Date
    Apr 2014
    Posts
    4
    Rep Power
    0

    Default Re: Rotate an image y-axis

    There are packages out there that do that stuff for you as has been previously suggested.
    Yes i decided to take JOGL and the source code above shows a rotatating triangle.
    Instead of this triangle I want to use an image, but I dont really know how to do this?!

  8. #8
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Rotate an image y-axis

    My suggestion is to look at the code, understand it and then modify it.
    That is the only way if you want to use the code.

    And right now I have no idea how we could help you. We are not writing you code. We cannot hold your hand and lead you. You are welcome if you have a question but you still have to move and learn. You cannot write an application if you do not know what you are doing at all.

    And the code seems to be from a lesson - so maybe you already found a tutorial that you could read / try to understand.
    gimbal2 likes this.

Similar Threads

  1. How Do You Rotate A 2D Image?
    By boxing_rex in forum New To Java
    Replies: 17
    Last Post: 05-11-2013, 03:02 PM
  2. How to rotate an image
    By ChipChamp in forum New To Java
    Replies: 4
    Last Post: 06-20-2012, 07:22 PM
  3. Replies: 0
    Last Post: 08-06-2011, 07:47 AM
  4. Image rotate
    By Micromani in forum AWT / Swing
    Replies: 0
    Last Post: 03-17-2010, 01:33 PM
  5. Rotate Image!
    By Moncleared in forum AWT / Swing
    Replies: 1
    Last Post: 02-11-2009, 05:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •