Results 1 to 2 of 2

Thread: pictures help

  1. #1
    Gamewolf is offline Member
    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Default pictures help

    Hi

    with some help and by looking at tutorials i have managed to create this monstrosity

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    
    
    public class RotateImage extends JPanel {
      int   xk = 0;
      int   yk = 0;
    
      boolean right, left, up, down;
    
    
      // Declare an Image object for us to use.
      Image character, button;
    
      // Create a constructor method
      public RotateImage() {
        // Load an image to play with.
        character = Toolkit.getDefaultToolkit().getImage("BlobA1.png");
        button = Toolkit.getDefaultToolkit().getImage("earth.png");
    
        wasd go = new wasd();
        go.start();
    
        animation blink = new animation();
        blink.start();
    
    
        this.setFocusable(true);
        this.addKeyListener(new KeyAdapter() {
    
          public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
    
              			right = true;
    
            } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
    
              			left = true;
    
    
            } else if (e.getKeyCode() == KeyEvent.VK_UP) {
    
              			up = true;
    
            } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
    
              			down = true;
    
            } else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
    
              for (int hopp = 1; hopp <= 20; hopp++) {
    
                yk = yk - 1;
    
                RotateImage.this.repaint();
    
                try {
                  Thread.sleep(50);
                } catch (InterruptedException f) {
                }
              }
    
            }
          }
    
          public void keyReleased(KeyEvent e) {
    
          	if(e.getKeyCode()==KeyEvent.VK_RIGHT)
          		right = false;
    
          	if(e.getKeyCode()==KeyEvent.VK_LEFT)
          		left = false;
    
          	if(e.getKeyCode()==KeyEvent.VK_UP)
          		up = false;
    
          	if(e.getKeyCode()==KeyEvent.VK_DOWN)
          		down = false;
    
          }
    
        });
    
    
      }
    
      class animation extends Thread
      {
      	public void run()
      	{
      		while(true)
      		{
      			character = Toolkit.getDefaultToolkit().getImage("BlobA1.png");
    
      			try {
                  Thread.sleep(2000);
                } catch (InterruptedException f) {
                }
    
                character = Toolkit.getDefaultToolkit().getImage("BlobA2.png");
    
                try {
                  Thread.sleep(2000);
                } catch (InterruptedException f) {
                }
    
    
    
      		}
    
      	}
    
      }
    
      class wasd extends Thread
      {
      	public void run()
      	{
    
    		while(true)
      		{
      			if(right==true)
      				xk = xk + 1;
    
      			if(left==true)
      				xk = xk - 1;
    
      			if(up==true)
      				yk = yk - 1;
    
      			if(down==true)
      				yk = yk + 1;
    
      			repaint();
    
      			try{
    				Thread.sleep(17);}
    			catch(InterruptedException f){
    			}
    
    
      		}
    
      	}
    
      }
    
      public void paintComponent(Graphics g) {
      	super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g; // Create a Java2D version of g.
        g2d.translate(xk, yk); // Translate the center of our coordinates.
        g2d.rotate(0); // Rotate the image by 1 radian.
        g2d.drawImage(character, xk, yk, this);
    
      }
    
    
      public static void main(String[] arg) {
    
        final JFrame frame = new JFrame("RotateImage");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1280, 720);
    
        RotateImage panel = new RotateImage();
        frame.setContentPane(panel);
        frame.setVisible(true);
        frame.setResizable(false);
    
      }
    }
    it is just a moveable blob which blinks, now however i want to get more pictures into the program, how do i do that? when i made an attempt i got other pictures into panel but they moved with the blob. i want them to be independant, any ideas?

  2. #2
    Gamewolf is offline Member
    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Default Re: pictures help

    ok i solved my problem, however i am now facing a new one. by using the rectangle function i want to have a kind of unit collision, how do i get the width and height from my picture?

Similar Threads

  1. Replies: 22
    Last Post: 07-25-2012, 10:51 PM
  2. Help with pictures
    By HelloWorld1234 in forum New To Java
    Replies: 10
    Last Post: 06-17-2012, 10:39 PM
  3. Help with pictures
    By HelloWorld1234 in forum New To Java
    Replies: 2
    Last Post: 06-14-2012, 01:40 AM
  4. PNG pictures
    By nhmllr in forum New To Java
    Replies: 15
    Last Post: 10-09-2011, 06:13 PM
  5. 3D pictures
    By mine0926 in forum Advanced Java
    Replies: 8
    Last Post: 10-27-2010, 02:43 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
  •