Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default the Cells don't change!

    hi
    i'm working on my project and i faced to a bad problem!
    My game is Snake&Ladders and i have a mapeditor that creates my map and class game that load that map..
    i used Serialization for saving map object and loading and i give to every trap or power a int and in game class i have a recourceManager function that according to that cell type (Trap or power) return a new cell ...

    when new objects are created , i can't change my cell in map ; like put player icon in it's cell ..

    Java Code:
    package game;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
    
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    
    import player.Player;
    import player.PlayerList;
    import cell.Cell;
    import cell.DeathTrap;
    import cell.DisableTrap;
    import cell.MoveBack;
    import cell.NormalCell;
    import cell.RandomBackwardTrap;
    import cell.ReverseTrap;
    import cell.SendToNearestPower;
    import cell.SlowTrapPower;
    import cell.SpeedPower;
    
    public class Game extends JFrame {
    	Map mp;
    	PlayerList list;
    	private int roll;
    	int count=0;
    	int line =0;
    	Font font = new Font("Comic Sans MS", Font.BOLD, 18);
    	Icon pl1 = new ImageIcon(getClass().getResource("AttackPl2.png"));
    	Icon pl2 = new ImageIcon(getClass().getResource("Defend3.png"));
    	Icon pl3 = new ImageIcon(getClass().getResource("Normal.jpg"));
    	Icon pl4 = new ImageIcon(getClass().getResource("Mordekhor2.jpg"));
    	JLabel showdice;
    	JLabel showTurn;
    	public JLabel status;
    	public JLabel gameStatus;
    	
    	
    	public Game(Map mp) {
    		super("Snake&Ladders");
    		this.mp = mp;
    		this.setVisible(true);
    		this.setLayout(null);
    		this.setBounds(600, 50, 1000, 800);
    		
    		for (int i = 0; i < mp.getRow(); i++) {
    			for (int j = 0; j < mp.getCol(); j++) {
    				this.add(mp.getCell()[i][j]);
    				mp.getCell()[i][j].removeMouseListener(mp.getCell()[i][j].mouse);
    				int type = mp.getCell()[i][j].type;
    				mp.getCell()[i][j] = resourceManager(i, j,type);
    			}
    		}
    
    		
    		
    		for (int j = mp.getCol() -1 ; j >= 0; j--) {
    			if (line % 2 == 0) {
    				for (int i = 0; i < mp.getRow(); i++) {
    					++count;
    					mp.getCell()[i][j].setCellNumber(count);
    				}
    			}else{
    				for (int i = mp.getRow() - 1; i >= 0; i--) {
    					++count;
    					mp.getCell()[i][j].setCellNumber(count);
    				}
    			}
    			++line;
    		}
    		
    		
    		repaint();
    		String[] Types = { "MordeKhor", "Attacker", "Defender", "Normal" };
    		String number = JOptionPane
    				.showInputDialog("Enter the Number of Players");
    		char[] playertype = new char[Integer.parseInt(number)];
    		String[] names = new String[Integer.parseInt(number)];
    		for (int i = 0; i < Integer.parseInt(number); i++) {
    			names[i] = JOptionPane.showInputDialog("Enter the Name of Player");
    			playertype[i] = ((String) JOptionPane.showInputDialog(null,"Select The Type Of Player", "Player Type",JOptionPane.PLAIN_MESSAGE, null, Types, null)).charAt(0);
    			
    
    			JLabel text = new JLabel(names[i]);
    			text.setBounds(780, 310 + (i * 80), 60, 60);
    			text.setFont(font);
    			add(text);
    
    			
    			showTurn = new JLabel();
    			showTurn.setBounds(800, 40, 170, 60);
    			showTurn.setFont(font);
    			this.add(showTurn);
    			
    			
    			showdice = new JLabel();
    			showdice.setBounds(800, 15, 120, 60);
    			showdice.setFont(font);
    			this.add(showdice);
    			
    			
    			status = new JLabel();
    			status.setBounds(750, 550, 170, 60);
    			status.setFont(font);
    			this.add(status);
    			
    			gameStatus = new JLabel();
    			gameStatus.setBounds(750, 650, 170, 60);
    			gameStatus.setFont(font);
    			this.add(gameStatus);
    			
    			switch (playertype[i]) {
    			case 'A':
    				JLabel jb1 = new JLabel();
    				jb1.setBounds(850, 290 + (i * 110), 150, 100);
    				
    				jb1.setIcon(pl1);
    				this.add(jb1);
    				break;
    			case 'D':
    				JLabel jb2 = new JLabel();
    				jb2.setBounds(850, 290 + (i * 110), 150, 100);
    				
    				jb2.setIcon(pl2);
    				this.add(jb2);
    				break;
    			case 'N':
    				JLabel jb3 = new JLabel();
    				jb3.setBounds(850, 290 + (i * 110), 150, 100);
    				
    				jb3.setIcon(pl3);
    				this.add(jb3);
    				break;
    			case 'M':
    				JLabel jb4 = new JLabel();
    				jb4.setBounds(850, 290 + (i * 110), 150, 100);
    				jb4.setIcon(pl4);
    				this.add(jb4);
    				break;
    			}
    			Icon[] icons = {pl1,pl2,pl3,pl4};
    			list = new PlayerList(names, playertype, roll , mp , icons);
    
    		}
    		
    		
    
    		Icon dc = new ImageIcon(getClass().getResource("Dice1.png"));
    		JButton dice = new JButton("Dice", dc);
    		dice.setBounds(800, 100, 120, 70);
    		this.add(dice);
    		dice.addActionListener(new ActionListener() {
    			Random rand = new Random();
    
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				roll = (rand.nextInt(6) + 1);
    				showdice.setText("Dice: " + roll);
    				showTurn.setText("Turn: " + list.getPlayers()[list.getTurn()-1].getName());
    				play();
    			}
    
    		});
    
    		JButton mdice = new JButton("Manual Dice");
    		mdice.setBounds(800, 180, 120, 70);
    		this.add(mdice);
    		
    		mdice.addActionListener(new ActionListener() {
    
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    
    				String r = JOptionPane.showInputDialog("Enter A Dice 1-6");
    				roll = (Integer.parseInt(r));
    				showdice.setText("Dice: " + roll);
    				showTurn.setText("Turn: " + list.getPlayers()[list.getTurn()-1].getName());
    				play();
    
    			}
    
    		});
    
    
    		this.repaint();
    	}
    
    	public int getRoll() {
    		return roll;
    	}
    
    	public Cell resourceManager(int x, int y, int type) {
    		if (type == 1)
    			return new DeathTrap(x, y , this);
    		else if (type == 2)
    			return new SlowTrapPower(x, y , this);
    		else if (type == 3)
    			return new DisableTrap(x, y , this);
    		else if (type == 4)
    			return new RandomBackwardTrap(x, y, this);
    		else if (type == 5)
    			return new ReverseTrap(x, y, this);
    		else if (type == 6)
    			return new SpeedPower(x, y, this);
    		else if (type == 7)
    			return new MoveBack(x, y, this);
    		else if (type == 8)
    			return new SendToNearestPower(x, y, this);
    		else
    			return new NormalCell(x, y);
    	}
    
    	public void play() {
    		this.list.getPlayers()[list.getTurn()-1].move(roll);
    		Player player = this.list.getPlayers()[list.getTurn()-1];
    		this.repaint();
    		this.status.setText(player.getName() + " Dar " + player.getPlace() + " Ast.");
    		list.changeTurn();
    		this.repaint();
    	}
    
    }

  2. #2
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

Similar Threads

  1. Replies: 12
    Last Post: 04-20-2012, 05:43 PM
  2. Replies: 2
    Last Post: 12-20-2010, 09:37 PM
  3. Replies: 5
    Last Post: 12-20-2010, 08:31 PM
  4. Can I change the border of a JPanel cell?
    By ryuzog in forum New To Java
    Replies: 2
    Last Post: 10-10-2010, 06:05 AM
  5. How to Clear the Cell?
    By javasaravanan in forum Advanced Java
    Replies: 1
    Last Post: 12-01-2009, 02:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •