נבנה על ידיBuilt by Ariel Yohimovich

JavaJava
43
חלק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.

או Ctrl+Por Ctrl+P

הנקודות החשובותKey points

  • JFrame ו-JPanel: JFrame הוא החלון; JPanel הוא משטח בתוכו.JFrame and JPanel: JFrame is the window; JPanel is a surface inside it.
  • Graphics2D: "מכחול" של Java:Graphics2D: Java's "brush":
  • Timer של Swing קורא כל 16ms (בערך 60 פעמים בשנייה) ל-update ול-repaint.A Swing Timer calls update and repaint every 16ms (about 60 times a second).
  • Swing קורא ל-paintComponent, שמצייר את הכדור במקום החדש.Swing calls paintComponent, which draws the ball in its new place.
  • Graphics2D מצייר גם על BufferedImage.Graphics2D draws on a BufferedImage too.
  • התוכנית רצה באמת בזמן בניית האתר (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: just Ball.update.
  • כך רואים בסימולציה בדיוק מה קורה בכל טיק: x עולה, עובר את הקיר, ו-vx מתהפך.That way the simulation shows exactly what happens on each tick: x grows, passes the wall, and vx flips.
  • משחק 1: כדורים קופציםGame 1: bouncing balls

מונחיםTerms

  • JFrame ו-JPanelJFrame and JPanel: JFrame הוא החלון; JPanel הוא משטח בתוכו. דורסים ב-JPanel את paintComponent(Graphics g) כדי לצייר.JFrame is the window; JPanel is a surface inside it. Override the JPanel's paintComponent(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 a javax.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 a BufferedImage and 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) כדי לצייר.JFrame is the window; JPanel is a surface inside it. Override the JPanel's paintComponent(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 a javax.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 a BufferedImage and 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

נבנה על ידיBuilt by Ariel Yohimovich