Results 1 to 4 of 4
- 08-02-2012, 09:11 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 93
- Rep Power
- 0
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?
- 08-02-2012, 09:45 PM #2
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.
One that exists. Check the API.What method should I use for in the Sprite2 class for this to run?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 08-05-2012, 03:51 AM #3
Member
- Join Date
- Jul 2012
- Posts
- 93
- Rep Power
- 0
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.
- 08-05-2012, 05:53 AM #4
Member
- Join Date
- Jul 2012
- Posts
- 93
- Rep Power
- 0
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:
The issue that I'm having now is at line 38 which displaysJava 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); } } }
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
-
background static sprites active
By leggoeggo in forum Java GamingReplies: 7Last Post: 07-10-2012, 05:28 PM -
Drawing monochrome/alpha channel sprites in different colors
By kjkrum in forum Java GamingReplies: 1Last Post: 02-18-2012, 08:47 PM -
Variables not called properly and not displaying
By sandeep43 in forum Advanced JavaReplies: 7Last Post: 06-02-2011, 01:52 PM -
Sprites - My Game (Sort of)
By David M. in forum Java 2DReplies: 2Last Post: 04-25-2011, 11:00 PM -
Controls not displaying properly after redirecting page
By vikranth.kadya in forum JavaServer Faces (JSF)Replies: 0Last Post: 04-15-2011, 08:11 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks