Results 1 to 5 of 5
  1. #1
    cvillejin is offline Member
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default Rotating an image 90 Degrees

    Hi, I'm trying to rotate an image 90 degrees clockwise AND counterclockwise
    at the click of a button. For instance if a click something that says "clockwise" it rotates my image 90 degrees clockwise. someone told me to use the code:

    public abstract void rotate(double theta,
    double x,
    double y)

    but I don't know how I'm supposed to use this code...nor where am i am supposed to put it. I mean, where/how do i put the 90 degrees in? inside of theta just put 90? and i put this entire code in mouse listeners? waitforClick(); ??

    someone please help

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Consider providing more details to your question, enough so that we may be able to help you. A good resource that can give you good suggestions on just what we need are the links on how to ask a smart question and how to create an SSCCE, both in my signature links below. Luck.

  3. #3
    cvillejin is offline Member
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default

    can someone give example of how to use the code above? and also

    i think there's something wrong with my for loop in the public GImage crop or my initialization of int[][] cropped, but i don't know how to revise it

    i got an error saying the following and i don't know how to fix it:

    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 200
    at ImageBanner.crop(ImageBanner.java:116)
    at ImageBanner.splitImage(ImageBanner.java:100)
    at ImageBanner.mouseClicked(ImageBanner.java:170)
    at java.awt.AWTEventMulticaster.mouseClicked(AWTEvent Multicaster.java:253)
    at java.awt.Component.processMouseEvent(Component.jav a:6270)
    at java.awt.Component.processEvent(Component.java:603 2)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4247)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)


    Java Code:
    import acm.graphics.*;
    import acm.program.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.*;
    import javax.swing.*;
    
    // -------------------------------------------------------------------------
    /**
    * @author 
    * @version 2011.03.25
    */
    public class ImageBanner extends StudentTestableGraphicsProgram
    {
        //~ Instance/static variables .............................................
        public static final int APPLICATION_WIDTH = 1024;
        public static final int APPLICATION_HEIGHT = 600;
    
        public int screenWidth;
        public int screenHeight;
    
        public GImage pic;
    
        public GImage bannerOne;
        public GImage bannerTwo;
        public GImage bannerThree;
        public GImage bannerFour;
        public GImage bannerFive;
    
        GImage sized;
    
        GOval rClockwise;
        GLabel rClockwise2 = new GLabel("Clockwise");
    
        GOval rCounter;
        GLabel rCounter2 = new GLabel("Counterclock");
    
        GOval iSplit;
        GLabel iSplit2 = new GLabel("Split!");
    
        //~ Methods ...............................................................
    
        // ----------------------------------------------------------
        /**
        *
        */
        public void init()
        {
            addMouseListeners();
            //find an image to display
            String file = chooseImage();
            pic = new GImage(file);
            
            //resizes image to 485,68
            pic = lossyResize(pic, 485, 200);
            pic = rotateClockwise(pic);
    
            //creates button for rotating image clockwise
            rClockwise = new GOval(150, 100);
            add(rClockwise, 10, 450);
            add(rClockwise2, 20, 510);
            rClockwise2.setFont("Serif-Bold-30");
            rClockwise.setFillColor(Color.GREEN);
            rClockwise.setFilled(true);
    
            //creates button for rotating image counterclockwise
            rCounter = new GOval(170, 120);
            add(rCounter, 300, 450);
            add(rCounter2, 310, 520);
            rCounter2.setFont("Serif-Bold-25");
            rCounter.setFillColor(Color.YELLOW);
            rCounter.setFilled(true);
    
            //creates button for splitting image into 5 banner parts
            iSplit = new GOval(150, 100);
            add(iSplit, 590, 450);
            add(iSplit2, 630, 510);
            iSplit2.setFont("Serif-Bold-30");
            iSplit.setFillColor(Color.RED);
            iSplit.setFilled(true);
            
           
    
        }
        /**
         *
         */
        public void run()
        {
    
            add(pic, 200, 50);
        }
        //splits images into 5 seperate pieces
        public void splitImage(GImage image)
        {
            int width =(int) image.getWidth() / 5;
    
            bannerOne = crop(image, 0, 0, width,(int) image.getHeight());
            bannerTwo = crop(image, width, 0, width,(int) image.getHeight());
            bannerThree = crop(image, width * 2, 0, width,(int) image.getHeight());
            bannerFour = crop(image, width * 3, 0, width,(int) image.getHeight());
            bannerFive = crop(image, width * 4, 0, width,(int) image.getHeight());
    
        }
        //crops images
        public GImage crop(GImage image, int x, int y, int width, int height)
        {
            int[][] cropped = new int[width][height];
            int[][] imageArray = image.getPixelArray();
            for(int xPos = x; xPos < x + width; xPos++)
            {
                for(int yPos = y; yPos < y + height; yPos++)
                {
                    cropped[xPos - x][yPos - y] = imageArray[xPos][yPos];
                }
            }
    
            return new GImage(cropped);
        }
        //rotates image clockwise
        public GImage rotateClockwise(GImage image)
        {
            int[][] original = image.getPixelArray();
            int[][] rotated = new int[original[0].length][original.length];
    
            for(int x = 0; x < original.length; x++)
            {
                for(int y = 0; y < original[0].length; y++)
                {
                    rotated[y][x] = original[x][y];
                }
            }
    
            return new GImage(rotated);
    
        }
       
        /**
         *
         */
        public void mouseClicked(MouseEvent e)
        {
           double x = e.getX();
           double y = e.getY();
           //allows user to click the button clockwise
            if(rClockwise.contains(x,y))
            {
                System.out.println("Rotate noww");
                pic = rotateClockwise(pic);
              
            }
            
           //allows user to click the button counter
            if(rCounter.contains(x,y))
            {
                System.out.println("COUNTERRR");
                
            }    
            
            //allows users to click the button split
            if(iSplit.contains(x,y))
            {
                System.out.println("SPLIT NUKKA");
                
            }
            
            //adds the 5 split banner images
            splitImage(pic);
            add(bannerOne, 0, 0);
            add(bannerTwo, 0, 100);
            add(bannerThree, 0, 200);
            add(bannerFour, 0, 300);
            add(bannerFive, 0, 400);
    
        }
        /**
         * [Author: ] Converts a GImage to be of a new width and height and returns it
         * Note that this process is "lossy" so the returned GImage will have lesser resolution than
         * the original if it is smaller size, and there is not a way to reverse this loss of precision
         * @param orig, the original GImage to resize
         * @param width the desired new width of the image
         * @param height the desired new height of the image
         * @return the GImage orig, resized to widthXheight
         */
        public GImage lossyResize(GImage orig, double width, double height)
        {
            return new GImage(orig.getImage().getScaledInstance((int) width,(int) height, 0));
        }
        /**
         * [Author: ] Saves a GImage to disk as a png file
         * @param picture A GImage picture that will be converted to png format and saved on disk
         * @param filename The name of the file to save (path optional, default to project folder.  e.g. "output.png")
         */
        public void saveImage(GImage picture, String filename)
        {
            try
            {
                File name = new File(filename);
                Image img = picture.getImage();
                BufferedImage buff = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
                Graphics graph = buff.getGraphics();
                graph.drawImage(img, 0, 0, null);
                graph.dispose();
                ImageIO.write(buff, "png",name);
            }
            catch(IOException e)
            {
                System.out.println("Error when writing file: " + e.toString());
            }
        }
        /**
         * [Author: ] Shows a file browser dialog so the user can choose a file.
         * Only image files *should* be chosen, but this method does not prevent other types of files to be chosen.
         * @return A string representing the path and filename chosen (e.g. "C:\My Documents\image.jpg")
         */
        public String chooseImage()
        {
            JFileChooser dialog = new JFileChooser();
            int result = dialog.showOpenDialog(this);
            if(result == JFileChooser.APPROVE_OPTION)
            {
                return dialog.getSelectedFile().getPath();
            }
            else
                return "";
        }
    }
    Last edited by cvillejin; 04-03-2011 at 07:45 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I think a problem that most of us will have in answering this question is that it involves non-standard libraries, in particular, the acm.graphics and acm.program libraries.

  5. #5
    cvillejin is offline Member
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default

    can someone tell me how to use the code:

    public abstract void rotate(double theta,
    double x,
    double y)

    ?

Similar Threads

  1. Problem in rotating an image
    By newtoapplets in forum Java Applets
    Replies: 0
    Last Post: 02-28-2011, 06:12 AM
  2. Rotating Buffered Image distorts image
    By VortexSpin in forum Java 2D
    Replies: 1
    Last Post: 02-13-2011, 05:54 AM
  3. Rotating an image
    By lackofcolor in forum Java 2D
    Replies: 3
    Last Post: 02-27-2009, 11:54 PM
  4. Rotating Image?
    By sciguy77 in forum Java Applets
    Replies: 9
    Last Post: 02-17-2009, 01:47 AM
  5. Rotating and flipping an image in SWT
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-02-2008, 08:01 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
  •