3 Replies - 523 Views - Last Post: 03 March 2013 - 06:30 AM Rate Topic: -----

#1 ChaosEvaUnit  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 18-November 11

Java 2D: Issues with setVisible

Posted 02 March 2013 - 07:13 AM

Greetings.
I'm making a basic 2D Tank Shooter game and I'm having some weird issues with using setVisible().
I'm going to include my entire main method for context, but most of it isn't relevant.
So basically I call I call the setVisible() for an object and set it to true or false, to hide it from display.
It works in many other situations within this main method, e.g. when a projectile hits a wall I call setVisible(false) and it does just that.
But I recently implemented some pickups and I call setVisible(false) when a tank intersects one of the pickups, but they just refuse to disappear and thus (in the case of health pickups) just keep replenishing health and remain on the screen.
I've tried loads of different silly little things but it's driving me crazy. I know it's going to be stupid and I willl kick myself when I hear what I've done wrong, but any ideas?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.geom.Rectangle2D;

import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.Timer;


public class GameArea extends JPanel implements ActionListener 
{

    private Timer timer;
    private Tank Player1;
    private Tank Player2;
    private PickUp healthPack1;
    private PickUp healthPack2;
    private PickUp damageBoost;
    private boolean inGame, gameOver;
   // private Image gameLogo, playerLives;
    
	ImageIcon gL = new ImageIcon(this.getClass().getResource("gameLogo.png"));

	
    JButton menuButt, optionsButt, exitButt, readyButt;
    JTextArea infoFeed;
    JTextField playerName;
    JLabel labelHPP1, labelLivesP1, labelHPP2, labelLivesP2, imageLivesP1, imageLivesP2;
    
    public GameArea() 
    {	
    	Image upP1 = new ImageIcon(this.getClass().getResource("tankUp.png")).getImage();
    	Image downP1 = new ImageIcon(this.getClass().getResource("tankDown.png")).getImage();
    	Image leftP1 = new ImageIcon(this.getClass().getResource("tankLeft.png")).getImage();
    	Image rightP1 = new ImageIcon(this.getClass().getResource("tankRight.png")).getImage();

    	Image upP2 = new ImageIcon(this.getClass().getResource("tankP2Up.png")).getImage();
    	Image downP2 = new ImageIcon(this.getClass().getResource("tankP2Down.png")).getImage();
    	Image leftP2 = new ImageIcon(this.getClass().getResource("tankP2Left.png")).getImage();
    	Image rightP2 = new ImageIcon(this.getClass().getResource("tankP2Right.png")).getImage();
    	
    	Image healthPack = new ImageIcon(this.getClass().getResource("medKit.png")).getImage();
    	Image special = new ImageIcon(this.getClass().getResource("special.png")).getImage();
    	
		Player1 = new Tank(KeyEvent.VK_W, KeyEvent.VK_S, KeyEvent.VK_A, KeyEvent.VK_D, KeyEvent.VK_SPACE, upP1, downP1, leftP1, rightP1, rightP1, 250, 265);
		Player2 = new Tank(KeyEvent.VK_UP, KeyEvent.VK_DOWN, KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT, KeyEvent.VK_ENTER, upP2, downP2, leftP2, rightP2, leftP2, 890, 265);
		
		healthPack1 = new PickUp(566, 50, healthPack); 
		healthPack2 = new PickUp(566, 470, healthPack);
		damageBoost = new PickUp(566, 260, special);
		
        timer = new Timer(5, this);
        timer.start();
        
        addKeyListener(new TAdapter());
        setFocusable(true);
        setBackground(Color.LIGHT_GRAY);
        setDoubleBuffered(true);
        inGame = false;
        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        
        JLabel gLogo = new JLabel(gL);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.FIRST_LINE_START;
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 1;
        c.gridheight = 1;
        c.insets = new Insets (20, 0, 20, 0);
        add(gLogo, c);
        
        Dimension gameArea = new Dimension(800,550);
        c.gridx = 1;
        c.gridy = 0;
        c.gridwidth = 2;
        c.gridheight = 4;
        add(Box.createRigidArea(gameArea), c);
        
        infoFeed = new JTextArea("Game" + "\n" + "Information" + "\n" + "Feed");
        c.fill = GridBagConstraints.BOTH;
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 1;
        c.gridheight = 1;
        c.weighty = 1;
        c.ipady = 100;
        c.ipadx = 50;
        add(infoFeed, c);
        
        menuButt = new JButton("New Game");
        menuButt.addActionListener(this);
        menuButt.setFocusable(false);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.CENTER;
        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 1;
        c.gridheight = 1;
        c.weighty = 1;
        c.ipady = 10;
        add(menuButt, c);
        
        optionsButt = new JButton("Options");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.CENTER;
        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 1;
        c.gridheight = 1;
        add(optionsButt, c);
        
        exitButt = new JButton("Exit");
        exitButt.addActionListener(this);
        exitButt.setFocusable(false);
        c.fill = GridBagConstraints.BOTH;
        c.anchor = GridBagConstraints.CENTER;
        c.gridx = 0;
        c.gridy = 4;
        c.gridwidth = 1;
        c.gridheight = 2;
        c.ipady = 20;
        add(exitButt, c);
        
        labelHPP1 = new JLabel("HP:");
        labelHPP1.setVisible(false);
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.LINE_START;
        c.gridx = 1;
        c.gridy = 5;
        c.gridheight = 1;
        c.gridwidth = 1;
        c.insets = new Insets (0,100,20,0);
        add(labelHPP1, c);
        
        labelLivesP1 = new JLabel("Lives: ");
        labelLivesP1.setVisible(false);
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.LINE_START;
        c.gridx = 1;
        c.gridy = 6;
        c.gridheight = 1;
        c.gridwidth = 1;
        add(labelLivesP1, c);
        
        labelHPP2 = new JLabel("HP:");
        labelHPP2.setVisible(false);
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.LINE_START;
        c.gridx = 2;
        c.gridy = 5;
        c.gridheight = 1;
        c.gridwidth = 1;
        c.insets = new Insets (0, 250, 20, 0);
        add(labelHPP2, c);
        
        labelLivesP2 = new JLabel("Lives:");
        labelLivesP2.setVisible(false);
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.LINE_START;
        c.gridx = 2;
        c.gridy = 6;
        c.gridheight = 1;
        c.gridwidth = 1;
        add(labelLivesP2, c);
        
    }

    public void addNotify()
    {
    	super.addNotify();
    }

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

        Graphics2D g2d = (Graphics2D)g;
        g2d.setColor(Color.BLACK);
        Rectangle2D gameArea = new Rectangle2D.Double(210, 20, 770, 520);
        g2d.draw(gameArea);
        g2d.setColor(Color.GRAY);
        Rectangle2D gameFill = new Rectangle2D.Double(211, 21, 769, 519);
        g2d.fill(gameFill);
    	
        if(inGame == true && gameOver == false)
        {
        	g2d.drawImage(Player1.getImage(), Player1.getX(), Player1.getY(), this);
        	g2d.drawImage(Player2.getImage(), Player2.getX(), Player2.getY(), this);
        	
        	g2d.drawImage(healthPack1.getImage(), healthPack1.getX(), healthPack1.getY(), this);
        	g2d.drawImage(healthPack2.getImage(), healthPack2.getX(), healthPack2.getY(), this);
        	g2d.drawImage(damageBoost.getImage(), damageBoost.getX(), damageBoost.getY(), this);
        	
            g2d.setColor(Color.BLACK);
            Rectangle2D barHPP1 = new Rectangle2D.Double(300, 572, 200, 20);
            g2d.draw(barHPP1);
            Rectangle2D barHPP2 = new Rectangle2D.Double(640, 572, 200, 20);
            g2d.draw(barHPP2);
            
            g2d.setColor(Color.DARK_GRAY);
            
            Rectangle2D nwBlock = new Rectangle2D.Double(211, 21, 100, 100);
            Rectangle2D neBlock = new Rectangle2D.Double(880, 21, 100, 100);
            Rectangle2D swBlock = new Rectangle2D.Double(211, 440, 100, 100);
            Rectangle2D seBlock = new Rectangle2D.Double(880, 440, 100, 100);
            Rectangle2D nwWall = new Rectangle2D.Double(430, 21, 20, 150);
            Rectangle2D neWall = new Rectangle2D.Double(720, 21, 20, 150);
            Rectangle2D swWall = new Rectangle2D.Double(430, 390, 20, 150);
            Rectangle2D seWall = new Rectangle2D.Double(720, 390, 20, 150);
            Rectangle2D nBlock = new Rectangle2D.Double(553, 140, 70, 70);
            Rectangle2D sBlock = new Rectangle2D.Double(553, 360, 70, 70);
            Rectangle2D wBlock = new Rectangle2D.Double(412, 255, 50, 50);
            Rectangle2D eBlock = new Rectangle2D.Double(707, 255, 50, 50);
            
            g2d.fill(nwBlock);
            g2d.fill(neBlock);
            g2d.fill(swBlock);
            g2d.fill(seBlock);
            g2d.fill(nwWall);
            g2d.fill(neWall);
            g2d.fill(swWall);
            g2d.fill(seWall);
            g2d.fill(nBlock);
            g2d.fill(sBlock);
            g2d.fill(wBlock);
            g2d.fill(eBlock);
            
            g2d.setColor(Color.BLACK);
            
            Rectangle2D nwBBounds = new Rectangle2D.Double(210, 20, 100, 100);
            Rectangle2D neBBounds = new Rectangle2D.Double(880, 20, 100, 100);
            Rectangle2D swBBounds = new Rectangle2D.Double(210, 440, 100, 100);
            Rectangle2D seBBounds = new Rectangle2D.Double(880, 440, 100, 100);
            Rectangle2D nwWBounds = new Rectangle2D.Double(430, 20, 20, 150);
            Rectangle2D neWBounds = new Rectangle2D.Double(720, 20, 20, 150);
            Rectangle2D swWBounds = new Rectangle2D.Double(430, 390, 20, 150);
            Rectangle2D seWBounds = new Rectangle2D.Double(720, 390, 20, 150);
            Rectangle2D nBBounds = new Rectangle2D.Double(553, 140, 70, 70);
            Rectangle2D sBBounds = new Rectangle2D.Double(553, 360, 70, 70);
            Rectangle2D wBBounds = new Rectangle2D.Double(412, 255, 50, 50);
            Rectangle2D eBBounds = new Rectangle2D.Double(707, 255, 50, 50);
            
            g2d.draw(nwBBounds);
            g2d.draw(neBBounds);
            g2d.draw(swBBounds);
            g2d.draw(seBBounds);
            g2d.draw(nwWBounds);
            g2d.draw(neWBounds);
            g2d.draw(swWBounds);
            g2d.draw(seWBounds);
            g2d.draw(nBBounds);
            g2d.draw(sBBounds);
            g2d.draw(wBBounds);
            g2d.draw(eBBounds);
            
            Rectangle2D fillHPP1;
            Rectangle2D fillHPP2;
            Rectangle2D fillLivesP1;
            Rectangle2D fillLivesP2;

            g2d.setColor(Color.GREEN);

            switch(Player1.getHitPoints())
            {
            	case 10: fillHPP1 = new Rectangle2D.Double(301, 573, 199, 19);
            	g2d.fill(fillHPP1);
            	break;
            	case 9: fillHPP1 = new Rectangle2D.Double(301, 573, 179, 19);
            	g2d.fill(fillHPP1);
            	break;
            	case 8: fillHPP1 = new Rectangle2D.Double(301, 573, 159, 19);
            	g2d.fill(fillHPP1);
            	break;
            	case 7: fillHPP1 = new Rectangle2D.Double(301, 573, 139, 19);
            	g2d.fill(fillHPP1);
            	break;
            	case 6: fillHPP1 = new Rectangle2D.Double(301, 573, 119, 19);
            	g2d.fill(fillHPP1);
            	break;
            	case 5: fillHPP1 = new Rectangle2D.Double(301, 573, 89, 19);
            	g2d.fill(fillHPP1);
            	break;
            	case 4: fillHPP1 = new Rectangle2D.Double(301, 573, 69, 19);
            	g2d.fill(fillHPP1);
            	break;
            	case 3: fillHPP1 = new Rectangle2D.Double(301, 573, 49, 19);
            	g2d.fill(fillHPP1);
            	break;
            	case 2: fillHPP1 = new Rectangle2D.Double(301, 573, 29, 19);
            	g2d.fill(fillHPP1);
            	break;
            	case 1: fillHPP1 = new Rectangle2D.Double(301, 573, 9, 19);
            	g2d.fill(fillHPP1);
            	break;
            	case 0: fillHPP1 = new Rectangle2D.Double(301, 573, 0, 0);
            	g2d.fill(fillHPP1);
            	break;  			
            }
            
            g2d.setColor(Color.MAGENTA);

            switch(Player2.getHitPoints())
            {
            	case 10: fillHPP2 = new Rectangle2D.Double(641, 573, 199, 19);
            	g2d.fill(fillHPP2);
            	break;
            	case 9: fillHPP2 = new Rectangle2D.Double(641, 573, 179, 19);
            	g2d.fill(fillHPP2);
            	break;
            	case 8: fillHPP2 = new Rectangle2D.Double(641, 573, 159, 19);
            	g2d.fill(fillHPP2);
            	break;
            	case 7: fillHPP2 = new Rectangle2D.Double(641, 573, 139, 19);
            	g2d.fill(fillHPP2);
            	break;
            	case 6: fillHPP2 = new Rectangle2D.Double(641, 573, 119, 19);
            	g2d.fill(fillHPP2);
            	break;
            	case 5: fillHPP2 = new Rectangle2D.Double(641, 573, 89, 19);
            	g2d.fill(fillHPP2);
            	break;
            	case 4: fillHPP2 = new Rectangle2D.Double(641, 573, 69, 19);
            	g2d.fill(fillHPP2);
            	break;
            	case 3: fillHPP2 = new Rectangle2D.Double(641, 573, 49, 19);
            	g2d.fill(fillHPP2);
            	break;
            	case 2: fillHPP2 = new Rectangle2D.Double(641, 573, 29, 19);
            	g2d.fill(fillHPP2);
            	break;
            	case 1: fillHPP2 = new Rectangle2D.Double(641, 573, 9, 19);
            	g2d.fill(fillHPP2);
            	break;
            	case 0: fillHPP2 = new Rectangle2D.Double(641, 573, 0, 0);
            	g2d.fill(fillHPP2);
            	break;  			
            }
            
            g2d.setColor(Color.RED);

            switch(Player1.getLives())
            {
            	case 3: fillLivesP1 = new Rectangle2D.Double(316, 628, 59, 19);
            	g2d.fill(fillLivesP1);
            	break;
            	case 2: fillLivesP1 = new Rectangle2D.Double(316, 628, 39, 19);
            	g2d.fill(fillLivesP1);
            	break;
            	case 1: fillLivesP1 = new Rectangle2D.Double(316, 628, 19, 19);
            	g2d.fill(fillLivesP1);
            	break;
            	case 0: fillLivesP1 = new Rectangle2D.Double(316, 628, 0, 0);
            	g2d.fill(fillLivesP1);
            	break;  
            }
            
            switch(Player2.getLives())
            {
            	case 3: fillLivesP2 = new Rectangle2D.Double(656, 628, 59, 19);
            	g2d.fill(fillLivesP2);
            	break;
            	case 2: fillLivesP2 = new Rectangle2D.Double(656, 628, 39, 19);
            	g2d.fill(fillLivesP2);
            	break;
            	case 1: fillLivesP2 = new Rectangle2D.Double(656, 628, 19, 19);
            	g2d.fill(fillLivesP2);
            	break;
            	case 0: fillLivesP2 = new Rectangle2D.Double(656, 628, 0, 0);
            	g2d.fill(fillLivesP2);
            	break;  
            }
            
            g2d.setColor(Color.BLACK);
            
            Rectangle2D life1P1 = new Rectangle2D.Double(315, 627, 20, 20);
            g2d.draw(life1P1);
            Rectangle2D life2P1 = new Rectangle2D.Double(335, 627, 20, 20);
            g2d.draw(life2P1);
            Rectangle2D life3P1 = new Rectangle2D.Double(355, 627, 20, 20);
            g2d.draw(life3P1);
            Rectangle2D life1P2 = new Rectangle2D.Double(655, 627, 20, 20);
            g2d.draw(life1P2);
            Rectangle2D life2P2 = new Rectangle2D.Double(675, 627, 20, 20);
            g2d.draw(life2P2);
            Rectangle2D life3P2 = new Rectangle2D.Double(695, 627, 20, 20);
            g2d.draw(life3P2);
        
        	ArrayList<Projectile> projectilesP1 = Player1.getProjectiles();
        	ArrayList<Projectile> projectilesP2 = Player2.getProjectiles();
        
        	for (int i = 0; i < projectilesP1.size(); i++ ) 
        	{
        		Projectile m = (Projectile) projectilesP1.get(i);
        		g2d.drawImage(m.getImage(), m.getX(), m.getY(), this);
        	}
        
        	for (int i = 0; i < projectilesP2.size(); i++ ) 
        	{
        		Projectile m = (Projectile) projectilesP2.get(i);
        		g2d.drawImage(m.getImage(), m.getX(), m.getY(), this);
        	}
        }
        
        else
        {
        	if(gameOver == true)
        	{
        	
            Font small = new Font("Helvetica", Font.BOLD, 14);

            g.setColor(Color.white);
            g.setFont(small);
            
            menuButt.setEnabled(true);
            
            	if(Player1.destroyed == false)
            	{
            		String msg = "Game Over!";
            		String msg2 = "Player 1 Wins!";
            		g.drawString(msg, 540, 250);
            		g.drawString(msg2, 534, 270);
            	}
            
            	if(Player2.destroyed == false)
            	{
            		String msg = "Game Over!";
            		String msg2	= "Player 2 Wins!";
            		g.drawString(msg, 540, 250);
            		g.drawString(msg2, 534, 270);
            	}
            	
            	if(Player1.destroyed == true && Player2.destroyed == true)
            	{
            		String msg = "Game Over!";
            		String msg2	= "It's a Draw!";
            		g.drawString(msg, 540, 250);
            		g.drawString(msg2, 545, 270);
            	}
        	}
        }

        
        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }


    public void actionPerformed(ActionEvent e) 
    {
    	if(e.getSource() == menuButt)
    	{
    		inGame = true;
    		gameOver = false;
    		menuButt.setEnabled(false);
    		labelHPP1.setVisible(true);
    		labelHPP2.setVisible(true);
    		labelLivesP1.setVisible(true);
    		labelLivesP2.setVisible(true);
    		repaint();
    	}
    	
    	if(e.getSource() == exitButt)
    	{
    		System.exit(0);
    	}
    	
		if(Player1.destroyed || Player2.destroyed == true)
		{
			gameOver = true;
			repaint();
		}
		
        ArrayList<Projectile> projectilesP1 = Player1.getProjectiles();
        ArrayList<Projectile> projectilesP2 = Player2.getProjectiles();

        for (int i = 0; i < projectilesP1.size(); i++) 
        {
            Projectile m = (Projectile) projectilesP1.get(i);
            if (m.isVisible()) 
                m.move();
            else projectilesP1.remove(i);
        }
        
        for (int i = 0; i < projectilesP2.size(); i++) 
        {
            Projectile m = (Projectile) projectilesP2.get(i);
            if (m.isVisible()) 
                m.move();
            else projectilesP2.remove(i);
        }

        Player1.move();
        Player2.move();
        checkCollisions();
        repaint();  
    }
    
    public void checkCollisions()
    {
    	Rectangle player1Bounds = Player1.getBounds();
    	Rectangle player2Bounds = Player2.getBounds();
    	
    	Rectangle healthPack1Bounds = healthPack1.getBounds();
    	Rectangle healthPack2Bounds = healthPack2.getBounds();
    	Rectangle damageBoostBounds = damageBoost.getBounds();
    	
    	Rectangle2D nwBBounds = new Rectangle2D.Double(210, 20, 100, 100);
        Rectangle2D neBBounds = new Rectangle2D.Double(880, 20, 100, 100);
        Rectangle2D swBBounds = new Rectangle2D.Double(210, 440, 100, 100);
        Rectangle2D seBBounds = new Rectangle2D.Double(880, 440, 100, 100);
        Rectangle2D nwWBounds = new Rectangle2D.Double(430, 20, 20, 150);
        Rectangle2D neWBounds = new Rectangle2D.Double(720, 20, 20, 150);
        Rectangle2D swWBounds = new Rectangle2D.Double(430, 390, 20, 150);
        Rectangle2D seWBounds = new Rectangle2D.Double(720, 390, 20, 150);
        Rectangle2D nBBounds = new Rectangle2D.Double(553, 140, 70, 70);
        Rectangle2D sBBounds = new Rectangle2D.Double(553, 360, 70, 70);
        Rectangle2D wBBounds = new Rectangle2D.Double(412, 255, 50, 50);
        Rectangle2D eBBounds = new Rectangle2D.Double(707, 255, 50, 50);
    	
        ArrayList<Rectangle2D> wallBounds = new ArrayList<Rectangle2D>();
        
        wallBounds.add(nwBBounds);
        wallBounds.add(neBBounds);
        wallBounds.add(swBBounds);
        wallBounds.add(seBBounds);
        wallBounds.add(nwWBounds);
        wallBounds.add(neWBounds);
        wallBounds.add(swWBounds);
        wallBounds.add(seWBounds);
        wallBounds.add(nBBounds);
        wallBounds.add(sBBounds);
        wallBounds.add(wBBounds);
        wallBounds.add(eBBounds);
        
    	if(player1Bounds.intersects(player2Bounds))
    	{
    		Player1.Collision();
    		Player2.Collision();
    	}
    	
    	for(int i = 0; i < wallBounds.size(); i++)
    	{
    		if(player1Bounds.intersects(wallBounds.get(i)))
    		{
    			Player1.Collision();
    		}
    		
    		if(player2Bounds.intersects(wallBounds.get(i)))
    		{
    			Player2.Collision();
    		}
    	}
    	
    	ArrayList<Projectile> projectilesP1 = Player1.getProjectiles();
    	ArrayList<Projectile> projectilesP2 = Player2.getProjectiles();
    	
    	for (int i = 0; i < projectilesP1.size(); i++)
    	{
    		Projectile m = (Projectile) projectilesP1.get(i);
    		Rectangle projectileBounds = m.getBounds();
    		
    		if(projectileBounds.intersects(player2Bounds))
    		{
    			m.setVisible(false);
    			Player2.projectileCollision();
    		}
    		
        	for(int j = 0; j < wallBounds.size(); j++)
        	{
        		if(projectileBounds.intersects(wallBounds.get(j)))
        		{
        			m.setVisible(false);
        		}
        	}
    	}
    	
    	for (int i = 0; i < projectilesP2.size(); i++)
    	{
    		Projectile m = (Projectile) projectilesP2.get(i);
    		Rectangle projectileBounds = m.getBounds();
    		
    		if(projectileBounds.intersects(player1Bounds))
    		{
    			m.setVisible(false);
    			Player1.projectileCollision();
    		}
    		
        	for(int j = 0; j < wallBounds.size(); j++)
        	{
        		if(projectileBounds.intersects(wallBounds.get(j)))
        		{
        			m.setVisible(false);
        		}
        	}
    	}
    	
    	if(player1Bounds.intersects(healthPack1Bounds))
    	{
    		healthPack1.setVisible(false);
    		System.out.println(healthPack1.getImage());
    		Player1.setHitPoints(Player1.getHitPoints() + 3);
    		
    		if(Player1.getHitPoints() > 10)
    		{
    			Player1.setHitPoints(10);
    		}
    	}
    	
    	if(player1Bounds.intersects(healthPack2Bounds))
    	{
    		healthPack2.setVisible(false);
    		Player1.setHitPoints(Player1.getHitPoints() + 3);
    		
    		if(Player1.getHitPoints() > 10)
    		{
    			Player1.setHitPoints(10);
    		}
    	}
    	
    	if(player1Bounds.intersects(damageBoostBounds))
    	{
    		damageBoost.setVisible(false);
    		Player2.damageUp = true;
    	}
    	
    	if(player2Bounds.intersects(healthPack1Bounds))
    	{
    		healthPack1.setVisible(false);
    		Player2.setHitPoints(Player2.getHitPoints() + 3);
    		
    		if(Player2.getHitPoints() > 10)
    		{
    			Player2.setHitPoints(10);
    		}
    	}
    	
    	if(player2Bounds.intersects(healthPack2Bounds))
    	{
    		healthPack2.setVisible(false);
    		Player2.setHitPoints(Player2.getHitPoints() + 3);
    		
    		if(Player2.getHitPoints() > 10)
    		{
    			Player2.setHitPoints(10);
    		}
    	}
    	
    	if(player2Bounds.intersects(damageBoostBounds))
    	{
    		damageBoost.setVisible(false);
    		Player1.damageUp = true;
    	}
    }

    private class TAdapter extends KeyAdapter 
    {
        public void keyReleased(KeyEvent e) 
        {
            Player1.keyReleased(e);
            Player2.keyReleased(e);
        }

        public void keyPressed(KeyEvent e) 
        {
            Player1.keyPressed(e);
            Player2.keyPressed(e);
        }
    }
}


In the main, I make the PickUp Object and then in the checkCollisions method I deal with the colliding of tanks with the pickups.
Also here is the PickUp class for clarity.

import java.awt.Image;
import java.awt.Rectangle;


public class PickUp 
{
	private Image image;
	private int x, y, height, width;
	private boolean visible;
	
	public PickUp(int X, int Y, Image pickUpImage) 
	{
    	image = pickUpImage;
    	
    	height = image.getHeight(null);
    	width = image.getWidth(null);
    	visible = true;
    	
    	x = X;
    	y = Y;
	}
	
	public int getX()
	{
		return x;
	}
	
	public int getY()
	{
		return y;
	}
	
	public int getHeight()
	{
		return height;
	}
	
	public int getWidth()
	{
		return width;
	}
	
	public Image getImage()
	{
		return image;
	}
	
    public Rectangle getBounds()
    {
    	return new Rectangle(x, y, width, height);
    }
    
    public boolean isVisible()
    {
    	return visible;
    }
    
    public void setVisible(boolean visible)
    {
    	this.visible = visible;
    }

    public void setImage(Image pickUpImage)
    {
    	this.image = pickUpImage;
    }
}



Cheers.

Is This A Good Question/Topic? 0
  • +

Replies To: Java 2D: Issues with setVisible

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2816
  • View blog
  • Posts: 11,918
  • Joined: 20-September 08

Re: Java 2D: Issues with setVisible

Posted 02 March 2013 - 08:01 AM

Your setVisible methods are not in the context of a Component/JComponent so won't have an 'automatic' implementation. In your case, you would normally have to call repaint() on the gui, such that whatever is visualising, say, 'PickUp' repaints itself and thus makes a change to the visualisation of PickUp
Was This Post Helpful? 0
  • +
  • -

#3 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,620
  • Joined: 29-July 11

Re: Java 2D: Issues with setVisible

Posted 02 March 2013 - 08:51 AM

First, you never have to call dispose() on a graphics object, because you are getting it from paint(). Swing will handle that for you.

Second, your paint() should really be paintComponent(), although you are calling up to super, which is good.

Third, paint() is entirely too complicated. The purpose of paint is to paint. Try and get all that logic out of there. Every object should have its own paint() or draw() method that it uses to paint itself. For example, in that PickUp class you should have:

public void draw(Graphics g) {
    if(isVisible) {
        d.drawImage(image, x, y, null);
    }
}



That was you can just paint it like so:
PickUp extraLife = new PickUp();

public void patintComponent(Graphics gr) {
    Graphics2D g = (Graphics2D)gr;
    extraLife.draw(g);
}



This way each object determines itself whether or not it is visible and whether or not to paint itself when its draw() is called.

Now, although I do not like the way you are doing it, it seems as f checkCollisions() is at least functional with regard to setting the health packs' visibility to false when it intersects a player, but I notice in paint() that you are always painting the health packs, lines 211 - 213. You should only paint them when they are visible.

To match your code:

if(healthPack1.isVisible()) {
    g2d.drawImage(healthPack1.getImage(), healthPack1.getX(), healthPack1.getY(), this);
}



This would seem to be why they are always staying on screen.

This post has been edited by farrell2k: 02 March 2013 - 08:52 AM

Was This Post Helpful? 2
  • +
  • -

#4 ChaosEvaUnit  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 18-November 11

Re: Java 2D: Issues with setVisible

Posted 03 March 2013 - 06:30 AM

View Postfarrell2k, on 02 March 2013 - 08:51 AM, said:

First, you never have to call dispose() on a graphics object, because you are getting it from paint(). Swing will handle that for you.

Second, your paint() should really be paintComponent(), although you are calling up to super, which is good.

Third, paint() is entirely too complicated. The purpose of paint is to paint. Try and get all that logic out of there. Every object should have its own paint() or draw() method that it uses to paint itself. For example, in that PickUp class you should have:

public void draw(Graphics g) {
    if(isVisible) {
        d.drawImage(image, x, y, null);
    }
}



That was you can just paint it like so:
PickUp extraLife = new PickUp();

public void patintComponent(Graphics gr) {
    Graphics2D g = (Graphics2D)gr;
    extraLife.draw(g);
}



This way each object determines itself whether or not it is visible and whether or not to paint itself when its draw() is called.

Now, although I do not like the way you are doing it, it seems as f checkCollisions() is at least functional with regard to setting the health packs' visibility to false when it intersects a player, but I notice in paint() that you are always painting the health packs, lines 211 - 213. You should only paint them when they are visible.

To match your code:

if(healthPack1.isVisible()) {
    g2d.drawImage(healthPack1.getImage(), healthPack1.getX(), healthPack1.getY(), this);
}



This would seem to be why they are always staying on screen.


This is perfect.
I've got them working correctly now!
And now working on cleaning up my paint method, thanks for the advice on that too.

This post has been edited by ChaosEvaUnit: 03 March 2013 - 06:30 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1