I made a box from another class that extends JPanel, the thing is, the paintComponent() is not getting called after I do repaint() from another class. I don't know how repaint works and I am trying to practice some work.
Here's my JFrame:
package Main;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import KeyActions.keyActions;
import Objects.Piece;
public class createWindow extends JFrame {
public createWindow() {
super();
myPanel panel = new myPanel();
keyActions actions = new keyActions();
KeyListener key = new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
keyAction(e.getKeyCode());
}
#Override
public void keyReleased(KeyEvent e) {
}
public void keyAction(int keycode) {
switch (keycode) {
case KeyEvent.VK_UP:
actions.keyUP();
break;
case KeyEvent.VK_RIGHT:
break;
case KeyEvent.VK_DOWN:
break;
case KeyEvent.VK_LEFT:
break;
}
}
};
add(panel);
addKeyListener(key);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
}
}
Here's my code from the JPanel:
package Main;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import Objects.Piece;
public class myPanel extends JPanel {
//main character
public Piece innerLayer = new Piece(25, 25, 50, 50, new Color(255, 255, 0), "self");
public myPanel() {
super();
setBackground(Color.gray);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(innerLayer.color);
g2d.setStroke(new BasicStroke(15));
g2d.drawRect(innerLayer.x, innerLayer.y, innerLayer.width, innerLayer.height);
System.out.println("I am get called");
}
}
And this is my keyAction:
package KeyActions;
import Main.myPanel;
public class keyActions {
myPanel panel = new myPanel();
public void keyUP() {
int x = panel.innerLayer.getX();
int y = panel.innerLayer.getY();
y--;
panel.innerLayer.addPosVal(x, y);
panel.repaint();
System.out.println("keyUp is called");
}
}
After the keyUP is called, the repaint() didn't call the paintComponent(), it should println "I am get called" but it did not.
Here's my code from the Piece object:
package Objects;
import java.awt.*;
import java.util.Arrays;
import javax.swing.*;
public class Piece {
public int x;
public int y;
public int width;
public int height;
public Color color;
public String type;
public Piece(int x, int y, int width, int height, Color color, String type) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = color;
this.type = type;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void addPosVal(int x, int y) {
this.x += x;
this.y += y;
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
public int getWidth() {
return this.width;
}
public int getHeight() {
return this.height;
}
}
Related
I've added keybinding on one of my JPanels. The problem is this keybinding didn't do its "actionPerformed". Even though I put a sysout in the actionPerformed, nothing was outputted on the console. Can someone help me with this problem? I've already tried to disable my buttons, but still my keybinding doesn't work.
package project.fin;
import java.awt.*;
import java.io.*;
import java.util.List;
import java.awt.event.*;
import javax.swing.*;
//Panel for my game
public class GamePlayPanel extends JPanel{
private Image current;
private Baby bayi;
public GamePlayPanel(String img) {
Dimension size = new Dimension(1200, 500);
this.setPreferredSize(size);
this.setMaximumSize(size);
this.setMinimumSize(size);
this.setSize(size);
this.setLayout(null);
//An baby object
bayi = new Baby(100, 410, 5);
//this is where my keyBinding initialized
bayi.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0), "moveRight");
bayi.getActionMap().put("moveRight", new Move_it(1));
}
//this is the action class that i want to put in my keybinding
private class Move_it extends AbstractAction{
int code;
public Move_it(int code) {
this.code=code;
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("test\n");
if (this.code==1) {
bayi.MoveRight();
}
repaint();
}
}
//To draw my baby
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
bayi.draw(g);
}
}
This is my baby class:
package project.fin;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import java.awt.event.*;
public class Baby extends JComponent{
float x, y;
float speed;
Image current;
private List <Image> ImgPool;
private int Current;
public Baby(float x, float y, float speed) {
// TODO Auto-generated constructor stub
this.x = x;
this.y = y;
this.speed = speed;
ImgPool = new ArrayList<Image>();
//These are just some images that i use to build my moving baby
ImgPool.add(new ImageIcon("baby1_50.png").getImage());
ImgPool.add(new ImageIcon("baby2_50.png").getImage());
ImgPool.add(new ImageIcon("baby1_50.png").getImage());
ImgPool.add(new ImageIcon("baby3_50.png").getImage());
this.current = ImgPool.get(0);
this.Current = 0;
}
//The action that i want my baby to do when a key is pressed
public void MoveRight() {
if (x>600) return;
this.x+=speed;
if (this.Current==3)this.Current=0;
else
this.Current++;
this.current = this.ImgPool.get(Current);
}
public void draw(Graphics g) {
g.drawImage(this.current, (int)this.x, (int)this.y, null);
}
}
Baby isn't attached to the component hierarchy and therefore won't receive any key events. In fact, the design doesn't make sense. There's no need for Bady to extend from JPanel at all.
Instead, make use of the GamePlayPanel directly
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.*;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new GamePlayPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class GamePlayPanel extends JPanel {
private Baby bayi;
public GamePlayPanel() {
//An baby object
bayi = new Baby(100, 410, 5);
//this is where my keyBinding initialized
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "moveRight");
getActionMap().put("moveRight", new Move_it(1));
}
#Override
public Dimension getPreferredSize() {
return new Dimension(1200, 500);
}
//this is the action class that i want to put in my keybinding
private class Move_it extends AbstractAction {
int code;
public Move_it(int code) {
this.code = code;
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("test\n");
if (this.code == 1) {
bayi.moveRight();
}
repaint();
}
}
//To draw my baby
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
bayi.draw(g);
}
}
public class Baby {
float x, y;
float speed;
Image current;
private List<Image> ImgPool;
private int Current;
public Baby(float x, float y, float speed) {
// TODO Auto-generated constructor stub
this.x = x;
this.y = y;
this.speed = speed;
ImgPool = new ArrayList<Image>();
//These are just some images that i use to build my moving baby
ImgPool.add(new ImageIcon("baby1_50.png").getImage());
ImgPool.add(new ImageIcon("baby2_50.png").getImage());
ImgPool.add(new ImageIcon("baby1_50.png").getImage());
ImgPool.add(new ImageIcon("baby3_50.png").getImage());
this.current = ImgPool.get(0);
this.Current = 0;
}
//The action that i want my baby to do when a key is pressed
public void moveRight() {
if (x > 600) {
return;
}
this.x += speed;
if (this.Current == 3) {
this.Current = 0;
} else {
this.Current++;
}
this.current = this.ImgPool.get(Current);
}
public void draw(Graphics g) {
g.drawImage(this.current, (int) this.x, (int) this.y, null);
}
}
}
I made a big aqua square, but somehow, a little gray square appeared in my JPanel, it looks like this.
Theres the little gray square there in the middle and I didn't draw it. How do I avoid it? Heres my code:
my Main:
package Main;
public class main {
public static void main(String[] args) {
createWindow window = new createWindow();
}
}
myFrame and myPanel:
package Main;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import Objects.Piece;
public class createWindow extends JFrame {
public myPanel panel;
public createWindow() {
super();
panel = new myPanel();
KeyListener key = new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
keyAction(e.getKeyCode());
}
public void keyAction(int keycode) {
switch (keycode) {
case KeyEvent.VK_UP:
System.out.println("Up");
break;
case KeyEvent.VK_RIGHT:
System.out.println("Right");
break;
case KeyEvent.VK_DOWN:
System.out.println("Down");
break;
case KeyEvent.VK_LEFT:
System.out.println("Left");
break;
}
}
};
pack();
add(panel);
addKeyListener(key);
setSize(800, 450);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
}
}
class myPanel extends JPanel {
public Piece firstP = new Piece(100, 100, 100, 100, new Color(50, 255, 255), "self");
myPanel() {
super();
setBackground(Color.gray);
add(firstP);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(firstP.color);
g2d.fillRect(firstP.x,firstP.y, firstP.width, firstP.height);
}
}
myPiece:
package Objects;
import java.awt.*;
import javax.swing.*;
public class Piece extends JPanel {
public int x;
public int y;
public int width;
public int height;
public Color color;
public String type;
public Piece(int x, int y, int width, int height, Color color, String type) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = color;
this.type = type;
}
}
Can someone fix my code? I am new to java paint and I can't see the bug here...
Here I have two classes, one including main function. I want to draw a rectangle which moves automatically. But the starting point of rectangle is not the same as the point i clicked with mouse. I could not figure out this problem. Can you help me?
This is the first class including main function
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;
public class BuyuyenSuDamlalari extends JPanel implements ActionListener {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(600,400);
BuyuyenSuDamlalari bsd1 = new BuyuyenSuDamlalari();
frame.setContentPane(bsd1);
BuyuyenSuDamlalariClickListener bscl = new BuyuyenSuDamlalariClickListener(bsd1);
bsd1.addMouseListener(bscl);
frame.setResizable(false);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
int x;
int y;
int radius;
int click;
public BuyuyenSuDamlalari() {
super();
setFocusable(true);
Timer zaman = new Timer(40, this); // bir saniyede 25 resim oluyor
zaman.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (click>0) {
g.drawRect(x, y, radius, radius);
}
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void actionPerformed(ActionEvent arg0) {
radius++;
repaint();
}
public void setClick(int click) {
this.click = click;
}
}
This is the second class wihich includes motionListener
import java.awt.event.*;
public class BuyuyenSuDamlalariClickListener extends MouseAdapter {
private BuyuyenSuDamlalari bsd = new BuyuyenSuDamlalari();
private int x;
private int y;
public BuyuyenSuDamlalariClickListener(BuyuyenSuDamlalari bsd) {
super();
this.bsd = bsd;
}
public void mousePressed(MouseEvent e) {
if (e.getClickCount()>0) {
bsd.setX(e.getX()-25);
bsd.setY(e.getY()-25);
}
bsd.setClick(1);
}
}
Pong.Java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class Pong {
private static final int WIDTH = 900, HEIGHT = 700;
JFrame win = new JFrame();
Paddle paddleOne = new Paddle(1);
Paddle paddleTwo = new Paddle(2);
Graphics g;
Pong(){
init();
}
void init(){
win.setTitle("PONG");
win.setSize(WIDTH, HEIGHT);
win.addKeyListener(keyListener);
win.setLocationRelativeTo(null);
win.getContentPane().setBackground(Color.black);
win.setVisible(true);
win.getContentPane().validate();
win.repaint();
}
public void paintComponent(Graphics g){
g.setColor(Color.white);
g.fillRect(paddleOne.getX(), paddleOne.getY(), paddleOne.getHEIGHT(), paddleOne.getWIDTH());
System.out.println("drawn");
}
KeyListener keyListener = new KeyListener() {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch(key){
case 87:
System.out.println("Player 1 Up");
break;
case 83:
System.out.println("Player 1 Down");
break;
case 38:
System.out.println("Player 2 Up");
break;
case 40:
System.out.println("Player 2 Down");
break;
}
win.getContentPane().validate();
win.repaint();
}
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
};
public static void main(String[] args) {
Pong p = new Pong();
}
}
Paddle.Java
public class Paddle{
private int WIDTH = 50, HEIGHT = 150, X, Y;
int playerNum;
Paddle(int playerNum){
if(playerNum == 1){
X = 10;
Y = 10;
}else if (playerNum == 2){
X = 500;
Y = 10;
}
}
public void setX(int x){
X = x;
}
public void setY(int y){
Y = y;
}
public int getWIDTH() {
return WIDTH;
}
public int getHEIGHT() {
return HEIGHT;
}
public int getX() {
return X;
}
public int getY() {
return Y;
}
}
I'm relatively new to Java programming, or more specifically Awt & Swing, what my question is, why isn't my rectangle drawing? Any help is appreciated. Thank you so much!
In order for something to be painted within in Swing, it first must extend from something Swing know's how to paint, this commonly means a JComponent (or more typically a JPanel).
Then you can override one of the paint methods, which is called by the painting subsystem, in this case, it's generally preferred to override paintComponent, but don't forget to call super.paintComponent before you do any custom painting, or you're setting yourself up for some weird and generally unpredictable painting issues.
Take a look at Painting in AWT and Swing and Performing Custom Painting for more details.
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Pongo {
public static void main(String[] args) {
new Pongo();
}
public Pongo() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new PongPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class PongPane extends JPanel {
private static final int WIDTH = 900, HEIGHT = 700;
Paddle paddleOne = new Paddle(1);
Paddle paddleTwo = new Paddle(2);
public PongPane() {
setBackground(Color.BLACK);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.white);
g.fillRect(paddleOne.getX(), paddleOne.getY(), paddleOne.getHEIGHT(), paddleOne.getWIDTH());
//System.out.println("drawn"); //Should not put something here which may overhead.
}
}
public class Paddle {
private int WIDTH = 50, HEIGHT = 150, X, Y;
int playerNum;
Paddle(int playerNum) {
if (playerNum == 1) {
X = 10;
Y = 10;
} else if (playerNum == 2) {
X = 500;
Y = 10;
}
}
public void setX(int x) {
X = x;
}
public void setY(int y) {
Y = y;
}
public int getWIDTH() {
return WIDTH;
}
public int getHEIGHT() {
return HEIGHT;
}
public int getX() {
return X;
}
public int getY() {
return Y;
}
}
}
I would also discourage the use of KeyListener within this context and inside advice the use of the key bindings API, it doesn't suffer from the same focus related issues that KeyListener does. See How to Use Key Bindings for more details
You need to overriding paintComponents to draw.
Here is your Pong.java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Pong {
private static final int WIDTH = 900, HEIGHT = 700;
JFrame win = new JFrame();
Paddle paddleOne = new Paddle(1);
Paddle paddleTwo = new Paddle(2);
Graphics g;
Pong() {
init();
}
void init() {
win.setTitle("PONG");
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setSize(WIDTH, HEIGHT);
win.addKeyListener(keyListener);
win.setLocationRelativeTo(null);
win.add(new Panel(paddleOne));
win.getContentPane().setBackground(Color.black);
win.setVisible(true);
win.getContentPane().validate();
win.repaint();
}
KeyListener keyListener = new KeyListener() {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case 87:
System.out.println("Player 1 Up");
break;
case 83:
System.out.println("Player 1 Down");
break;
case 38:
System.out.println("Player 2 Up");
break;
case 40:
System.out.println("Player 2 Down");
break;
}
win.getContentPane().validate();
win.repaint();
}
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
};
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Pong();
}
});
}
}
Here Panel.java in where paintComponent overridden.
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Panel extends JPanel{
private Paddle paddleOne;
public Panel(Paddle pdl) {
paddleOne = pdl;
}
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.white);
g.fillRect(paddleOne.getX(), paddleOne.getY(), paddleOne.getHEIGHT(), paddleOne.getWIDTH());
//System.out.println("drawn"); //Should not put something here which may overhead.
}
}
So i am making a small game called dodge were rectangles fall from the "sky" and you have to do dodge them. As i ran the game this error message just popped up and i have no clue why. Also the screen just goes white whenever i run the program.
I cannot figure this out, Ive searched for hours in my code cant figure out why. Please Halp
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at dodge.Enemy.move(Enemy.java:22)
at dodge.Enemy.draw(Enemy.java:32)
at dodge.EnemyManager.draw(EnemyManager.java:34)
at dodge.Dodge.paint(Dodge.java:38)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5226)
at
Sorry thats long. But here is my code.
Dodge.java
package dodge;
import java.awt.event.KeyListener;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.event.*;
public class Dodge extends JPanel implements KeyListener{
private Player player;
private Stage stage;
private EnemyManager manager;
public Dodge(){
setSize(new Dimension(800, 600));
setPreferredSize(new Dimension(800, 600));
addKeyListener(this);
setFocusable(true);
stage = new Stage();
player = new Player(this, 200, 200);
manager = new EnemyManager(this, 10);
}
#Override
public void update(Graphics g){
paint(g);
}
#Override
public void paint(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
stage.draw(g);
player.draw(g);
manager.draw(g);
g.dispose();
repaint();
}
#Override
public void keyReleased(KeyEvent e){
player.setXD(0);
player.setYD(0);
}
#Override
public void keyTyped(KeyEvent e){
}
public Stage getStage(){
return stage;
}
#Override
public void keyPressed(KeyEvent e){
int c = e.getKeyCode();
if(c == KeyEvent.VK_W){
}
if(c ==KeyEvent.VK_A){
player.setXD(-1);
}
if(c == KeyEvent.VK_S){
}
if(c ==KeyEvent.VK_D){
player.setXD(1);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Dodge the Rectangles");
frame.add(new Dodge());
frame.pack();
frame.setResizable(false);
frame.setSize(new Dimension(800, 600));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
EnemyManager.java
package dodge;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
public class EnemyManager {
private int amount;
private List<Enemy> enemies = new ArrayList<Enemy>();
private Dodge instance;
public EnemyManager(Dodge instance, int a){
this.amount = a;
spawn();
this.instance = instance;
}
private void spawn(){
Random random = new Random();
int ss = enemies.size();
if(ss<amount){
for(int i = 0; i < amount - ss; i++){
enemies.add(new Enemy(instance, random.nextInt(778), 10));
}
}else if(ss>amount){
for(int i = 0; i < ss - amount; i++){
enemies.remove(i);
}
}
}
public void draw(Graphics g){
for(Enemy e : enemies) e.draw(g);
}
}
Enemy.java
package dodge;
import java.awt.*;
public class Enemy extends Entity{
private Rectangle hitbox;
private int ix, iy;
private boolean dead = false;
private Dodge instance;
public Enemy(Dodge instance, int x, int y){
super(x, y);
this.instance = instance;
hitbox = new Rectangle (x, y, 32, 32);
ix = 0;
iy = 1;
}
private void move(){
if(instance.getStage().isCollided(hitbox)){
iy = 0;
dead = true;
}
hitbox.x += ix;
hitbox.y +=iy;
}
private boolean isDead() {return dead;}
#Override
public void draw(Graphics g){
move();
g.setColor(Color.CYAN);
g.fillRect(hitbox.x, hitbox.y, hitbox.width, hitbox.height);
}
}
Entity.java
package dodge;
import java.awt.*;
public abstract class Entity {
protected int x, y, w, h;
protected boolean removed = false;
public Entity(int x, int y){
this.x = x;
this.y = y;
}
public void draw(Graphics g){
}
public int getX() {return x;}
public int getY(){return y;}
public int getW() {return w;}
public int getH(){return h;}
}
Player.java
package dodge;
import java.awt.*;
public class Player extends Entity{
private int xd, yd;
private Dodge instance;
private Rectangle hitbox;
public Player(Dodge instance, int x , int y){
super(x, y);
this.instance = instance;
w = 16; h = 16;
hitbox = new Rectangle(x, y, w, h);
}
public void draw(Graphics g){
move();
if(!instance.getStage().isCollided(hitbox)){
yd = 1;
}else yd = 0;
g.setColor(Color.ORANGE);
g.fillOval(hitbox.x, hitbox.y, hitbox.width, hitbox.height);
}
private void move(){
hitbox.x += xd;
hitbox.y +=yd;
}
public void setXD(int value){
xd = value;
}
public void setYD(int value){
yd = value;
}
}
Stage.java
package dodge;
import java.awt.*;
public class Stage {
private Rectangle platform = new Rectangle(0, 540, 800, 100);
public Stage(){
}
public boolean isCollided(Rectangle entity){
return platform.intersects(entity);
}
public void draw(Graphics g){
g.setColor(Color.WHITE);
g.fillRect(platform.x, platform.y, platform.width, platform.height);
}
}
I hope that wasnt too long. Thanks
Read the stacktrace. Either Enemy.instance or Enemy.instance.getStage() is null.
Also the screen just goes white whenever i run the program.
Don't override update() and paint(). That is old code used in AWT and is NOT the way custom painting is done in Swing. In Swing you simply override the paintComponent() method of the JPanel and don't forget to invoke super.paintComponent(). Read the section from the Swing tutorial on Custom Painting for more information and examples.
Don't invoke repaint() in a painting method. That will cause an infinite loop. The is not the way to do animation. Use a Swing Timer or a separate Thread to schedule the animation.