נבנה על ידיBuilt by Ariel Yohimovich
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.
JFrame ו-JPanel, ציור עם Graphics2D, וה-game loop: עדכון, ציור, המתנה. בסוף המפגש מוסיפים צעד לפרויקט המלווה.JFrame and JPanel, drawing with Graphics2D, and the game loop: update, draw, wait. At the end we add a step to the companion project.מה נעשה היוםToday's plan
- מונחיםTerms
- חלון עם כדור קופץA window with a bouncing ball
- אותו ציור, על תמונהThe same drawing, on an image
- הלוגיקה לבדThe logic alone
- פרויקט מדףShelf project
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.המונחים של המפגש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.
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.Before the code, the words. Every term here is also linked in the course glossary.נושא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.
Timer של Swing קורא כל 16ms (בערך 60 פעמים בשנייה) ל-update ול-repaint. Swing קורא ל-paintComponent, שמצייר את הכדור במקום החדש. זו לולאת המשחק.A Swing Timer calls update and repaint every 16ms (about 60 times a second). 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.
Graphics2D מצייר גם על BufferedImage. התוכנית רצה באמת בזמן בניית האתר (headless), ושמרה את התמונה שאתם רואים מתחת לפלט.Graphics2D draws on a BufferedImage too. 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.
בלי חלון ובלי ציור: רק Ball.update. כך רואים בסימולציה בדיוק מה קורה בכל טיק: x עולה, עובר את הקיר, ו-vx מתהפך.No window and no drawing: just Ball.update. 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
שלושה כדורים במשטח 100x100. update מחזירה true כשהכדור נגע בקיר. אחרי 10 טיקים הדפיסו את מספר ההתנגשויות ואת המיקום של כל כדור. זו הלוגיקה של המשחק; במחשב שלכם חברו אותה לחלון מהתזכורת הראשונה.Three balls on a 100x100 field. update returns true when the ball hit a wall. After 10 ticks print the number of bounces and each ball's position. This is the game's logic; on your computer, connect it to the window from the first refresher.JFrame ו-JPanel: JFrame הוא החלון; JPanel הוא משטח בתוכו.JFrame and JPanel: JFrame is the window; JPanel is a surface inside it.
המונחים של המפגשThis session's termsGraphics2D: "מכחול" של Java:Graphics2D: Java's "brush":
המונחים של המפגשThis session's termsלולאת משחק: הלב של כל משחק:game loop: The heart of every game:
המונחים של המפגשThis session's termsלוגיקת משחק: המצב והכללים של המשחק (מיקום, מהירות, התנגשויות), בלי שום ציור.game logic: The game's state and rules (position, speed, collisions), with no drawing at all.
המונחים של המפגשThis session's termsheadless: הרצה בלי מסך.headless: Running with no screen.
המונחים של המפגשThis session's termsTimer של Swing קורא כל 16ms (בערך 60 פעמים בשנייה) ל-update ול-repaint.A Swing Timer calls update and repaint every 16ms (about 60 times a second).
חלון עם כדור קופץA window with a bouncing ballSwing קורא ל-paintComponent, שמצייר את הכדור במקום החדש.Swing calls paintComponent, which draws the ball in its new place.
חלון עם כדור קופץA window with a bouncing ballחלון עם כדור קופץA window with a bouncing ballGraphics2D מצייר גם על BufferedImage.Graphics2D draws on a BufferedImage too.
אותו ציור, על תמונהThe same drawing, on an imageהתוכנית רצה באמת בזמן בניית האתר (headless), ושמרה את התמונה שאתם רואים מתחת לפלט.The program really ran while building the site (headless), and saved the picture you see below the output.
אותו ציור, על תמונהThe same drawing, on an imageבלי חלון ובלי ציור: רק Ball.update.No window and no drawing: just Ball.update.
הלוגיקה לבדThe logic aloneכך רואים בסימולציה בדיוק מה קורה בכל טיק: x עולה, עובר את הקיר, ו-vx מתהפך.That way the simulation shows exactly what happens on each tick: x grows, passes the wall, and vx flips.
הלוגיקה לבדThe logic aloneמשחק 1: כדורים קופציםGame 1: bouncing balls
הצעד של היום בפרויקט מדףToday's step in the Shelf projectבאיזה כיוון y גדל בציור של Java?Which way does y grow in Java drawing?
- כלפי מטהDownward
- כלפי מעלהUpward
- ימינהTo the right
- תלוי במחשבIt depends on the computer
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.כלפי מטהDownward
נכון: (0,0) בפינה השמאלית העליונה.Right: (0,0) is the top-left corner.מה שני השלבים שחוזרים בכל סיבוב של לולאת משחק?Which two steps repeat in every round of a game loop?
- עדכון וציורUpdate and draw
- קומפילציה והרצהCompile and run
- פתיחה וסגירהOpen and close
- שמירה וטעינהSave and load
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.עדכון וציורUpdate and draw
נכון.Right.למה להפריד את הלוגיקה מהציור?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
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.כדי לבדוק ולשנות אותה בלי חלוןTo test and change it without a window
נכון.Right.שלושה טיקיםThree ticks
- כדור ב-x=0 עם מהירות 5.A ball at x=0 with speed 5.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.קיר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).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מסך של תוויםA screen of characters
render(x, width) מחזירה שורה כמו |.o..|.render(x, width) returns a line like |.o..|.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.צייד באגים: כדור בורחBug hunter: an escaping ball
- הכדור מתחיל ב-90 עם מהירות 4, והבדיקה היא
x == 100.The ball starts at 90 with speed 4, and the check is x == 100.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.משחק 1: כדורים קופציםGame 1: bouncing balls
- שלושה כדורים במשטח 100x100.Three balls on a 100x100 field.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.כוח משיכהGravity
- כדור נופל:A falling ball:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מסך דו-ממדיA 2D screen
- מסך 8x4 של
char, שני כדורים שזזים באלכסון וקופצים מהקירות.An 8x4 char screen, two balls moving diagonally and bouncing off the walls.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.x += vx;x += vx;
שלושה טיקיםThree ticksהכדור עובר מעט את הקיר (110) ורק אז חוזר: כך עובדת בדיקה אחרי הזזה.The ball passes the wall a little (110) and only then returns: that's how checking after moving works.
קירA wallמה למדנו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
כל אחד אומר דבר אחד שלמד היום.Everyone says one thing they learned today.עכשיו לתרגולNow, practice
5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
מפנים לעמוד התרגול.Point to the practice page.