Forum Stats

  • 3,752,912 Users
  • 2,250,585 Discussions
  • 7,868,049 Comments

Discussions

How to paint polygons?

//Danielle Grieco
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferStrategy;
import javax.swing.*;
import java.util.ArrayList;

public class FINALGAME extends Canvas implements KeyListener
{
    protected boolean on = true;
    protected int width, height;
    protected Image buffer;
   
    //Game Constants;
   final static int INIT_BUILDINGS = 5;
    final static int BUILDING_SPEED = 7;
    final int ROTATION_SPEED = 5;
    final int JET_SPEED = 10;
    final static int SCREEN_WIDTH = 1000;
    final static int SCREEN_HEIGHT = 1000;
   
    private int livesLeft;
    private ArrayList<Building> buildings = new ArrayList<Building>();
    private JetPack theJet;
    private BufferStrategy strategy;
    /*private boolean shipResetInProgress = false;
    private int shipResetCycleCt = 0;*/
   private JButton startButton;

   
    //Game Constructor
    public FINALGAME() {

     //Code from Game that creates the window for the display
       JFrame frame = new JFrame("Final Game");
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        frame.add(panel);
        panel.add(this, BorderLayout.NORTH);
        frame.setSize(SCREEN_WIDTH,SCREEN_HEIGHT);
        frame.getContentPane().setBackground(Color.RED);
        frame.setVisible(true);
        frame.setResizable(false);
        livesLeft = 5;
        /*startButton = new JButton("Start");
        startButton.setBackground(Color.BLUE);
        //startButton.setForeground(Color.WHITE);
        startButton.setFont(new Font("Helvetica",Font.BOLD, 36));
        startButton.setPreferredSize(new Dimension(600, 200));
        panel.add(startButton, BorderLayout.SOUTH);
        //startButton.addActionListener(this);*/
      
       Point[] newShape = {new Point(10,10), new Point(0, 25), new Point(0, 35), new Point(20,35), new Point(20,25)};
       theJet = new JetPack(newShape);
       frame.addWindowListener(new WindowAdapter() {
       public void windowClosing(WindowEvent e) {System.exit(0);}
     });
       Point[] aShape;
       for (int i = 0; i < INIT_BUILDINGS; i++)
       {
          Point[] tempShape = {new Point(15,20), new Point(15,0), new Point(0,0), new Point(0,20)};
          aShape = tempShape;
         
       //Start the building at the bottom right of the screen
         int xLoc = (SCREEN_WIDTH/2);
      
          int yLoc = (SCREEN_HEIGHT/2);

          buildings.add(new Building(aShape, xLoc, yLoc));
         
       }   
      
     // APCS - End of the part to create asteroids
   
     //Update the window and add key listener for ship and timer listener for asteroids
    render();
     //PART ONE - UNCOMMENT THIS TO USE THE KEY LISTENER
    //addKeyListener(this);
   
     //Need timer events to make asteroids move - SEH
    //  Removed calls to render from the keyPress events but needed more frequent rendering by timer
    int delay = 100; //milliseconds - started at 1000
    ActionListener taskPerformer = new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
           //...Perform a task...
          render();
        }
     };
     new Timer(delay, taskPerformer).start();

     requestFocusInWindow();     
   }
   
     private class ButtonListener implements ActionListener
     {
       public void actionPerformed(ActionEvent e)
       {
       
       }
     }
       public void keyPressed(KeyEvent e)
       {
          int press = e.getKeyCode();
          if (press == KeyEvent.VK_UP)
          { 
             theJet.move(JET_SPEED);
            
          }
          if (press == KeyEvent.VK_DOWN)
          {
             theJet.move(0-JET_SPEED);
            
          }
          if (press == KeyEvent.VK_LEFT)
          {
             theJet.rotate (0-ROTATION_SPEED);
            
          }
          if (press == KeyEvent.VK_RIGHT)
          {
             theJet.rotate(ROTATION_SPEED);
            
          }
       }
       public void keyTyped(KeyEvent e)
       {
      
       }
       public void keyReleased(KeyEvent e)
       {
      
       }
    
     public void paint(Graphics brush)
     {
       if (theJet!= null)
       {
          theJet.paint(brush);
       }
       for (int i = 0; i < buildings.size(); i++)
       {
         
          if (buildings.get(i) == null)
             continue;
          if (theJet.collides(buildings.get(i)))
          {
             livesLeft--;
             theJet.reset();          
          }
          buildings.get(i).move(BUILDING_SPEED);

          buildings.get(i).paint(brush);
      }
     }
     public void render(){
         //renders # of frames in the background then shows them in order
        //the parameter is the number of frames, that are cycled through
        createBufferStrategy(2);
         strategy = getBufferStrategy();
         Graphics g = null;
         do {
             try{
                  g =  strategy.getDrawGraphics();

             } finally {
//SEH - This line being here is the cause of the disappearing graphics on my laptops.  I moved it down but don't know
//  if this will destroy the fact that this made it work on the school laptops.
//  Perhaps the working or not working depends on whether the processor is fast enough to repaint before the dispose
//  (or garbage collector?) actually clears the Graphics object g?
                g.dispose();
                 paint(g);
             }
             strategy.show();
             g.dispose();
         } while (strategy.contentsLost());
         Toolkit.getDefaultToolkit().sync();
   }
  
     public static void main(String[] args)
     {
       new FINALGAME();
     }  

}