📄 canvas.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//----------------------
import java.util.List;
import java.util.*;
public class Canvas
{
private static final String VERSION = "Version 1.0";
private static Canvas canvasStart;
private GameEngine gameengine;
// Start canvas with the setup below.
public static Canvas getCanvas()
{
if(canvasStart == null) {
canvasStart = new Canvas("Snakes & Ladders - By Reece Warrender", 450, 373,
Color.white);
}
canvasStart.setVisible(true);
return canvasStart;
}
// ----- instance part -----
private JFrame frame;
private CanvasPane canvas;
private Graphics2D graphic;
private Color backgroundColour;
private Image canvasImage;
private List objects;
private HashMap shapes;
// Create a Canvas.
private Canvas(String title, int width, int height, Color bgColour)
{
frame = new JFrame();
canvas = new CanvasPane();
frame.setContentPane(canvas);
frame.setTitle(title);
canvas.setPreferredSize(new Dimension(width, height));
backgroundColour = bgColour;
frame.pack();
objects = new ArrayList();
shapes = new HashMap();
makeMenuBar(frame);
gameengine = new GameEngine();
}
// Set the canvas visible (to the front).
public void setVisible(boolean visible)
{
if(graphic == null) {
// first time: instantiate the offscreen image and fill it with
// the background colour
Dimension size = canvas.getSize();
canvasImage = canvas.createImage(size.width, size.height);
graphic = (Graphics2D)canvasImage.getGraphics();
graphic.setColor(backgroundColour);
graphic.fillRect(0, 0, size.width, size.height);
graphic.setColor(Color.black);
}
frame.setVisible(visible);
}
// Drawing the objects
public void draw(Object referenceObject, String color, Shape shape)
{
objects.remove(referenceObject); // just in case it was already there
objects.add(referenceObject); // add at the end
shapes.put(referenceObject, new ShapeDescription(shape, color));
redraw();
}
// Erasing objects from the screen.
public void erase(Object referenceObject)
{
objects.remove(referenceObject); // just in case it was already there
shapes.remove(referenceObject);
redraw();
}
// Sets the colour - Default value is colour BLACK.
public void setForegroundColor(String colorString)
{
if(colorString.equals("red"))
graphic.setColor(Color.red);
else if(colorString.equals("black"))
graphic.setColor(Color.black);
else if(colorString.equals("green"))
graphic.setColor(Color.green);
else if(colorString.equals("gray"))
graphic.setColor(Color.lightGray);
else if(colorString.equals("white"))
graphic.setColor(Color.white);
else
graphic.setColor(Color.black);
}
// May be used for animation.
/**public void wait(int milliseconds)
{
try
{
Thread.sleep(milliseconds);
}
catch (Exception e)
{
// ignoring exception at the moment
}
}*/
// Redraw all the shapes on the canvas.
private void redraw()
{
erase();
for(Iterator i=objects.iterator(); i.hasNext(); ) {
((ShapeDescription)shapes.get(i.next())).draw(graphic);
}
canvas.repaint();
}
// Erase all the canvas
private void erase()
{
Color original = graphic.getColor();
graphic.setColor(backgroundColour);
Dimension size = canvas.getSize();
graphic.fill(new Rectangle(0, 0, size.width, size.height));
graphic.setColor(original);
}
// The actual canvas, tis is essentually a JPanel with added refresh ability.
private class CanvasPane extends JPanel
{
public void paint(Graphics g)
{
g.drawImage(canvasImage, 0, 0, null);
}
}
// The actual canvas, tis is essentually a JPanel with added refresh ability.
private class ShapeDescription
{
private Shape shape;
private String colorString;
public ShapeDescription(Shape shape, String color)
{
this.shape = shape;
colorString = color;
}
public void draw(Graphics2D graphic)
{
setForegroundColor(colorString);
graphic.fill(shape);
}
}
// Create a menu bar at the top of the page
public void makeMenuBar(JFrame frame){
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
// create the File menu
JMenu fileMenu = new JMenu("Game");
menubar.add(fileMenu);
//JMenuItem newgameItem = new JMenuItem("New Game");
// newgameItem.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent e) { newgame(); }
// });
//fileMenu.add(newgameItem);
JMenuItem rollItem = new JMenuItem("Roll");
rollItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { roll(); }
});
fileMenu.add(rollItem);
JMenuItem quitItem = new JMenuItem("Quit");
quitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { quit(); }
});
fileMenu.add(quitItem);
// Place the newxt menu choice to the Right.
menubar.add(Box.createHorizontalGlue());
// create the Help menu
JMenu helpMenu = new JMenu("Help");
menubar.add(helpMenu);
//item = new JMenuItem("About Snakes & Ladders");
JMenuItem helpItem = new JMenuItem("About Snakes & Ladders");
helpItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { showAbout(); }
});
helpMenu.add(helpItem);
}
//Function New Game - In the Menu Bar
//private void newgame()
//{
// gameengine.NewGame();
//}
//Function Roll - In the Menu Bar
private void roll()
{
gameengine.RunGame();
}
// Function Quit - In the Menu Bar
private void quit()
{
System.exit(0);
}
// Function About - In the Menu Bar (To the right)
private void showAbout()
{
JOptionPane.showMessageDialog(frame,
"Snakes and Ladders\nBy Reece Warrender\n" + VERSION,
"About Snakes and Ladders",
JOptionPane.INFORMATION_MESSAGE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -