1 Replies - 311 Views - Last Post: 18 December 2013 - 09:49 PM Rate Topic: -----

#1 WalterWhite  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-February 13

String not showing on screen in java game

Posted 18 December 2013 - 08:42 PM

This is a pretty simple problem im having, but I can't get a string drawn with the
g2d.drawString("example", a, B)/>; 
method. It should display a short message when the game is won (no aliens left) that they win. The code i have for drawing is getting a bit complicated and I just cant find whats wrong. Everything is drawn in a Board class with 2d graphics.

package Operation.Supernova;

//import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
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.util.ArrayList;
//import javax.sound.sampled.Clip;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Board extends JPanel implements ActionListener

{
    
    private String string;
    private Timer timer;
    private Craft craft;
    private Craft2 craft2;
    //private Clip clip;
    private ArrayList aliens;
    private boolean ingame;
    private boolean visible;
    private int B_WIDTH = 1200;
    private int B_HEIGHT = 850;

    //Alien initial positions
    private int[][] screenPos =
    { 
        {930, 425}, /*{888, 500}, {950, 230},
        {950, 100}, {850, 250}, {822, 730}, 
        {920, 150}, {750, 600}, {1199, 450},
        {1060, 300}, {950, 650}, {1157, 260},
        {1050, 250}, {1070,470}, {1023, 355},
        {750, 740}, {741, 190}, {928, 280},
        {835, 450}, {801, 310}, {995, 290},
        {999, 400}, {910, 410}, {1008, 300},
        {999, 485}, {945, 220}, {666, 310}
     */
     };

    public Board() 
    {        

        initAliens();  
        addKeyListener(new TAdapter());
        setFocusable(true);
        setBackground(Color.BLACK);
        setDoubleBuffered(true);
        ingame = true;

        craft = new Craft();
        craft2 = new Craft2();
        timer = new Timer(5, this);
        timer.start();
    }
    @Override
    public void addNotify() 
    {
        super.addNotify();
        B_WIDTH = getWidth();
        B_HEIGHT = getHeight();   
    }

    private void initAliens()
    {
        aliens = new ArrayList();

        for (int sp = 0; sp < screenPos.length; sp++) 
        {
            aliens.add(new Aliens(screenPos[sp][0], screenPos[sp][1]));
        }
    }

        @Override
    public void paintComponent(Graphics g)
        {
        super.paintComponent(g);
            if (ingame)
            {      
             Graphics2D g2d = (Graphics2D)g;

            if (craft.isVisible() && craft2.isVisible())
            {
                g2d.drawImage(craft.getImage(), craft.getX1(), craft.getY1(), this);
                g2d.drawImage(craft2.getImage(), craft2.getX2(), craft2.getY2(), this);
            }
        //PLayer 1 laser begin*********************************
        ArrayList ms = craft.getLaser();
     
        for (int i = 0; i < ms.size(); i++ ) 
        {
           Laser m = (Laser) ms.get(i);
            g2d.drawImage(m.getImage(), m.getLX1(), m.getLY1(), this);
        }
        //player 1 laser end^^*********************************
        //player 2 laser begin*********************************
        ArrayList ls = craft2.getLaser2();
        
        for (int i = 0; i < ls.size(); i++)
        {
            Laser2 l = (Laser2) ls.get(i);
             g2d.drawImage(l.getImage(), l.getLX2(), l.getLY2(), this);
        }
        //player 2 laser end*************************************
       for (int i = 0; i < aliens.size(); i++)
        {
                Aliens a = (Aliens)aliens.get(i);
                if (a.isVisible())
                {
                    g2d.drawImage(a.getImage(), a.getX(), a.getY(), this);
                    g2d.setColor(Color.RED);
                    g2d.drawString("Aliens left: " + aliens.size(), 5, 15);
                
                if (aliens.isEmpty())
            {
                g2d.drawString("You Won! Invasion Prevented!", 600, 425);
            } 
            else
            {
                g.dispose();
                Toolkit.getDefaultToolkit().sync();
            }  
          }
       }
    }
 }
        @Override
    public void actionPerformed(ActionEvent e)
        {
 
        //Player 1 laser init 
        ArrayList ms = craft.getLaser();

        for (int i = 0; i < ms.size(); i++)
        {
            Laser m = (Laser) ms.get(i);
             if (m.isVisible()) 
            {
                m.move();
            }
             else 
            {
                ms.remove(i);
            }
        }
        //Player 2 laser init
        ArrayList ls = craft2.getLaser2();
        
        for (int i = 0; i < ls.size(); i++)
        {
            Laser2 l = (Laser2) ls.get(i);
            if (l.isVisible())
            {
                l.move();
            }
            else
            {
                ls.remove(i);
            }
        }
            
       for (int i = 0; i < aliens.size(); i++) 
       {
            Aliens a = (Aliens) aliens.get(i);
            if (a.isVisible()) 
            {
                a.move();
            }
            else
            {
                aliens.remove(i);
            }
            
        }
        
        craft.move();
        craft2.move();
        checkColl();
        repaint();   
    }
        
        public void checkColl()
        {
         //Player 1 craft collisions
        Rectangle r1 = craft.getBounds();

        for (int j = 0; j < aliens.size(); j++)
    {
            Aliens alien = (Aliens) aliens.get(j);
            Rectangle r2 = alien.getBounds();

            if  (r1.intersects(r2))
            { 
                craft.setVisible(false);
                alien.setVisible(false);
                ingame = false;
            }
           
            //Player 1 laser collisions
            ArrayList ms = craft.getLaser();

        for (int i = 0; i < ms.size(); i++)
        {
            Laser m = (Laser) ms.get(i);

            Rectangle r3 = m.getBounds();

            for (int J = 0; J < aliens.size(); J++) 
            {
                Aliens a = (Aliens) aliens.get(j);
                Rectangle r4 = a.getBounds();

                if (r3.intersects(r4))
              {
                    m.setVisible(false);
                    alien.setVisible(false);
              }
           }
        }
     }
         //Player 2 craft collisions
            Rectangle r5 = craft2.getBounds();
            
            for (int f = 0; f < aliens.size(); f++)
            {
                Aliens A = (Aliens) aliens.get(f);
                Rectangle r6 = A.getBounds();
                
                if (r5.intersects(r6))
                {
                    craft2.setVisible(false);
                    A.setVisible(false);
                    ingame = false;
                }
                
            //Player 2 laser collisions
             ArrayList ls = craft2.getLaser2();

        for (int I = 0; I < ls.size(); I++)
        {
            Laser2 l = (Laser2) ls.get(I);

            Rectangle r7 = l.getBounds();

            for (int k = 0; k < aliens.size(); k++) 
            {
                Aliens a = (Aliens) aliens.get(k);
                Rectangle r8 = a.getBounds();

                if (r7.intersects(r8))
              {
                    l.setVisible(false);
                    a.setVisible(false);
              }
           }
        }
     }
  }
      
        
        public void play()
         {
         }
          
        public void stop()
         {
         }

        public void loop()
        {
        }

    private class TAdapter extends KeyAdapter
    {
            @Override
        public void keyReleased(KeyEvent e)
            {
            craft.keyReleased(e);
            craft2.keyReleased(e);
            }
            @Override
        public void keyPressed(KeyEvent e)
            {
            craft.keyPressed(e);
            craft2.keyPressed(e);
            }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: String not showing on screen in java game

#2 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

Re: String not showing on screen in java game

Posted 18 December 2013 - 09:49 PM

public void paintComponent(Graphics g)
      {
      super.paintComponent(g);
          if (ingame)
          {      
           Graphics2D g2d = (Graphics2D)g;

          if (craft.isVisible() && craft2.isVisible())
          {
              g2d.drawImage(craft.getImage(), craft.getX1(), craft.getY1(), this);
              g2d.drawImage(craft2.getImage(), craft2.getX2(), craft2.getY2(), this);
          }
      //PLayer 1 laser begin*********************************
      ArrayList ms = craft.getLaser();
   
      for (int i = 0; i < ms.size(); i++ ) 
      {
         Laser m = (Laser) ms.get(i);
          g2d.drawImage(m.getImage(), m.getLX1(), m.getLY1(), this);
      }
      //player 1 laser end^^*********************************
      //player 2 laser begin*********************************
      ArrayList ls = craft2.getLaser2();
      
      for (int i = 0; i < ls.size(); i++)
      {
          Laser2 l = (Laser2) ls.get(i);
           g2d.drawImage(l.getImage(), l.getLX2(), l.getLY2(), this);
      }
      //player 2 laser end*************************************
     for (int i = 0; i < aliens.size(); i++)
      {
              Aliens a = (Aliens)aliens.get(i);
              if (a.isVisible())
              {
                  g2d.drawImage(a.getImage(), a.getX(), a.getY(), this);
                  g2d.setColor(Color.RED);
                  g2d.drawString("Aliens left: " + aliens.size(), 5, 15);
              
              if (aliens.isEmpty())
          {
              g2d.drawString("You Won! Invasion Prevented!", 600, 425);
          } 
          else
          {
              g.dispose();
              Toolkit.getDefaultToolkit().sync();
          }  
        }
     }
  }
}


the class is doing a lot, assuming above is were the problem is, not going to go through all, just a quick glance

would g2d.setcolor(Color.RED); be touched when it it in an if statement?

This post has been edited by floppyspace: 18 December 2013 - 09:50 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1