Hi, I need some help please i could not show the balls,when my pacman is moving,i want my pacman will through balls when it's moving.but i could not show his balls,can you please help me what is wrong or missing in my code.
I appreciate someone can help me
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;
public class Mypacman{
public Mypacman(){
}
public void showUI(){
JFrame jframe = new JFrame();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.add(new Mypanel());
jframe.pack();
jframe.setVisible(true);
}
class Mypanel extends JPanel{
int xPos = 0, yPos = 35;
int arcMouth=300;
Balls ballc = new Balls();
List<Balls>ball = new ArrayList<Balls>();
public Mypanel(){
myTimer();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillArc(xPos,yPos,30,30,30,arcMouth);
}
public Dimension getPreferredSize() {
return new Dimension(250,200);
}
public void myTimer(){
java.util.Timer timer = new java.util.Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
updatePosition();
}
};
timer.scheduleAtFixedRate(timerTask, 0, 100);
}
public void updatePosition(){
xPos += 1;
yPos += 0;
if((xPos%2)==0){
arcMouth=360;
}
else{
arcMouth=300;
updateball();
ballc.moveBalls();
}
repaint();
}
public void updateball(){
for(Balls bb:ball){
ball.add(new Balls(xPos,yPos));
}
}
}
class Balls{
int x,y;
public Balls(){
}
public Balls(int x,int y){
this.x = x;
this.y = y;
}
public void setXball(int x ){
this.x=x;
}
public void setYball(int y){
this.y = y;
}
public int getXball(){
return x;
}
public int getYball(){
return y;
}
public void moveBalls(){
x+=2;
y+=2;
}
protected void drawbullets(Graphics g) {
g.setColor(Color.blue);
g.fillRect(getXball(), getYball(), 1, 1);
}
}
public static void main(String[]args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Mypacman pacman = new Mypacman();
pacman.showUI();
}
});
}
}