Code works but nothing shows
I'm fooling around with java and am trying to make a simple pacman game.
but after changing some code(i dont know when) panman is invisible.
i cant find out why and have tried everything to my knowledge, i hope someone on this forum can tell me what im doing wrong.
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.Color;
public class spelApp extends JFrame
{
public spelApp()
{
pac p = new pac(30, 30);
setSize( 500, 500 );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setTitle( "pacman" );
setLocation( 200, 100 );
setBackground(Color.RED);
spelPaneel hoofdPaneel = new spelPaneel();
knoppenBalkPaneel knoppenPaneel = new knoppenBalkPaneel(hoofdPaneel);//knoppenBalkPaneel are the four buttons to move with
add(hoofdPaneel, BorderLayout.CENTER);
add(knoppenPaneel, BorderLayout.SOUTH);
setVisible( true );
}
public static void main( String[] args )
{
new spelApp();
}
}
Code:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import java.util.Random;
public class spelPaneel extends JPanel implements ActionListener
{
javax.swing.Timer timer = new javax.swing.Timer(500, this);//1000 is 1 second
Random Timer = new Random();
public pac[] pacs = new pac[3];
public spelPaneel()
{
pacs[0] = new pac (100,100);
pacs[1] = new pacMan (100,100);
pacs[2] = new pacVrouw (100,100);
timer.start();
}
public void paintComponent( Graphics g )
{
super.paintComponent( g );
g.setColor(new Color(124, 252, 0));//the area where pacs is allowed to move
g.fillRect(50, 50, 400, 400);
}
public void actionPerformed(ActionEvent e)
{
for(int i = 0; i<3; i++)
if (e.getSource() == timer)
{
if (pacs[i].get_y() > 100 && pacs[i].get_y() < 400)
{
pacs[i].set_y(pacs[i].get_y() + Timer.nextInt(40)-20);
System.out.println("between Y 100&400");
}
else
{
pacs[i].set_y(250);// put pac back in the middle
System.out.println("back to Y middle");
}
if (pacs[i].get_x() > 100 && pacs[i].get_x() < 400)
{
pacs[i].set_x(pacs[i].get_x() + Timer.nextInt(40)-20);
System.out.println("between X 100&400");
}
else
{
pacs[i].set_x(250);// put pac back in the middle
System.out.println("back to X middle");
}
System.out.println("repaint");
repaint();
}
}
}
Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class knoppenBalkPaneel extends JPanel implements ActionListener
{
private JButton leftButton, rightButton, upButton, downButton;
spelPaneel hoofdPaneel;
public knoppenBalkPaneel(spelPaneel hoofdPaneel)
{
this.hoofdPaneel = hoofdPaneel;
leftButton = new JButton("left");
leftButton.addActionListener(this);
add(leftButton);
rightButton = new JButton("right");
rightButton.addActionListener(this);
add(rightButton);
upButton = new JButton("up");
upButton.addActionListener(this);
add(upButton);
downButton = new JButton("down");
downButton.addActionListener(this);
add(downButton);
}
public void actionPerformed(ActionEvent e)
{
for(int i = 0; i<3; i++)
if (e.getSource() == leftButton)
{
hoofdPaneel.pacs[i].status_Left();
hoofdPaneel.repaint();
}
for(int i = 0; i<3; i++)
if (e.getSource() == rightButton)
{
hoofdPaneel.pacs[i].status_Right();
hoofdPaneel.pacs[i].set_y(500);
hoofdPaneel.repaint();
}
for(int i = 0; i<3; i++)
if (e.getSource() == upButton)
{
hoofdPaneel.pacs[i].status_Up();
hoofdPaneel.repaint();
}
for(int i = 0; i<3; i++)
if (e.getSource() == downButton)
{
hoofdPaneel.pacs[i].status_Down();
hoofdPaneel.repaint();
}
}
}
Code:
import java.awt.Color;
import java.awt.Graphics;
public class pac
{
protected final int LEFT = 1;
protected final int RIGHT = 2;
protected final int UP = 3;
protected final int DOWN = 4;
protected int x, y ;
protected int Status;
public pac( int X, int Y)
{
this.x = X;
this.y = Y;
Status = 1;
}
public void drawPac (Graphics g){
if (Status == LEFT)
{
g.setColor(Color.BLACK);
g.fillOval(x+100, y+100, 70, 70);
}
if (Status == RIGHT)
{
g.setColor(Color.BLACK);
g.fillOval(x+100, y+100, 70, 70);
}
if (Status == UP)
{
g.setColor(Color.BLACK);
g.fillOval(x+100, y+100, 70, 70);
}
if (Status == DOWN)
{
g.setColor(Color.BLACK);
g.fillOval(x+100, y+100, 70, 70);
}
}
public void status_Left()
{
setStatus1(LEFT);
}
public void setStatus1(int nieuw_Status)
{
Status = nieuw_Status;
}
public int getStatus1()
{
return Status;
}
public void status_Right()
{
setStatus1(RIGHT);
}
public void setStatus2(int nieuw_Status)
{
Status = nieuw_Status;
}
public int getStatus2()
{
return Status;
}
public void status_Up()
{
setStatus1(UP);
}
public void setStatus3(int nieuw_Status)
{
Status = nieuw_Status;
}
public int getStatus3()
{
return Status;
}
public void status_Down()
{
setStatus1(DOWN);
}
public void setStatus4(int nieuw_Status)
{
Status = nieuw_Status;
}
public int getStatus4()
{
return Status;
}
public void set_y(int nieuw_y)
{
y = nieuw_y;
}
public int get_y()
{
return y;
}
public void set_x(int nieuw_x)
{
x = nieuw_x;
}
public int get_x()
{
return x;
}
public void setStatus(int nieuw_Status)
{
Status = nieuw_Status;
}
public int getStatus()
{
return Status;
}
}
Re: Code works but nothing shows
Where in that code are you drawing the pacman?
Where are you calling the drawPac method?
Re: Code works but nothing shows
Thank you!
i had deletet the drawPac in the paincomponent in spelPaneel!
put pacs[0].drawPac(g); in there and now it works