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

חזרה למפגשBack
JavaJava

חלון ראשון: Swing, ציור ו-game loopFirst window: Swing, drawing & the game loop

מפגש 43: JFrame ו-JPanel, ציור עם Graphics2D, וה-game loop: עדכון, ציור, המתנה.Session 43: JFrame and JPanel, drawing with Graphics2D, and the game loop: update, draw, wait.

מה נעשה היוםToday's plan

  • מונחיםTerms
  • חלון עם כדור קופץA window with a bouncing ball
  • אותו ציור, על תמונהThe same drawing, on an image
  • הלוגיקה לבדThe logic alone
  • פרויקט מדףShelf project
נושאTopic 1

מונחיםTerms

המונחים של המפגשThis session's terms

  • JFrame ו-JPanel: JFrame הוא החלון; JPanel הוא משטח בתוכו.JFrame and JPanel: JFrame is the window; JPanel is a surface inside it.
  • Graphics2D: "מכחול" של Java:Graphics2D: Java's "brush":
  • לולאת משחק: הלב של כל משחק:game loop: The heart of every game:
  • לוגיקת משחק: המצב והכללים של המשחק (מיקום, מהירות, התנגשויות), בלי שום ציור.game logic: The game's state and rules (position, speed, collisions), with no drawing at all.
  • headless: הרצה בלי מסך.headless: Running with no screen.
נושאTopic 2

חלון עם כדור קופץA window with a bouncing ball

חלון עם כדור קופץA window with a bouncing ball

  • 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.
  • זו לולאת המשחק.That's the game loop.
נושאTopic 3

אותו ציור, על תמונהThe same drawing, on an image

אותו ציור, על תמונהThe same drawing, on an image

  • 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.
נושאTopic 4

הלוגיקה לבדThe logic alone

הלוגיקה לבדThe logic alone

  • בלי חלון ובלי ציור: רק 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.
נושאTopic 5

פרויקט מדףShelf project

הצעד של היום בפרויקט מדףToday's step in the Shelf project

  • משחק 1: כדורים קופציםGame 1: bouncing balls

JFrame ו-JPanel: JFrame הוא החלון; JPanel הוא משטח בתוכו.JFrame and JPanel: JFrame is the window; JPanel is a surface inside it.

Graphics2D: "מכחול" של Java:Graphics2D: Java's "brush":

לולאת משחק: הלב של כל משחק:game loop: The heart of every game:

לוגיקת משחק: המצב והכללים של המשחק (מיקום, מהירות, התנגשויות), בלי שום ציור.game logic: The game's state and rules (position, speed, collisions), with no drawing at all.

headless: הרצה בלי מסך.headless: Running with no screen.

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.

זו לולאת המשחק.That's the game loop.

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

באיזה כיוון y גדל בציור של Java?Which way does y grow in Java drawing?

  • כלפי מטהDownward
  • כלפי מעלהUpward
  • ימינהTo the right
  • תלוי במחשבIt depends on the computer

כלפי מטהDownward

מה שני השלבים שחוזרים בכל סיבוב של לולאת משחק?Which two steps repeat in every round of a game loop?

  • עדכון וציורUpdate and draw
  • קומפילציה והרצהCompile and run
  • פתיחה וסגירהOpen and close
  • שמירה וטעינהSave and load

עדכון וציורUpdate and draw

למה להפריד את הלוגיקה מהציור?Why separate the logic from the drawing?

  • כדי לבדוק ולשנות אותה בלי חלוןTo test and change it without a window
  • כי Swing מחייבBecause Swing requires it
  • כדי שירוץ מהר יותרSo it runs faster
  • אין סיבהNo reason

כדי לבדוק ולשנות אותה בלי חלוןTo test and change it without a window

שלושה טיקיםThree ticks

  • כדור ב-x=0 עם מהירות 5.A ball at x=0 with speed 5.

קירA wall

  • Ball.update(w, h) מזיזה, ומהפכת את vx כש-x מגיע ל-0 או ל-w (אותו דבר ל-y).Ball.update(w, h) moves, and flips vx when x reaches 0 or w (the same for y).

מסך של תוויםA screen of characters

  • render(x, width) מחזירה שורה כמו |.o..|.render(x, width) returns a line like |.o..|.

צייד באגים: כדור בורחBug hunter: an escaping ball

  • הכדור מתחיל ב-90 עם מהירות 4, והבדיקה היא x == 100.The ball starts at 90 with speed 4, and the check is x == 100.

משחק 1: כדורים קופציםGame 1: bouncing balls

  • שלושה כדורים במשטח 100x100.Three balls on a 100x100 field.

כוח משיכהGravity

  • כדור נופל:A falling ball:

מסך דו-ממדיA 2D screen

  • מסך 8x4 של char, שני כדורים שזזים באלכסון וקופצים מהקירות.An 8x4 char screen, two balls moving diagonally and bouncing off the walls.

x += vx;x += vx;

הכדור עובר מעט את הקיר (110) ורק אז חוזר: כך עובדת בדיקה אחרי הזזה.The ball passes the wall a little (110) and only then returns: that's how checking after moving works.

מה למדנוWhat we learned

  • JFrame ו-JPanel: JFrame הוא החלון; JPanel הוא משטח בתוכו.JFrame and JPanel: JFrame is the window; JPanel is a surface inside it.
  • Timer של Swing קורא כל 16ms (בערך 60 פעמים בשנייה) ל-update ול-repaint.A Swing Timer calls update and repaint every 16ms (about 60 times a second).
  • Graphics2D מצייר גם על BufferedImage.Graphics2D draws on a BufferedImage too.
  • בלי חלון ובלי ציור: רק Ball.update.No window and no drawing: just Ball.update.
  • משחק 1: כדורים קופציםGame 1: bouncing balls

עכשיו לתרגולNow, practice

5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
הערות מרצהSpeaker notes

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