old-java-games

java games my brother and I developed as kids.
Log | Files | Refs | README

PongArea.java (2186B)


      1 import java.awt.Color;
      2 import java.awt.Graphics;
      3 import java.awt.Image;
      4 import java.awt.event.ActionEvent;
      5 import java.awt.event.ActionListener;
      6 import java.io.File;
      7 import java.io.IOException;
      8 import java.util.logging.Level;
      9 import java.util.logging.Logger;
     10 
     11 import javax.imageio.ImageIO;
     12 import javax.swing.JPanel;
     13 
     14 public class PongArea extends JPanel implements ActionListener {
     15     private static final long serialVersionUID = -842988339431434549L;
     16     Pong myApplet = null;
     17     Image paddle;
     18     Color[] colours = new Color[] { Color.white, Color.blue, Color.green, Color.red, Color.yellow };
     19     int colourno = 0;
     20 
     21     public PongArea(Pong parent) {
     22         myApplet = parent;
     23         try {
     24             paddle = ImageIO.read(new File("../Assets/paddle.gif"));
     25         } catch (IOException ex) {
     26             Logger.getLogger(PongArea.class.getName()).log(Level.SEVERE, null, ex);
     27         }
     28     }
     29 
     30     @Override
     31     public void paintComponent(Graphics g) {
     32         super.paintComponent(g);
     33 
     34         if (myApplet == null)
     35             return;
     36 
     37         final int w = getBounds().width;
     38         final int h = getBounds().height;
     39         g.setColor(Color.gray);
     40         g.fillRect(0, 0, w, h);
     41         g.setColor(Color.black);
     42 
     43         if (Pong.playerPosition != null) {
     44             // show the player's paddle
     45             // g.drawString("|", myApplet.playerPosition.x, myApplet.playerPosition.y);
     46             g.drawImage(paddle, Pong.playerPosition.x, Pong.playerPosition.y, Color.black, null);
     47 
     48             // show the ball
     49             colourno = 0;// <-------remove this for confusion mode!
     50             for (int i = 0; i < Pong.ballPositions.length; i++) {
     51                 if (Pong.ballPositions[i].x > -1)
     52                     // g.drawImage(pongImage, myApplet.ballPositions[i].x,
     53                     // myApplet.ballPositions[i].y,null);
     54                     g.setColor(colours[colourno++]);
     55                 if (colourno >= colours.length)
     56                     colourno = 0;
     57 
     58                 g.drawString("O", Pong.ballPositions[i].x, Pong.ballPositions[i].y);
     59             }
     60         }
     61     }
     62 
     63     @Override
     64     public void actionPerformed(ActionEvent e) {
     65         repaint();
     66     }
     67 }