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 7 of 7

Thread: Having difficult to move("focus") one sprite frame to another. Please help.

  1. #1
    Junior Member marksquall's Avatar
    Join Date
    Jan 2009
    Posts
    23
    My Mood
    Fine
    Thanks
    0
    Thanked 1 Time in 1 Post

    Question Having difficult to move("focus") one sprite frame to another. Please help.

    Dear members and administrators,

    Hello and a greeting of peace. I hope everyone is going great.

    I have been into simple sprite animation lately and I have successfully done it using the other programming language (VB .NET). Just a simple up, down left, right movement of sprite moving along the Form. I tried to "transfer" (or let's say convert) this code to Java using JFrame as the Form counterpart. But displaying just one sprite has been difficult. If it is not too large, it is too small, if it's not too small, I just dislayed all sprites in the JFrame. I cant "focus" it on the top-left image as my first frame of sprite.

    Attached herein is the copy of the character I want to move in the JFrame:

    char.png

    I want to focus a "rectangle" on the top-left most of the image, so it has an illusion of a single image, then as I press a keyboard S (for down movement), the "rectangle" will simply focus on the next image and so on, having the illusion that it is moving downwards.


    I hope someone could give me suggestion while I am doing this up to this point. Thank you and more power.

    Respectfully Yours,

    MarkSquall


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

    Default Re: Having difficult to move("focus") one sprite frame to another. Please help.

    You said you've attempted to convert the VB.NET code to Java so lets see some evidence. Show us work, ask specific questions. Where are you stuck? What is confusing you? Be intuitive in your work and let the community support you along the way.

    Quote Originally Posted by marksquall View Post
    If it is not too large, it is too small, if it's not too small
    What do you mean by this statement?

  3. #3
    Junior Member marksquall's Avatar
    Join Date
    Jan 2009
    Posts
    23
    My Mood
    Fine
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Having difficult to move("focus") one sprite frame to another. Please help.

    Hello everyone,

    I am sorry for not including the code, I am just kind of working on collision detection in the other language. By the way, here is the code I tried to do:

    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Toolkit;
    import java.awt.Image;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    class ImageImplement extends JPanel {
     
          private Image img;
     
          private int leftPos =0;
          public ImageImplement(Image img) {
            this.img = img;
            Dimension size = new Dimension(0, 0);
            setPreferredSize(size);
            setMinimumSize(size);
            setMaximumSize(size);
            setSize(size);
            setLayout(null);
     
          }
     
          public void paintComponent(Graphics gr) {
     
    //          
    //           VB.NET Code
    //           Dim img As New Bitmap(Application.StartupPath & "\img\char.png")
    //           frames(x) = New Bitmap(32, 48)
    //           Dim gr As Graphics = Graphics.FromImage(frames(x))
    //           gr.DrawImage(img, 0, 0, New Rectangle(leftPos, Int(x / 4) * 64, 70, 70), GraphicsUnit.Pixel) <--VB.NET
    //           VB.NET
     
    //          int frameX = (0 % columns) * width;
    //          int frameY = (0/ columns) * height;
    //gr.drawImage(img, 0, 0, leftPos, ((int) x / 4 ) * 64, 70, 70, null);
    //          gr.drawImage(img, 0, 0, 70, 70, this);
     
               gr.drawImage(img, 0, 0, 40, 70, 50, 0, 70, 70, null); //<--I am currently experimenting on this one     
     }
     
    //      public void paint(Graphics g) {
    //        super.paint(g);
    //
    //        Graphics2D g2d = (Graphics2D)g;
    //        g2d.drawImage(img, 0, 0, 32, 78, this);
    //
    //        //Toolkit.getDefaultToolkit().sync();
    //        g.dispose();
    //    }
     
     
        }
     
    public class MainWIn extends JFrame
    {
    public static void main(String args[])
    {
        new MainWIn().start();
    }
     
    public void start()
    {
          ImageImplement panel = new ImageImplement(new ImageIcon("D:\\char.png").getImage());
          add(panel);
          setVisible(true);
          setSize(400,400);
          setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    }

    Kinda mess, sorry. Well I am experimenting on the line:

    gr.drawImage(img, 0, 0, 40, 70, 50, 0, 70, 70, null);

    It seems I can't get the right combination, that is why I said my image (character) was to large (too pixelated) and too small (so tiny I can't see them anymore).

    I just want to display the first image (very top left) as my start of my animation.

    Thanks for the detailed advise AlexHail.


    Best regards,

    MarkSquall

  4. #4
    Junior Member marksquall's Avatar
    Join Date
    Jan 2009
    Posts
    23
    My Mood
    Fine
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Having difficult to move("focus") one sprite frame to another. Please help.

    Dear members and administrators,

    I hope someone could give me an idea how to achieve this, or any tutorial that might give an insight on sprite movement.

    Thanks a lot guys for any tutorials that you might give.



    Warm regards,

    MarkSquall

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,412
    Thanks
    49
    Thanked 2,214 Times in 2,187 Posts

    Default Re: Having difficult to move("focus") one sprite frame to another. Please help.

    Is this what you are trying to do:
    Draw a number of images in rows and columns making a rectanglar shape
    Draw a rectangle around one of the images
    Move the surrounding rectangle to be around another image by key presses
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member marksquall's Avatar
    Join Date
    Jan 2009
    Posts
    23
    My Mood
    Fine
    Thanks
    0
    Thanked 1 Time in 1 Post

    Talking Re: Having difficult to move("focus") one sprite frame to another. Please help.

    Dear Norm:

    Hello and hi, hope you're always in good health.

    I guess that's what I want to do.

    My plan is without chopping (if I chop the image, I will have 16 PNG files of course) the image into "image frames", it will simply move the rectangular "focus" to each of the frames of the image (the image is attached on my first post). Of course, the image "inside" the rectangle is the only one visible from the JFrame (JPanel?).

    My plan is when I press a button (let's say the "S" button) the rectangular shape will move (or should I say will shift its focus?or is it accurate to say just changing the x, y coordinate of the rectangle) to the second image (second to the top) and the image will move one pixel down, thus having an illusion that the image is walking downwards in the JFrame (JPanel?).

    This line:

    Dim leftPos As Integer
    leftPos = 0
    gr.DrawImage(img, 0, 0, New Rectangle(leftPos, Int(x / 4) * 64, 70, 70), GraphicsUnit.Pixel)<--VB.NET


    from my VB .NET does the trick in focusing the rectangle in the upper left corner of the image (my first frame).


    Sorry for the awful English. Thanks Norm and thanks everyone.


    Warmest regards,

    MarkSquall

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,412
    Thanks
    49
    Thanked 2,214 Times in 2,187 Posts

    Default Re: Having difficult to move("focus") one sprite frame to another. Please help.

    accurate to say just changing the x, y coordinate of the rectangle to the second image
    Yes, that is what I was trying to say.
    The key listener would compute the coordinates for the rectangle's new position and call repaint()
    When the paintComponent() method is called, it would use those coordinates to draw the rectangle.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Understanding layers of system logic. Clarity for what "SDK" & "JDK" mean
    By MilkWetGhost in forum Java Theory & Questions
    Replies: 1
    Last Post: October 3rd, 2013, 12:25 PM
  2. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  3. Replies: 3
    Last Post: December 7th, 2011, 01:03 AM
  4. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 07:35 AM