Reputation: 31
I am programming a game and when he go left the image should rotate left but it is not it is giving me and error:
Exception in thread "Thread-2" java.lang.NullPointerException
at Schoo.NewGame.TheRealGame.turnPlayer(TheRealGame.java:365)
at Schoo.NewGame.TheRealGame$2.run(TheRealGame.java:154)
at java.lang.Thread.run(Unknown Source)
Yes, i know that java.lang.NullPointerException means something is null but nothing should be null anyway here is the code on line 365:
g2.rotate(90.0+90.0+90.0);
line 154 just runs the method:
public static void turnPlayer(Direction d){
Graphics g = playerImage.getGraphics();
Graphics2D g2 = (Graphics2D) g;
if (d == Direction.LEFT){
if (p.getDirection() == Direction.LEFT){
return;
}
if (p.getDirection() == Direction.UP){
g2.rotate(90.0);
}
if (p.getDirection() == Direction.RIGHT){
g2.rotate(90.0+90.0);
}
if (p.getDirection() == Direction.DOWN){
g2.rotate(90.0+90.0+90.0);
}
p.setDirection(d);
playerImage.repaint();
}
if (d == Direction.UP){
if (p.getDirection() == Direction.UP){
return;
}
if (p.getDirection() == Direction.RIGHT){
g2.rotate(90.0);
}
if (p.getDirection() == Direction.DOWN){
g2.rotate(90.0+90.0);
}
if (p.getDirection() == Direction.LEFT){
g2.rotate(90.0+90.0+90.0);
}
p.setDirection(d);
playerImage.repaint();
}
if (d == Direction.RIGHT){
if (p.getDirection() == Direction.RIGHT){
return;
}
if (p.getDirection() == Direction.DOWN){
g2.rotate(90.0);
}
if (p.getDirection() == Direction.LEFT){
g2.rotate(90.0+90.0);
}
if (p.getDirection() == Direction.UP){
g2.rotate(90.0+90.0+90.0);
}
p.setDirection(d);
playerImage.repaint();
}
if (d == Direction.DOWN){
if (p.getDirection() == Direction.DOWN){
return;
}
if (p.getDirection() == Direction.LEFT){
g2.rotate(90.0);
}
if (p.getDirection() == Direction.UP){
g2.rotate(90.0+90.0);
}
if (p.getDirection() == Direction.RIGHT){
g2.rotate(90.0+90.0+90.0);
}
p.setDirection(d);
playerImage.repaint();
}
RepaintPlayer();
f.repaint();
playerImage.repaint();
}
and if you need the whole class:
public class TheRealGame{
private static boolean running = false;
private static boolean paused = false;
private static boolean right = false, left = false, up = false, down = false;
private static JFrame f;
private static ArrayList<JLabel> ae = new ArrayList<JLabel>();
private static Player p;
private static Playere pt;
private static JLabel playerImage;
private static boolean info = false;
private static JLabel iy, ix, im, in, iu;
public static void main(Playere playertype){
pt = playertype;
p = new Player(pt);
f = new JFrame();
f.setVisible(true);
f.setSize(700, 700);
f.setResizable(false);
f.setLocationRelativeTo(null);
f.addKeyListener(new KeyListener(){
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyChar() == KeyEvent.VK_W){
up = true;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyChar() == KeyEvent.VK_A){
left = true;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyChar() == KeyEvent.VK_S){
down = true;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyChar() == KeyEvent.VK_D){
right = true;
}
if (e.getKeyCode() == KeyEvent.VK_SPACE || e.getKeyCode() == KeyEvent.VK_ENTER){
p.attack();
}
if (e.getKeyCode() == KeyEvent.VK_F3){
if (info == true){
info = false;
iy.setVisible(false);
ix.setVisible(false);
im.setVisible(false);
in.setVisible(false);
iu.setVisible(false);
}else if (info == false){
info = true;
iy.setVisible(true);
ix.setVisible(true);
im.setVisible(true);
in.setVisible(true);
iu.setVisible(true);
}
}
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyChar() == KeyEvent.VK_W){
up = false;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyChar() == KeyEvent.VK_A){
left = false;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyChar() == KeyEvent.VK_S){
down = false;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyChar() == KeyEvent.VK_D){
right = false;
}
if (e.getKeyCode() == KeyEvent.VK_SPACE || e.getKeyCode() == KeyEvent.VK_ENTER){
p.attack();
}
}
@Override
public void keyTyped(KeyEvent e) {
}
});
iy = new JLabel();
ix = new JLabel();
im = new JLabel();
in = new JLabel();
iu = new JLabel();
iy.setLocation(0, 10);
ix.setLocation(0, 20);
im.setLocation(0, 30);
in.setLocation(0, 40);
iu.setLocation(0, 50);
iy.setBounds((int) iy.getLocation().getX(), (int) iy.getLocation().getY(), 100, 15);
ix.setBounds((int) ix.getLocation().getX(), (int) ix.getLocation().getY(), 100, 15);
im.setBounds((int) im.getLocation().getX(), (int) im.getLocation().getY(), 100, 15);
in.setBounds((int) in.getLocation().getX(), (int) in.getLocation().getY(), 100, 15);
iu.setBounds((int) iu.getLocation().getX(), (int) iu.getLocation().getY(), 100, 15);
f.add(ix);
f.add(iy);
f.add(im);
f.add(in);
f.add(iu);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("free play - tokyo ghoul");
Start();
p.Paint();
}
public static void resume(){
if (paused == false){
return;
}
}
public static void pause(){
if (paused == true){
return;
}
}
public static void Stop(){
if (running == false){
return;
}
running = false;
}
public static void Start(){
running = true;
new Thread(new Runnable(){
@Override
public void run() {
int last = 0, u = 0;
while (running == true){
if (paused != true){
if (up == true){
p.move(p.getX(), p.getY()-1);
if (p.getDirection() != Direction.UP){
turnPlayer(Direction.UP);
}
}
if (down == true){
p.move(p.getX(), p.getY()+1);
if (p.getDirection() != Direction.DOWN){
turnPlayer(Direction.DOWN);
}
}
if (left == true){
p.move(p.getX()-1, p.getY());
if (p.getDirection() != Direction.LEFT){
turnPlayer(Direction.LEFT);
}
}
if (right == true){
p.move(p.getX()+1, p.getY());
if (p.getDirection() != Direction.RIGHT){
turnPlayer(Direction.RIGHT);
}
}
if (info == true){
int l = 10-last;
iy.setText("y: "+p.getY());
ix.setText("x: "+p.getX());
im.setText("enemys: "+ae.size());
in.setText("next enemy: "+l);
iu.setText("Updated "+u+" times.");
RefreshInfo();
}
if (p.getY() == -337){
p.move(p.getX(), 337);
}
if (p.getY() == 337){
p.move(p.getX(), -337);
}
if (p.getX() == -349){
p.move(349, p.getY());
}
if (p.getX() == 349){
p.move(-349, p.getY());
}
RepaintAllLabels();
Enemy.UpdateAll();
if (info != true){
f.repaint();
}
if (last == 10){
Random r = new Random();
int x = 1+r.nextInt(2), y = 1+r.nextInt(2), distance = 1+r.nextInt(570), nx = 0, ny = 0;
if (x == 1){
}
last = 0;
}
last++;
u++;
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
public static void RefreshInfo(){
f.remove(iy);
f.remove(ix);
f.remove(im);
f.remove(in);
f.remove(iu);
f.add(ix);
f.add(iy);
f.add(im);
f.add(in);
f.add(iu);
iy.setLocation(0, 10);
ix.setLocation(0, 20);
im.setLocation(0, 30);
in.setLocation(0, 40);
iu.setLocation(0, 50);
iu.setBounds((int) iu.getLocation().getX(), (int) iu.getLocation().getY(), 100, 15);
iy.setBounds((int) iy.getLocation().getX(), (int) iy.getLocation().getY(), 100, 15);
ix.setBounds((int) ix.getLocation().getX(), (int) ix.getLocation().getY(), 100, 15);
im.setBounds((int) im.getLocation().getX(), (int) im.getLocation().getY(), 100, 15);
in.setBounds((int) in.getLocation().getX(), (int) in.getLocation().getY(), 100, 15);
f.repaint();
}
public static void UpdateAll(){
}
public static void Paint(JLabel imgs, int x, int y, File file){
ImageIcon img = new ImageIcon(file.getPath());
imgs.setBounds(x, y, img.getIconWidth(), img.getIconHeight());
imgs.setLocation(x, y);
imgs.setVisible(true);
f.add(imgs);
imgs.setVisible(true);
}
public static void Repaint(JLabel l){
f.remove(l);
l.setBounds((int)l.getLocation().getX(), (int)l.getLocation().getY(), l.getWidth(), l.getHeight());
f.add(l);
}
public static void addAE(JLabel l){
ae.add(l);
}
public static void RepaintAllLabels(){
for (int i = 0; i < ae.size(); i++){
Repaint(ae.get(i));
}
}
public static void MovePlayer(int x, int y){
playerImage.setLocation(x, y);
playerImage.repaint();
f.repaint();
}
public static void RepaintPlayer(){
if (p.isAttacking()){
if (pt == Playere.Kaneki){
ImageIcon img = new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/Kaneki_hit.png"));
playerImage = new JLabel(img);
}
if (pt == Playere.Touka){
ImageIcon img = new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/touka_hit.png"));
playerImage = new JLabel(img);
}
}else{
if (pt == Playere.Kaneki){
ImageIcon img = new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/Kaneki_walk.png"));
playerImage = new JLabel(img);
}
if (pt == Playere.Touka){
ImageIcon img = new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/touka_walk.png"));
playerImage = new JLabel(img);
}
}
playerImage.setVisible(false);
playerImage.repaint();
playerImage.setVisible(true);
playerImage.repaint();
playerImage.setVisible(true);
MovePlayer(p.getX(), p.getY());
f.repaint();
}
public static void paintplayer(){
if (pt == Playere.Kaneki){
playerImage = new JLabel(new ImageIcon(StartMenu.class.getResource("/schoo/NewGame/kaneki_walk.png")));
if (playerImage == null){
System.out.println("[ERROR THIS WAS THE ERROR THE WHOLE TIME!!");
return;
}
ImageIcon imgs = new ImageIcon(StartMenu.class.getResource("/schoo/NewGame/kaneki_walk.png"));
playerImage.setBounds(p.getX(), p.getY(), imgs.getIconWidth(), imgs.getIconHeight());
playerImage.setLocation(0, 0);
playerImage.setVisible(true);
f.add(playerImage);
playerImage.setVisible(true);
}
if (pt == Playere.Touka){
playerImage = new JLabel(new ImageIcon(StartMenu.class.getResource("/schoo/NewGame/touka_walk.png")));
playerImage.setBounds(p.getX(), p.getY(), new ImageIcon(StartMenu.class.getResource("/schoo/NewGame/touka_walk.png")).getIconWidth(), new ImageIcon(StartMenu.class.getResource("/schoo/NewGame/touka_walk.png")).getIconHeight());
playerImage.setLocation(p.getX(), p.getY());
playerImage.setVisible(true);
f.add(playerImage);
playerImage.setVisible(true);
}
}
public static void turnPlayer(Direction d){
Graphics g = playerImage.getGraphics();
Graphics2D g2 = (Graphics2D) g;
if (d == Direction.LEFT){
if (p.getDirection() == Direction.LEFT){
return;
}
if (p.getDirection() == Direction.UP){
g2.rotate(90.0);
}
if (p.getDirection() == Direction.RIGHT){
g2.rotate(90.0+90.0);
}
if (p.getDirection() == Direction.DOWN){
g2.rotate(90.0+90.0+90.0);
}
p.setDirection(d);
playerImage.repaint();
}
if (d == Direction.UP){
if (p.getDirection() == Direction.UP){
return;
}
if (p.getDirection() == Direction.RIGHT){
g2.rotate(90.0);
}
if (p.getDirection() == Direction.DOWN){
g2.rotate(90.0+90.0);
}
if (p.getDirection() == Direction.LEFT){
g2.rotate(90.0+90.0+90.0);
}
p.setDirection(d);
playerImage.repaint();
}
if (d == Direction.RIGHT){
if (p.getDirection() == Direction.RIGHT){
return;
}
if (p.getDirection() == Direction.DOWN){
g2.rotate(90.0);
}
if (p.getDirection() == Direction.LEFT){
g2.rotate(90.0+90.0);
}
if (p.getDirection() == Direction.UP){
g2.rotate(90.0+90.0+90.0);
}
p.setDirection(d);
playerImage.repaint();
}
if (d == Direction.DOWN){
if (p.getDirection() == Direction.DOWN){
return;
}
if (p.getDirection() == Direction.LEFT){
g2.rotate(90.0);
}
if (p.getDirection() == Direction.UP){
g2.rotate(90.0+90.0);
}
if (p.getDirection() == Direction.RIGHT){
g2.rotate(90.0+90.0+90.0);
}
p.setDirection(d);
playerImage.repaint();
}
RepaintPlayer();
f.repaint();
playerImage.repaint();
}
public static enum Direction{
LEFT, UP, RIGHT, DOWN;
}
}
Thank you for taking your time to help me!
Upvotes: 0
Views: 57
Reputation: 324108
You can use the Rotated Icon. This class does all the rotation logic for you. You just need to specify the degrees of rotation.
So the basic code to create the label would be:
RotatedIcon icon = new RotatedIcon( new ImageIcon(...) );
JLabel player = new JLabel( icon );
Then when you want to rotate the Icon you can use:
icon.setDegrees(...);
player.repaint();
Upvotes: 3
Reputation: 285403
That's not how you do Swing Graphics -- you shouldn't be calling getGraphics()
on a component since there is great risk that the object will either be null or non-functioning. Instead if this were my program, I'd get the actual image used, rotate it in all 4 directions (3 plus the original), make ImageIcons out of these images, store them in a collection, or better a HashMap<Direction, Icon>
, say called playerDirectionIconMap, and then use them without fear. Then to get the proper icon, I'd just call something like,
// this method should probably not be static
public void turnPlayer(Direction d){
playerImage.setIcon(playerDirectionIconMap.get(p.getDirection()));
// .... other code?
}
Note that the turnPlayer(...)
method should not be static as this suggests a broken design that needs to be fixed.
Also note that:
Math.PI / 2
.Upvotes: 1