Hi Everyone,

I'm doing a 2D PAcman in Java. Can anyone tell, why the screen gets mess up when I display the moster. I simply use set bound

enemy.setBounds(x1,x2,50,50);

How do I manage to include the monster without destroring the scren.

Many thanks.

ehm ... what do you mean with "mess up"?
the description is pretty vague, and without seeing any of the code, it's pretty hard to know what you're doing.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Pacmen extends JFrame implements KeyListener,ActionListener 
{
private Timer timer = new Timer(580000,this);
static int x=265,y=305,x1=50,x2=10,sw=0,swup=0,swdn=0,swlf=0,score=0,i=0, j=0, total=0, k=0,totscore=0;
static int counter=0,ran,ran2;

//static Integer[] points1 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

static int points[] = new int [249];

static JLabel pacman  = new JLabel(new ImageIcon("pacman.gif"));

static JLabel enemy = new JLabel(new ImageIcon("enemey.gif"));

static ImageIcon bg = new ImageIcon("empty_stage.jpg");

static ImageIcon test = new ImageIcon("enemy.jpg");

static JLabel coordinate = new JLabel();

Image img=Toolkit.getDefaultToolkit().createImage("enemey.gif");

Image hehe;


Container c = getContentPane();
Container zxc = getContentPane();
    public Pacmen() 
    {
   for (int i=0;i<=248;i++)
    {
        points[i]=0;

    }


   //

    setTitle("Pacman");
    setLayout(null);
    addKeyListener(this);
    setVisible(true);
    setResizable(false);
    setSize(555,614);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    c.add(pacman);



    enemy.setBounds(x1,x2,50,50);
    pacman.setBounds(x,y,30,30);

    c.setBackground(Color.black);



    }

    public void paint(Graphics g)
    {
        super.paint(g);

        bg.paintIcon(this,g,0,0);
        //g.drawImage(img,x1,x2,this);


    }
     public void paint(Graphics g,int x1)
    {
        super.paint(g);


        g.drawImage(img,x1,x2,this);


    }



    public void keyPressed(KeyEvent e)
    {

    String key = e.getKeyText(e.getKeyCode());

    if(key.equals("W"))
        {
        timer.start();  
        pacman.setIcon(new ImageIcon("pacman3.gif"));
        if (swup==0)
        {
        y=y-5;
        //score=score+1;
        }
        pacman.setBounds(x,y,30,30);

        }


   if(key.equals("D"))
   {
   timer.start();
   pacman.setIcon(new ImageIcon("pacman4.gif"));
   if (sw==0)
   {
      x=x+5;
      //score=score+1;
   }
   pacman.setBounds(x,y,30,30);
   }

   if(key.equals("A"))
   {
   timer.start();
   pacman.setIcon(new ImageIcon("pacman.gif"));
   if (swlf==0)
   {
     x=x-5;
     //score=score+1;
   }
   pacman.setBounds(x,y,30,30);
   }

   if(key.equals("S"))
   {
   timer.start();
   pacman.setIcon(new ImageIcon("pacman2.gif"));
   if (swdn==0) 
   {
   y=y+5;
   score=score+1;
   }
   pacman.setBounds(x,y,30,30);   
   }

   if(x>=530 && y>=245)
   {
   x=0;
   y=245;
   pacman.setIcon(new ImageIcon("pacman4.gif"));
   pacman.setBounds(x,y,30,30);
   }

  else if(x<=0 && y>=245)
   {
   x=530;
   y=245;
   pacman.setIcon(new ImageIcon("pacman.gif"));
   pacman.setBounds(x,y,30,30);

   }

Can you print screen what your screen looks like when it messes up please?

When I went to Uni we had to work in groups to create and modify a space invaders game in Java. If you're facing the same problem we did when we were trying to display the monsters for Space Invaders I might be able to help.

Many thanks for the reply. Attached is the messed up screen.

That's just the first part of a program, full of redundant (experimental?) code, and with some obvious mistakes (overriding paint, loading a new ImageIcon every time the user presses a key, repeatedly starting the same Timer...
but most important, it doesn't show any code for adding or moving the enemy, nor do you explain what you mean by "why the screen gets mess up when I display the moster".
So please help us to help ypou by cleaning up the code, fixing the indentation and layout, posting the whole thing, and explaining exactly what goes wrong.

Yes, this is experimental. Have not clean up yet.

OK, that's fair enough, but obviously it's hard for us to read the code when we don't know which bits are real and which aren't.
While you are cleaning up I would certainly recommend that
(1) you load all your ImageIcons just once, when the program starts and
(2) place your graphics in a new JPanel and override its paintComponent method rather than messing around with the painting of the whole window.

I shall do your suggestion, many thanks :)

Yes, this is experimental. Have not clean up yet.

then before (and/or with suggestion by @JamesCherrill +++)

  • JFrame by default never to react to Key Event

  • add JPanel to JFrame

  • override paintComponent for JPanel instead of paint for JFrame

  • use KeyBindings instead of KeyListener or setFocusable for JPanel

Many thanks for the suggestions, still modifying the code. Many thanks again :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.