Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

problem in the game bomberman with the position of the icon bomb

843806Dec 9 2008 — edited Dec 10 2008
hi

in this code the player move with arrows in any where and when u press the space button bomb appear
but the bomb not appear in the position where the player stay and the player go to anthor place

please test my code to understande my problem
import javax.swing.JFrame;

public class Mainprogram extends JFrame {

    private MainBord mainbord;

    public Mainprogram() {
        mainbord = new MainBord();
        addKeyListener(mainbord);
        getContentPane().add(mainbord);
    }

    public static void main(String[] args) {
        JFrame frame = new Mainprogram();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 900);
        //frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MainBord extends JPanel implements KeyListener {

    private BufferedImage bord;
    private JLabel bomabr_label;
    private JPanel bombar_panel;
    private ImageIcon divider_icon,bombar_icon,bomb_icon;
    private Graphics g;
    private int x;
    private int y;
    private int dividerSize = 40;

    public MainBord() {
        drawimage();
        drawComponents();
    }

    private void drawComponents() {
        divider_icon = new ImageIcon(getClass().getResource("wall.gif"));
        bombar_icon = new ImageIcon(getClass().getResource("bombar.gif"));
        bomb_icon=new ImageIcon(getClass().getResource("bomb.gif"));
        x = (bord.getWidth() - (divider_icon.getIconWidth() + dividerSize) * 8) / 2;
        y = (bord.getHeight() - (divider_icon.getIconHeight() + dividerSize) * 8) / 2;
        for (int i = 0; i < 8; ++i) {
            for (int j = 0; j < 8; ++j) {
                divider_icon.paintIcon(this, g, (divider_icon.getIconWidth() + dividerSize) * i + x, (divider_icon.getIconHeight() + dividerSize) * j + y);
            }
        }

        bombar_panel = new JPanel();
        bomabr_label = new JLabel(bombar_icon);
        bombar_panel.add(bomabr_label);
    }

    private void drawimage() {

        bord = new BufferedImage(800, 900, BufferedImage.TYPE_INT_RGB);
        g = bord.getGraphics();
        g.setColor(Color.ORANGE);
        g.fillRect(0, 0, 1000, 1000);

    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(bord, 0, 0, this);
        bombar_icon.paintIcon(this, g, x - bombar_icon.getIconWidth(), y - bombar_icon.getIconHeight());
    }

    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_RIGHT:
                x += 5;
                break;
            case KeyEvent.VK_LEFT:
                x -= 5;
                break;
            case KeyEvent.VK_UP:
                y -= 5;
                break;
            case KeyEvent.VK_DOWN:
                y += 5;
                break;
            case KeyEvent.VK_SPACE:
                x=bombar_icon.getIconWidth();
                y=bombar_icon.getIconHeight();
                bomb_icon.paintIcon(this, g, x+dividerSize, y+dividerSize);
                break;
        }
        repaint();
    }

    public void keyReleased(KeyEvent e) {
    }
}
the problem in the last case in the switch

please refactor it to ucerstande how you do this .

the picture use in my code here:
http://www.4shared.com/file/75272822/8ecd434d/pictures.html

thanks
beshoy
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 7 2009
Added on Dec 9 2008
5 comments
123 views