חלקPart 8: משחקים ב-JavaGames in Java
כ-3 שעות (כולל הפסקה)About 3 hours (with a break)
חלון ראשון: Swing, ציור ו-game loopFirst window: Swing, drawing & the game loop
JFrame ו-JPanel, ציור עם Graphics2D, וה-game loop: עדכון, ציור, המתנה.
JFrame and JPanel, drawing with Graphics2D, and the game loop: update, draw, wait.
הנקודות החשובותKey points
- JFrame ו-JPanel:
JFrameהוא החלון;JPanelהוא משטח בתוכו.JFrame and JPanel:JFrameis the window;JPanelis a surface inside it. - Graphics2D: "מכחול" של Java:Graphics2D: Java's "brush":
Timerשל Swing קורא כל 16ms (בערך 60 פעמים בשנייה) ל-updateול-repaint.A SwingTimercallsupdateandrepaintevery 16ms (about 60 times a second).- Swing קורא ל-
paintComponent, שמצייר את הכדור במקום החדש.Swing callspaintComponent, which draws the ball in its new place. Graphics2Dמצייר גם עלBufferedImage.Graphics2Ddraws on aBufferedImagetoo.- התוכנית רצה באמת בזמן בניית האתר (headless), ושמרה את התמונה שאתם רואים מתחת לפלט.The program really ran while building the site (headless), and saved the picture you see below the output.
- בלי חלון ובלי ציור: רק
Ball.update.No window and no drawing: justBall.update. - כך רואים בסימולציה בדיוק מה קורה בכל טיק:
xעולה, עובר את הקיר, ו-vxמתהפך.That way the simulation shows exactly what happens on each tick:xgrows, passes the wall, andvxflips. - משחק 1: כדורים קופציםGame 1: bouncing balls
מונחיםTerms
- JFrame ו-JPanelJFrame and JPanel:
JFrameהוא החלון;JPanelהוא משטח בתוכו. דורסים ב-JPanel אתpaintComponent(Graphics g)כדי לצייר.JFrameis the window;JPanelis a surface inside it. Override the JPanel'spaintComponent(Graphics g)to draw. - Graphics2DGraphics2D: "מכחול" של Java:
setColor,fillRect,fillOval,drawString. ראשית הצירים בפינה השמאלית העליונה, ו-y גדל כלפי מטה.Java's "brush":setColor,fillRect,fillOval,drawString. The origin is the top-left corner, and y grows downward. - לולאת משחקgame loop: הלב של כל משחק: שוב ושוב, בערך 60 פעמים בשנייה, מעדכנים את העולם (
update) ומציירים אותו (repaint). ב-Swing הכי פשוט עםjavax.swing.Timer.The heart of every game: over and over, about 60 times a second, update the world (update) and draw it (repaint). In Swing the simplest is ajavax.swing.Timer. - לוגיקת משחקgame logic: המצב והכללים של המשחק (מיקום, מהירות, התנגשויות), בלי שום ציור. כשהם במחלקה נפרדת אפשר להריץ ולבדוק אותם בלי חלון.The game's state and rules (position, speed, collisions), with no drawing at all. In a separate class you can run and test them with no window.
- headlessheadless: הרצה בלי מסך. Java יכולה לצייר על
BufferedImageולשמור PNG גם בלי חלון: כך נוצרו התמונות באתר.Running with no screen. Java can draw on aBufferedImageand save a PNG with no window: that's how the pictures on the site were made. - JFrame ו-JPanelJFrame and JPanel:
JFrameהוא החלון;JPanelהוא משטח בתוכו. דורסים ב-JPanel אתpaintComponent(Graphics g)כדי לצייר.JFrameis the window;JPanelis a surface inside it. Override the JPanel'spaintComponent(Graphics g)to draw. - Graphics2DGraphics2D: "מכחול" של Java:
setColor,fillRect,fillOval,drawString. ראשית הצירים בפינה השמאלית העליונה, ו-y גדל כלפי מטה.Java's "brush":setColor,fillRect,fillOval,drawString. The origin is the top-left corner, and y grows downward. - לולאת משחקgame loop: הלב של כל משחק: שוב ושוב, בערך 60 פעמים בשנייה, מעדכנים את העולם (
update) ומציירים אותו (repaint). ב-Swing הכי פשוט עםjavax.swing.Timer.The heart of every game: over and over, about 60 times a second, update the world (update) and draw it (repaint). In Swing the simplest is ajavax.swing.Timer. - לוגיקת משחקgame logic: המצב והכללים של המשחק (מיקום, מהירות, התנגשויות), בלי שום ציור. כשהם במחלקה נפרדת אפשר להריץ ולבדוק אותם בלי חלון.The game's state and rules (position, speed, collisions), with no drawing at all. In a separate class you can run and test them with no window.
- headlessheadless: הרצה בלי מסך. Java יכולה לצייר על
BufferedImageולשמור PNG גם בלי חלון: כך נוצרו התמונות באתר.Running with no screen. Java can draw on aBufferedImageand save a PNG with no window: that's how the pictures on the site were made.
Cheatsheet
חלון עם כדור קופץA window with a bouncing ball
JFrame f = new JFrame("Bouncing ball");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Main());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);אותו ציור, על תמונהThe same drawing, on an image
BufferedImage img = new BufferedImage(400, 200, BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(new Color(0xF4F1EA));
g.fillRect(0, 0, 400, 200);
g.setColor(new Color(0x2D5BD0));
g.setStroke(new BasicStroke(4));
g.drawRect(10, 10, 380, 180);
int[][] balls = {{80, 60, 0xC4410E}, {200, 120, 0x0E9F8E}, {310, 70, 0x8A3FFC}};
for (int[] b : balls) {הלוגיקה לבדThe logic alone
Ball b = new Ball(10, 20, 30, 0);
for (int tick = 1; tick <= 4; tick++) {
b.update(100);
System.out.println("tick " + tick + ": x=" + b.x + " vx=" + b.vx);
}שגיאות שפגשנוErrors we met
| שגיאהError | מה זה אומרWhat it means | מה עושיםWhat to do |
|---|