Results 1 to 4 of 4
  1. #1
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default need help displaying two sprites properly

    My program consists of 4 classes: a board class, two sprite classes, and a main method. Everything works so far except I when I add the second sprite image to the paint() method in the board class:

    public void paint(Graphics g) {
    super.paint(g);

    Graphics2D g2d = (Graphics2D) g;
    g2d.drawImage(sprite1.getImage(), sprite1.getX(), sprite1.getY(), this);
    Toolkit.getDefaultToolkit().sync();
    g.dispose();
    g2d.drawImage(sprite2, 400, 299, null);
    }

    The program says there is no suitable method found for the found for drawImage(package.Sprite2,int,int<nulltype>).

    What method should I use for in the Sprite2 class for this to run?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: need help displaying two sprites properly

    First off, don't dispose() a Graphics that you didn't create in your code. And don't try to use a Graphics after you've dispose()d it.

    Second, is this antiquated AWT code or is it Swing?

    Also, you're not using any Graphics2D methods, so why the cast?

    To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.

    What method should I use for in the Sprite2 class for this to run?
    One that exists. Check the API.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: need help displaying two sprites properly

    Ok, this code works fine for sprite1:

    public void paint(Graphics g) {
    super.paint(g);

    g2d = g;

    g2d.drawImage(sprite1.getImage(), sprite1.getX(), sprite1.getY(), this);
    Toolkit.getDefaultToolkit().sync();

    }

    I believe this is related to java.swing.ImageIcon;

    What I want to do is display another sprite within the paint method of the board class on the same board.

    Here is the sprite2 class:

    Java Code:
    private String sprite2 = "sprite2.png";
    
        
        private int x;
        private int y;
        private Image image;
        
        
        public Cross() {
            ImageIcon ii = new ImageIcon(this.getClass().getResource(sprite2));
            image = ii.getImage();
            x = 40;
            y = 60;
        }
        
        
        public int getX() {
            return x;
        }
        
        public int getY() {
            return y;
        }
        
        public Image getImage() {
            return image;
        }
    }
    Last edited by jwl; 08-05-2012 at 05:01 AM.

  4. #4
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: need help displaying two sprites properly

    Ok so the sprite2 class above looks almost identical to the sprite1 class except there is a move method that is applied to the sprite1 class to move the sprite with the arrow keys. I want to keep sprite2 completely stationary. Here is the entire code for the board class:

    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    
    public class Board extends JPanel implements ActionListener {
    
        private Timer timer;
        private Character sprite1;
        private Cross sprite2;
        private Graphics g2d;
       
        public Board() {
           
            addKeyListener(new TAdapter());
            setFocusable(true);
            setBackground(Color.BLACK);
            setDoubleBuffered(true);
    
          sprite1 = new Character();
            
            timer = new Timer(5, this);
            timer.start();
        }
        
        public void paint(Graphics g) {
            super.paint(g);
    
             g2d =  g;
           
          g2d.drawImage(sprite1.getImage(), sprite1.getX(), sprite1.getY(), this);  
          g2d.drawImage(sprite2.getImage(), sprite2.getX(), sprite2.getY(), this);
            Toolkit.getDefaultToolkit().sync();
           
        }
    
        public void actionPerformed(ActionEvent e) {
            sprite1.move();
            repaint();  
        }
    
    
        private class TAdapter extends KeyAdapter {
    
            public void keyReleased(KeyEvent e) {
               sprite1.keyReleased(e);
            }
    
            public void keyPressed(KeyEvent e) {
                sprite1.keyPressed(e);
            }
        }
     }
    The issue that I'm having now is at line 38 which displays


    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at christian.Board.paint(Board.java:38)

    when I run the code.


    Where I'm confused is both sprite classes have almost the exact same getX(), getY(), and ImageIcon applications.

Similar Threads

  1. background static sprites active
    By leggoeggo in forum Java Gaming
    Replies: 7
    Last Post: 07-10-2012, 05:28 PM
  2. Replies: 1
    Last Post: 02-18-2012, 08:47 PM
  3. Variables not called properly and not displaying
    By sandeep43 in forum Advanced Java
    Replies: 7
    Last Post: 06-02-2011, 01:52 PM
  4. Sprites - My Game (Sort of)
    By David M. in forum Java 2D
    Replies: 2
    Last Post: 04-25-2011, 11:00 PM
  5. Controls not displaying properly after redirecting page
    By vikranth.kadya in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 04-15-2011, 08:11 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
  •