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

Thread: Drawing 2 images of a the same Sprite

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Drawing 2 images of a the same Sprite

    Im trying to draw 2 image of the asteroid sprite in my program. I can seem to get to display more than one asteroid


    import java.awt.*;
    import java.awt.image.*;
    import java.applet.*;
    import java.util.*;
    import java.net.*;

    public class SpriteTest extends Applet implements Runnable {
    int screenWidth = 640;
    int screenHeight = 480;

    //double buffer objects
    BufferedImage backbuffer;
    Graphics2D g2d;
    Sprite asteroid;
    Sprite spaceship;
    ImageEntity background;
    Thread gameloop;
    Random rand = new Random();

    public void init() {
    //create the back buffer for smooth graphics
    backbuffer = new BufferedImage(screenWidth, screenHeight,
    BufferedImage.TYPE_INT_RGB);
    g2d = backbuffer.createGraphics();

    //load the background
    background = new ImageEntity(this);
    background.load("bluespace.png");

    //load the asteroid sprite
    asteroid = new Sprite(this, g2d);
    asteroid.load("asteroid2.png");

    spaceship = new Sprite(this, g2d);
    spaceship.load("spaceship1.png");

    }

    public void start() {
    gameloop = new Thread(this);
    gameloop.start();
    }

    public void stop() {
    gameloop = null;
    }

    public void run() {
    Thread t = Thread.currentThread();
    while (t == gameloop) {
    try {
    Thread.sleep(30);
    }
    catch (InterruptedException e) {
    e.printStackTrace();
    }
    repaint();
    }
    }

    public void update(Graphics g) {
    //draw the background
    g2d.drawImage(background.getImage(), 0, 0, screenWidth-1, screenHeight-1, this);

    int width = screenWidth - asteroid.imageWidth() - 1;
    int height = screenHeight - asteroid.imageHeight() - 1;
    int width2 = screenWidth - spaceship.imageWidth() -2;
    int height2 = screenHeight - spaceship.imageHeight() -2;

    Point2D point = new Point2D(rand.nextInt(width), rand.nextInt(height));
    Point2D point2 = new Point2D(rand.nextInt(width2), rand.nextInt(height2));


    asteroid.setPosition(point);
    asteroid.transform();
    asteroid.draw();
    }
    spaceship.setPosition(point2);
    spaceship.transform();
    spaceship.draw();

    paint(g);
    }

    public void paint(Graphics g) {
    //draw the back buffer to the screen

    g.drawImage(backbuffer, 0, 0, this);


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Drawing 2 images of a the same Sprite

    Im trying to draw 2 image of the asteroid sprite
    Where do you draw the first one and then the second one?
    I don't see any comments telling me where to look for each draw being done.


    Also you didn't post all of the code. It ends in the middle of the paint() method.

Similar Threads

  1. Images (read/write, drawing)
    By helloworld922 in forum File Input/Output Tutorials
    Replies: 1
    Last Post: September 5th, 2011, 09:11 AM
  2. Sprite Animation with ArrayLists
    By FShounen in forum AWT / Java Swing
    Replies: 13
    Last Post: June 6th, 2011, 08:43 AM
  3. Alphabet from sprite sheet
    By Asido in forum Java Theory & Questions
    Replies: 2
    Last Post: September 18th, 2010, 11:49 AM
  4. problem with drawing images on JPanel
    By Asido in forum What's Wrong With My Code?
    Replies: 13
    Last Post: July 19th, 2010, 03:07 PM
  5. Problem with sprite rotation in graphics
    By Katotetu in forum Algorithms & Recursion
    Replies: 0
    Last Post: May 8th, 2009, 07:27 PM