1

hello i really need your help if you would be kind to help me out i'll appreciate it a lot .i have recently started learning java

i am making a java program that allows my player to jump and move to the place of the mouse i think that i have succeeded to make that but it not really SPECIFIC ;

also i want to display my sprite sheet so that he would look like he is really walking but i am having a problem in this part here is my codes :

package animation;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferStrategy;
import java.awt.*;

import javax.swing.*;

import animation.Boulle.move;
import gfx.Sprite;
import gfx.SpriteSheet;

public class deux_bulle extends JFrame implements MouseListener, MouseMotionListener {
    Monp p;

    int X, Y, x = 150, y = 150;
    Thread t, t2, t3,t4;
    int f = 0;
    boolean b = false;
    public Graphics g;

    private int framedelay;
    public static SpriteSheet sheet;
    public static gfx.Sprite Sprite[] = new gfx.Sprite[3]; // my sprite sheet
                                                            // has 3 pictures

    public deux_bulle() {
        setSize(400, 400);

        p = new Monp();

        sheet = new SpriteSheet("/Untitled.png");
        add(p);
        p.addMouseListener(this);
        p.addMouseMotionListener(this);

        for (int i = 0; i < Sprite.length; i++) {
            Sprite[i] = new Sprite(sheet, i + 1, 1);
        }

    }

    class Monp extends JPanel {

        @Override
        public void paint(Graphics g) {

            super.paint(g);

            g.drawImage(deux_bulle.Sprite[f].getBufferedIamge(), x, y, 200, 200, null);

        }
    }

    class moveX implements Runnable {

        @Override
        public void run() {

            while (x < X) {
                try {
                    x++;
                    if (f < 3)

                    //g.drawImage(deux_bulle.Sprite[f].getBufferedIamge(), x, y, 200, 200, null);
                    repaint();
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            while (x > X) {
                try {
                    x--;
                    repaint();
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }

    class image implements Runnable{
        public void run() {
            framedelay++;
            if (framedelay>=3)
          {f++;
                if(f>=3)
              {f=0;}
                framedelay=0;}
        }}
    class moveY implements Runnable {

        @Override
        public void run() {
            while (y < Y) {
                try {
                    y++;
                    repaint();

                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            while (y > Y) {
                try {

                    y--;
                    repaint();

                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }

    class jump implements Runnable {

        @Override
        public void run() {
            int p = y;
            while (y != Y) {
                try {
                    y--;
                    repaint();

                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            while (y != p) {
                try {
                    y++;
                    repaint();

                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }

    public static void main(String[] args) {
        deux_bulle db = new deux_bulle();
        db.setVisible(true);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2) {
            Y = 70;

            t3 = new Thread(new jump());
            t3.start();
        }
    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent arg0) {

    }

    @Override
    public void mousePressed(MouseEvent e) {
        X = e.getX();
        Y = e.getY();
            t4 = new Thread(new image());
            t = new Thread(new moveX());
            t2 = new Thread(new moveY());
            t.start();
            t2.start();
            t4.start();


    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseDragged(MouseEvent e) {
        // TODO Auto-generated method stub
        x = e.getX();
        y = e.getY();
        repaint();
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        // TODO Auto-generated method stub

    }

}

my sprite sheet class

 package gfx;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class SpriteSheet {
private BufferedImage sheet;
    public SpriteSheet(String path) {

        try {
            sheet= ImageIO.read(getClass().getResource(path));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
 public BufferedImage getSprite(int x,int y)

 {
    return sheet.getSubimage(x*66-66,y*66-66,66,66);}
}

my sprite class

package gfx;

import java.awt.image.BufferedImage;

public class Sprite {
 public SpriteSheet sheet;
 public BufferedImage image;
    public Sprite(SpriteSheet sheet,int x,int y) {
        image= sheet.getSprite(x, y);
    }
 public BufferedImage getBufferedIamge(){
    return image;

 }
}
  • [That's one way to manage a sprite sheet](http://stackoverflow.com/questions/35472233/load-a-sprites-image-in-java/35472418#35472418) – MadProgrammer Apr 18 '17 at 21:56
  • [That's probably a better way to manage dynamic movement](http://stackoverflow.com/questions/16493809/how-to-make-sprite-jump-in-java/16494178#16494178), [that's another](http://stackoverflow.com/questions/19626338/japplet-creates-a-ball-that-bounces-and-gets-progressively-less-high-in-java/19626396#19626396) – MadProgrammer Apr 18 '17 at 21:58
  • [That's another way to manage sprite sheets](http://stackoverflow.com/questions/27933159/how-to-draw-an-bufferedimage-to-a-jpanel/27933189#27933189) – MadProgrammer Apr 18 '17 at 21:59
  • As a general recommendation, you don't need three threads, one will do, it should coordinate the update to the state (movement), update the UI and manage the framerate. For simplicity, a Swing `Timer` might be a better place to start – MadProgrammer Apr 18 '17 at 22:01
  • thanks this helps me and i'll try to work with timer –  Apr 18 '17 at 22:43

0 Answers0