Results 1 to 5 of 5
  1. #1
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default movement during scaling

    well I have only just learned how to do this, but I am attempting to create a figure that grows in size every second or so, except the figure also moves downward as it grows. also if i change the position it draws it to say... 130 (in x and y) it also moves in the x direction as well. I am just unsure what is causing this, any advice or help is appreciated. Fram is my window adapter, and pls tell me if there is anything here woefully inefficient, thx. Also, oddly enough, if you comment out the translate() that is causing the scrolling background, it still moves as it scales.

    here is code:
    Java Code:
    import java.awt.*;
    import java.awt.image.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.imageio.*;
    import java.io.*;
    public class TRYGRAPHICS2D extends Fram
    {
        GPanel base = new GPanel();
        public TRYGRAPHICS2D()
        {
            super("TRYGRAPHICS2D");
            add(base);
            setSize(250,250);
            setResizable(false);
            setVisible(true);
        }
        public static void main(String[] args)
        {
            TRYGRAPHICS2D a = new TRYGRAPHICS2D();
        }
    }
    
    public class GPanel extends Panel
    {
        File file = new File("J:\\programmin\\WhiteBook\\CH11\\pic\\starfish.jpg");
        BufferedImage img;
        BufferedImage guy;
        Timer timer;
        int xoff=0, yoff=0;
        double xs=1.0,ys=1.0;
        GPanel()
        {
            setBackground(Color.BLUE);
            try
            {
                img = ImageIO.read(getClass().getClassLoader().getResource("pic/starfish.jpg"));
                guy = ImageIO.read(getClass().getClassLoader().getResource("pic/stickman.jpg"));
            }
            catch(IOException e) {System.out.println("error");}
            start();
        }
        public void paint(Graphics g)
        {
            //System.out.println(getClass().getClassLoader().getResource("pic/starfish.jpg"));
            Graphics2D g2 = (Graphics2D)g;
            //g.drawLine(50,50,100,100);
            //g2.draw(new Line2D.Double(50, 50, 100, 100));
            g2.translate(-xoff,-yoff);
            g2.drawImage(img,null,0,0);
            g2.translate(0,0);
            g2.scale(xs,ys);
            g2.drawImage(guy,30,30,null);
        }
        public void start()
        {
            timer = new Timer(2000, new ActionListener() 
            {
                int tcount=0;
                public void actionPerformed(ActionEvent evt) 
                {
                    xoff+=10;
                    //yoff+=10;
                    xs+=.25;
                    ys+=.25;
                    repaint();
                }
            });
            timer.setDelay(500);
            timer.start();
        }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: movement during scaling

    If I understand correctly what you need to do is calculate the centre of your image and recalculate the x & y coords from that point for your "enlarged" image.

  3. #3
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: movement during scaling

    i have been experimenting with it and that it seems right, how would you calculate the new coordinates needed though? what equation should be used to find them?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: movement during scaling

    Draw it out on a piece of graph paper so you can see. Where is the centre in relation to your x & y co-ords? Hint: the width and height of your image is a factor.

  5. #5
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: movement during scaling

    I'm sorry to start this up after so long, but I can't seem to figure out this equation.

    I drew a line just above the image, alternating colors every 10 pixels (for measuring):
    Java Code:
    for(int x = 0;x < 20; ++x)
            {
                g2.setColor((x+2)%2==0 ? Color.BLUE : Color.RED);
                g2.drawLine(25 + (x*10),25,35 + (x*10),25);
            }
    so I scale it forward 2 times in the x, it covers roughly 2.5 of these lines, so I move it back 25. This doesn't work, the image goes back too far. I screw around and find out that moving it back about 12 makes it work, then I go 2 3 times scale, yet again, for some odd reason it isn't going according to measurement, then I notice that moving it back 1 pixel in the source moves it back 3 on the screen. This irritates me, because if it is moving by multiples like that I would need to use floating point numbers to get it even again right? Problem is I have yet to see a draw image method that supports that... Do you know of one?
    Last edited by PRW56; 07-13-2012 at 10:32 AM.

Similar Threads

  1. Replies: 2
    Last Post: 03-10-2012, 04:26 AM
  2. Scaling VS. Clipping
    By JojoDiaz in forum New To Java
    Replies: 0
    Last Post: 11-14-2011, 04:10 AM
  3. scaling image
    By OmerHalit in forum Advanced Java
    Replies: 3
    Last Post: 05-07-2010, 01:30 AM
  4. Scaling a JPEG
    By ScottVal in forum Advanced Java
    Replies: 5
    Last Post: 03-21-2009, 08:47 PM
  5. scaling images
    By willemjav in forum Java Applets
    Replies: 7
    Last Post: 06-19-2008, 09:54 AM

Posting Permissions

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