title: How do you call the methods of a class instance that was created in another class, from another class?
body: <p>I am trying to create some animated game play text for my breakout game in Java Swing GUI.</p>
  
  <p><strong>Expected behavior:</strong> Everytime a brick is hit its points will Slam onto the screen, pause for 0.25 seconds and then fade up into nothing.</p>
  
  <p><strong>What I have done:</strong> A timer is used inside a method inside a class called <code>AlertText</code>. When the brick is hit in the class <code>GameLogic</code>, a new <code>AlertText</code> is created and its timers start running. Now in the Game Class I have the paint class.</p>
  
  <p><strong>Question:</strong> So how do I call upon the specific instances of <code>AlertText</code> that were created in <code>GameLogic</code> to use the setter and getter methods to set my <code>g.drawString</code> in paint in <code>Game</code> class.  I feel like this should be a common technique? Is there a name for it?</p>
  
  <p>I got it to work with Global variables for one style of brick so I know animation is working, but I would need 100+ global variables to do every kind of brick.</p>
  
  <hr>
  
  <p>Game Class</p>
  
  <pre><code>public class Game extends JPanel   
   {
   public static final int HEIGHT = 720;
   public static final int WIDTH = 600;
   public Color color;
  
  private GameLogic gl = new GameLogic();
  private KeyboardController  controller;
  public Paddle player = new Paddle(110, HEIGHT-30, 100, 10, 10, color.black, controller);
  public Ball gameBall = new Ball(300, 300, 15,  color.black);
  private boolean PaddleUpdateComplete = false;
  
  private List&lt;AlertText&gt; activeAlerts = new ArrayList&lt;AlertText&gt;();
  Game game = new Game();
  
  
  
  public void spawnNewAlert(Brick b){
      AlertText alert = new AlertText();
      alert.setxPos(b.getXPosition());
      alert.setyPos(b.getYPosition());
      alert.setText(b.getPoints()+"");
      alert.setColor(b.getColor());
      activeAlerts.add(alert);
      alert.fireText();
  }    
  
  @Override
  public void paint(Graphics g)
  {
       g.clearRect(0, 0, WIDTH, HEIGHT);
      g.setColor(Color.WHITE);
      g.fillRect(0, 0, WIDTH, HEIGHT);
  
  
      g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, (int)AlertText.staticAlertSize));
      g.setColor(new Color(AlertText.staticRed, AlertText.staticGreen, AlertText.staticBlue));
      g.drawString(AlertText.staticAlertOne, AlertText.staticAlertXPos, AlertText.staticAlertYPos);
  
      player.draw((Graphics2D)g);
      gameBall.draw((Graphics2D)g);
      gl.drawBricks(g);
      // Draw GameObjects and anything else here
      g.setFont(scoreFont);
      g.drawString("Score: " + player.getScore(), 10, 25);
      g.drawString("LIVES: " + player.getLives(), 150, 25);
      if(gl.gameOver(player) &amp;&amp;
              gameBall.getYPosition() &gt;= HEIGHT){
          g.setColor(Color.black);
          g.setFont(endFont);
          g.drawString("Game Over!  Score: " + player.getScore(), (WIDTH/2) - 85, (HEIGHT/2));
      }
      if(gl.empty()){
          g.setColor(Color.black);
          g.setFont(endFont);
          g.drawString("You won!  Score: " + player.getScore(), (WIDTH/2) - 85, (HEIGHT/2));
          timer.stop();
      }
      if(PowerUps.isMegaPaddle){
      g.setColor(Color.orange);
      g.setFont(TimeFont);
      g.drawString(PowerUps.megaPaddlecount+"", 300, 500);
      }
      if(PowerUps.isMegaBall){
       g.setColor(Color.red);
       g.setFont(TimeFont);
       g.drawString(PowerUps.megaBallcount+"", 250, 400);   
      }
      if(!game.activeAlerts.isEmpty()){
  
          for(AlertText alert: game.activeAlerts){
          g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, alert.getTextSize()));
          g.setColor(alert.getColor());
          g.drawString(alert.getText(), alert.getxPos(), alert.getxPos());
          if(alert.count&lt;=0){
           game.activeAlerts.remove(alert);
          }
       }
  
      } 
  }
  
  public void updateGameState()
  {
      gameBall.move();
      player.move(controller);
      gl.checkCollisions(gameBall, player, timer, WIDTH, HEIGHT, game);
      gl.removeBrick();
      // Move GameObjects and check for collisions here
      if(Paddle.paddleHits==1 &amp;&amp; !PaddleUpdateComplete){
          gameBall.setXVelocity(10);
          gameBall.setYVelocity(gameBall.getYVelocity()-6);
          PaddleUpdateComplete = true;
      }
  
  
  }
  
  public final void setupGame()
  {
      gameBall.setXVelocity(0);
      gameBall.setYVelocity(-10);
      player.setLives(5);
      gl.makeBricks();
      // Instantiate instance variables here
  
  
  
  
  }
  
  // Constructor method should not be modified
  public Game()
  {
      // Set the size of the Panel to the correct size
      this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
  
      // Set the background color of the Panel to black
      this.setBackground(Color.BLACK);
  
      // Instantiate a KeyboardController and listen for input with it
      controller = new KeyboardController(); 
      this.addKeyListener(controller);
  
      // Call the setupGame method to initialize instance variables
      this.setupGame();
  
      // Get focus in the window
      this.setFocusable(true);
      this.requestFocusInWindow();
  }
  
  // Start method should not be modified
  public void start()
  {
      // Set up a new Timer to repeat every 20 milliseconds (50 FPS)
      timer = new Timer(20, new ActionListener() {
  
          @Override
          public void actionPerformed(ActionEvent e) 
          {
              repaint();
              updateGameState();
          }
      });
  
      timer.setRepeats(true);
      timer.start();
  }
  
  Timer timer; 
  }
  </code></pre>
  
  <p>Alert Class method fireText()</p>
  
  <pre><code>public void fireText(){ 
    count = 50;
    textSize=0;
    Firing=true;
    Timer time = new Timer(50, null);
      time.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
          if(count&gt;47){
              textSize+=10;
              xPos-=2;
              count--;
          }
          else if(count&gt;42){
              count--;
          }
          else {
          yPos -= 1;
          xPos += 1;
          textSize -= 1;
          count--;
          if(count&lt;=0) {
              text ="";
              count=-1;
              Firing =false;
              time.stop();     
          }
          }
  
    }
    });
      time.start();      
   }
  </code></pre>
  
  <p>GameLogic method</p>
  
  <pre><code>public void checkCollisions(Ball ball, Paddle player, Timer time, int WIDTH, int HEIGHT, Game game) {
      if(hitPaddle(ball, player)){
          ball.setYVelocity(ball.getYVelocity() * -1);
          Paddle.paddleHits++;
          return;
      }
      //check if ball hit any walls
      if(ball.getXPosition() &gt;= (WIDTH - ball.getDiameter()) || ball.getXPosition() &lt;= 0){
          ball.setXVelocity(ball.getXVelocity() * -1);
      }
      if(ball.getYPosition() &gt; (player.getYPosition() + player.getHeight() + 10)){
          resetBall(ball, player, time, WIDTH, HEIGHT);
      }
      if(ball.getYPosition() &lt;= 0){
          ball.setYVelocity(ball.getYVelocity() * -1);
      }
  
      //handle collisions between bricks
      int brickRowsActive = 0;
      for(ArrayList&lt;Brick&gt; alb : bricks){
          if(alb.size() == horizontalCount){
              ++brickRowsActive;
          }
      }
  
      for(int i = (brickRowsActive==0) ? 0 : (brickRowsActive - 1); i &lt; bricks.size(); ++i){
          for(Brick b : bricks.get(i)){ 
              if(brickHitByBall(ball, b)){
                  checkPowerUps(b, player, ball);
                  game.spawnNewAlert(b);
                  player.setScore(player.getScore() + b.getPoints());
                  b.decrementType();
              }
          }
      }
  }
  </code></pre>