Jump to content

Animation Help Needed

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Snig501

Snig501

    Newbie

  • Members
  • Pip
  • 9 posts
Hello,
I am trying to learn about 2D animation and I'm having a problem loading my image.
The program is supposed to display a black background and display star.png.

Here is my code

package star;


import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Image;


import java.awt.Toolkit;

import java.awt.event.ActionEvent;


import java.awt.event.ActionListener;


import javax.swing.ImageIcon;

import javax.swing.JPanel;

import javax.swing.Timer;


public class Board extends JPanel implements ActionListener {


    Image star;

    Timer timer;

    int x, y;


    public Board() {

        setBackground(Color.BLACK);


        ImageIcon ii =

            new ImageIcon(this.getClass().getResource("star.png"));

        star = ii.getImage();


        setDoubleBuffered(true);


        x = y = 10;

        timer = new Timer(25, this);

        timer.start();

    }



    public void paint(Graphics g) {

        super.paint(g);


        Graphics2D g2d = (Graphics2D)g;

        g2d.drawImage(star, x, y, this);

        Toolkit.getDefaultToolkit().sync();

        

    }



    public void actionPerformed(ActionEvent e) {

 

        x += 1;

        y += 1;


        if (y > 240) {

            y = -45;

            x = -45;

        }

        repaint();  

    }

}

When I use

public Board() {

        setBackground(Color.BLACK);


        ImageIcon ii =

            new ImageIcon(this.getClass().getResource("star.png"));

        star = ii.getImage();

it gives me an error and it won't run

and when I use

public Board() {

        setBackground(Color.BLACK);


        ImageIcon ii =

            new ImageIcon("star.png");

        star = ii.getImage();

all I get is my black background.

Any help will be very much appreciated.

Thanks.

#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
I can't check this frOm my phone but you can try overriding paintComponent instead of paint. That's the common way of drawing on a jpanel.

#3
Snig501

Snig501

    Newbie

  • Members
  • Pip
  • 9 posts
OK Thanks. I'll give it a shot :)

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I think it's easiest to use ImageIO.read(), to get your image.

#5
Eieio

Eieio

    Learning Programmer

  • Members
  • PipPipPip
  • 47 posts
Use the following code to get your image...

Change GIVEN_CLASS with the class name you use in the code... for example if you use Example.java, put Example.class instead of GIVEN_CLASS.class

Also you may need to use static in front of Image if the class is static.


public Image getImage(String location) {

        InputStream is = GIVEN_CLASS.class.getResourceAsStream(location);

        try {

            Image img = ImageIO.read(is);

            return img;

        } catch (Exception e) {

            e.printStackTrace();

        }

        return null;

}



#6
Snig501

Snig501

    Newbie

  • Members
  • Pip
  • 9 posts
Thanks I'll try this when I get a chance :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users