Pac-Man
From Xdw
It's freaking Pac-Man!
I don't know why it's retarded.
[edit] The Kuldeep Way
package kulpac;
import java.awt.*; import javax.swing.*; import java.awt.event.*;
/**
* @author Kuldeep Gaindawood * @version 1.0, 2009-06-15 */
public class Pacman extends JFrame implements ActionListener {
JPanel mainPanel; CustomCanvas drawingArea; JButton newGameB, highScoreB;
public static void main(String args[]) { try { UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName()); } catch (Exception ballsAreAnException) { } Pacman frame = new Pacman(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
public Pacman() { super("Pacman Game"); setSize(400, 550); //setLocation (0, 50); setResizable(false);
//////// BUTTONS // newGameB = new JButton (paintMethod1 ()); // highScoreB = new JButton (paintMethod2 ()); //////// TEXTFIELDS
//////// LABELS
//////// LISTS
//////// PANELS
//////// CANVAS
drawingArea = new CustomCanvas(); mainPanel = (JPanel) getContentPane(); mainPanel.setLayout(new BorderLayout()); mainPanel.add(drawingArea); setVisible(true); }
public void actionPerformed(ActionEvent e) { }
}
class CustomCanvas extends Canvas {
public CustomCanvas() { setBackground((Color.black)); }
public void paint(Graphics g) { Graphics2D g2; g2 = (Graphics2D) g; Image img = Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/pac.gif")); g2.drawImage(img, 0, 0, this);
}
}
--38.116.200.43 11:22, 3 June 2009 (PDT)
[edit] The Tyler Way
package engine;
import java.awt.Canvas; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Toolkit; import java.awt.image.BufferStrategy; import javax.swing.JFrame; import javax.swing.JPanel;
/**
* The main game executable. Creates and handle the game window and its enclosed * entities. * * @author Hauskaz * @version 1.0, 2009-05-28 */
public class Main extends Canvas {
/** * The main method. Creates an intstance of this class to get the ball * rolling. * * @param args the command line arguments, ignored in this case */ public static void main(String[] args) { new Main(); }
// the BufferStrategy for accelerated graphics drawing private BufferStrategy strategy; // the master switch for whether the game is running or not private boolean gameRunning = true; // the difference between refreshes, used to determine FPS and how far to // move entities to compensate private long delta; // used for finding the delta private long lastLoopTime = 0; // the graphics context where all the stuff will be drawn on Graphics2D g;
/** * The game's main constructor. Sets up and runs the rendering process. */ Main() { // main window JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// creates a Panel and sets the size of it JPanel masterPanel = (JPanel) frame.getContentPane(); masterPanel.setPreferredSize(new Dimension(800, 600)); masterPanel.setLayout(null);
// sets the size of the canvas and adds it to the pane setBounds(0, 0, 800, 600); masterPanel.add(this);
// turns off Canvas repainting since it will be handled in accelerated // mode setIgnoreRepaint(true);
// size and display the window frame.pack(); frame.setResizable(false); frame.setVisible(true);
// initializes the buffer strategy which will allow AWT to manage // accelerated graphics // double buffering should suffice createBufferStrategy(2); strategy = getBufferStrategy();
Image packer = Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/pac.gif"));
int x = 0; int y = 0;
// game loop while (gameRunning) { // determine the length of time since the last update, used to // determine how far to move entities in this loop delta = System.currentTimeMillis() - lastLoopTime; lastLoopTime = System.currentTimeMillis();
// grab a graphics context and blank it g = (Graphics2D) strategy.getDrawGraphics(); g.setColor(Color.black); g.fillRect(0, 0, 800, 600);
// draw stuff x += 4; g.drawImage(packer, x, y, this);
// drawing is done, dump the graphics and flip the buffer g.dispose(); strategy.show();
// pause (delay should equate to 100 FPS) try { Thread.sleep(10); } catch (Exception e) { System.out.println("Unknown framerate error."); } } }
}