SlideShare a Scribd company logo
1 of 10
Download to read offline
THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE |
Advanced Programming Lab Notes
Brick Breaker Game In Java
1 RESOURCES
• Package java.awt
o http://docs.oracle.com/javase/8/docs/api/java/awt/package-frame.html
• Package javax.swing
o http://docs.oracle.com/javase/8/docs/api/javax/swing/package-frame.html
• Defining and starting thread
o https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html
2 LAB SESSION
2.1 CREATE A NEW PROJECT
• Select File->New->Java Project
• “Under Create a Java Project” dialog box, for the
Project Name field, type in BrickBreaker-add as project
name
2.2 CREATE A MAIN CLASS
• Under the “src” package
create Main class
• Add the code snippet shown below to the Main class.
THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE |
• Now you can get the screen
• We will use 8 brick at each row
• Each brick has a dimension 60x25
• Therefore the dimension of the frame is
490x400
• We will use pillButton image for brick
image
• There are 4 brick color
o Blue
o Green
o Red
o Yellow
2.3 BRICK CLASS
• We need to create Brick class to create brick object
• Create new class called Brick under the “src” package.
THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE |
• This class also use as a
container for ball (brick
breaker) and paddle (user
controller)
o x, y: Brick’s coordinate on
the frame
o width, height: width and
height of the brick
o pic: pill button image
that display on the brick.
o s: is the path for the
image file
2.4 BRICKBREAKERPANEL CLASS
• We need to add BrickBreakerPanel class as a container for the bricks.
• First we need to create BrickBreakerPanel class as a new class under the “src” package
• 2 pixel spaces between two bricks.
THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE |
• To show the bricks we need to
add code that shown on the left side to
Main class
• Than we get the screen shown below
2.5 ANIMATE THE OBJECTS
• We need to create runnable Animate class to animate the objects on the screen.
THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE |
• Animate class implements Runnable because we want to use threads.
• Create the Animate class under the “src” package that implements Runnable interface.
• We need to add “update” method to BrickBreakerPanel class to repaint the screen. And also need to add the
“super.paintComponent(g);” in paintComponent methods to erase and repaint the screen.
2.6 ADD USER INPUTS (ADD PADDLE)
• Now we‘re going to add user input.
• User controls the ball by paddle during the game. Then we need to add paddle to game.
• Draw the paddle on the screen
THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE |
• Add the KeyListener interface to the BirckBreakerPanel class.
• Also add unimplemented
methods.
• To control the paddle we need to get user input from keyboard through key listener. When the user press Left key
paddle going to move to the left (15 px), and when the user press the right button paddle going to move to the
right (15px).
• Add animate object and also the thread to the BrickBreakerPanel class constructor.
• Add key listener to the BrickBreakerPanel class constructor to read the keys.
THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE |
• To start the game when the user press the “Enter”
key. Add code to the keyPressed method.
• Create animate object, create thread with the
animate object and then start the thread to start the
animation/game.
• When you run the code you can see that the paddle is going to out of the screen. To prevent this;
2.7 ADD BALL
• Add array list for ball object in BrickBreakerPanel Class.
• Add the first ball. Width and height of the ball is 25 px.
THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE |
• Paint the ball on the screen
• Run the project.
2.8 INTERACTION WITH THE PADDLE AND SCREEN BORDERS
• We can specify the displacement dx and dy in each coordinate direction by the difference in 3 px.
• Add dx, dy integer variables for the movement in Brick class.
• In update thread, add the code snippet for the ball movement between the screen borders. If the ball intersects
the paddle, ball going to flipped the Y coordinate direction.
THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE |
• Run the project. You can see that the ball moving between the screen borders but not destroy any bricks.
• Ball can start the movement and left the screen.
2.9 DESTROYING THE BRICKS
• Create Boolean variable “destroy” in Brick class. Default value is “False”.
• If the destroy field equals to “False” then the brick will appear on the screen, otherwise won’t.
• In update thread, if the ball intersects the brick and the brick is not destroyed then this brick going to be
destroyed. Then change the direction of the ball.
• Run the project and have fun.
THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE |
3 CONTACT
Dr. Celal Murat KANDEMİR
Eskisehir Osmangazi University
Faculty of Education
kandemir@ogu.edu.tr
twitter.com/cmkandemir

More Related Content

Viewers also liked

Canvas Öğrenme Yönetim Sistemi Kullanım Kılavuzu Bölüm 1
Canvas Öğrenme Yönetim Sistemi Kullanım Kılavuzu Bölüm 1Canvas Öğrenme Yönetim Sistemi Kullanım Kılavuzu Bölüm 1
Canvas Öğrenme Yönetim Sistemi Kullanım Kılavuzu Bölüm 1cmkandemir
 
CSS Uygulamaları 1
CSS Uygulamaları 1CSS Uygulamaları 1
CSS Uygulamaları 1cmkandemir
 
Web Sitesi Geliştirme Adımları
Web Sitesi Geliştirme AdımlarıWeb Sitesi Geliştirme Adımları
Web Sitesi Geliştirme Adımlarıcmkandemir
 
Matching Game In Java
Matching Game In JavaMatching Game In Java
Matching Game In Javacmkandemir
 
PHP ve MySQL Bağlantısı - Temel İşlemler
PHP ve MySQL Bağlantısı - Temel İşlemlerPHP ve MySQL Bağlantısı - Temel İşlemler
PHP ve MySQL Bağlantısı - Temel İşlemlercmkandemir
 
Chapter 6 - Introduction to 8085 Instructions
Chapter 6 - Introduction to 8085 InstructionsChapter 6 - Introduction to 8085 Instructions
Chapter 6 - Introduction to 8085 Instructionscmkandemir
 
İnternet Tabanlı Programlama Uygulama Notları
İnternet Tabanlı Programlama Uygulama Notlarıİnternet Tabanlı Programlama Uygulama Notları
İnternet Tabanlı Programlama Uygulama Notlarıcmkandemir
 
Chapter 7 - Programming Techniques with Additional Instructions
Chapter 7 - Programming Techniques with Additional InstructionsChapter 7 - Programming Techniques with Additional Instructions
Chapter 7 - Programming Techniques with Additional Instructionscmkandemir
 
openCV and Java - Face Detection
openCV and Java - Face DetectionopenCV and Java - Face Detection
openCV and Java - Face Detectioncmkandemir
 
impress.js Framework
impress.js Frameworkimpress.js Framework
impress.js Frameworkcmkandemir
 
Kod Akış Kontrolü - Döngüler, Fonksiyonlar
Kod Akış Kontrolü - Döngüler, FonksiyonlarKod Akış Kontrolü - Döngüler, Fonksiyonlar
Kod Akış Kontrolü - Döngüler, Fonksiyonlarcmkandemir
 
Canvas Öğrenme Yönetim Sistemi Kullanım Kılavuzu Bölüm 2
Canvas Öğrenme Yönetim Sistemi Kullanım Kılavuzu Bölüm 2Canvas Öğrenme Yönetim Sistemi Kullanım Kılavuzu Bölüm 2
Canvas Öğrenme Yönetim Sistemi Kullanım Kılavuzu Bölüm 2cmkandemir
 
JDK and Eclipse Installation and Configuration
JDK and Eclipse Installation and ConfigurationJDK and Eclipse Installation and Configuration
JDK and Eclipse Installation and Configurationcmkandemir
 
CSS - Sunum Bileşenleri
CSS - Sunum BileşenleriCSS - Sunum Bileşenleri
CSS - Sunum Bileşenlericmkandemir
 

Viewers also liked (15)

Canvas Öğrenme Yönetim Sistemi Kullanım Kılavuzu Bölüm 1
Canvas Öğrenme Yönetim Sistemi Kullanım Kılavuzu Bölüm 1Canvas Öğrenme Yönetim Sistemi Kullanım Kılavuzu Bölüm 1
Canvas Öğrenme Yönetim Sistemi Kullanım Kılavuzu Bölüm 1
 
CSS Uygulamaları 1
CSS Uygulamaları 1CSS Uygulamaları 1
CSS Uygulamaları 1
 
Web Sitesi Geliştirme Adımları
Web Sitesi Geliştirme AdımlarıWeb Sitesi Geliştirme Adımları
Web Sitesi Geliştirme Adımları
 
Matching Game In Java
Matching Game In JavaMatching Game In Java
Matching Game In Java
 
PHP ve MySQL Bağlantısı - Temel İşlemler
PHP ve MySQL Bağlantısı - Temel İşlemlerPHP ve MySQL Bağlantısı - Temel İşlemler
PHP ve MySQL Bağlantısı - Temel İşlemler
 
Chapter 6 - Introduction to 8085 Instructions
Chapter 6 - Introduction to 8085 InstructionsChapter 6 - Introduction to 8085 Instructions
Chapter 6 - Introduction to 8085 Instructions
 
İnternet Tabanlı Programlama Uygulama Notları
İnternet Tabanlı Programlama Uygulama Notlarıİnternet Tabanlı Programlama Uygulama Notları
İnternet Tabanlı Programlama Uygulama Notları
 
Chapter 7 - Programming Techniques with Additional Instructions
Chapter 7 - Programming Techniques with Additional InstructionsChapter 7 - Programming Techniques with Additional Instructions
Chapter 7 - Programming Techniques with Additional Instructions
 
PHP Temelleri
PHP TemelleriPHP Temelleri
PHP Temelleri
 
openCV and Java - Face Detection
openCV and Java - Face DetectionopenCV and Java - Face Detection
openCV and Java - Face Detection
 
impress.js Framework
impress.js Frameworkimpress.js Framework
impress.js Framework
 
Kod Akış Kontrolü - Döngüler, Fonksiyonlar
Kod Akış Kontrolü - Döngüler, FonksiyonlarKod Akış Kontrolü - Döngüler, Fonksiyonlar
Kod Akış Kontrolü - Döngüler, Fonksiyonlar
 
Canvas Öğrenme Yönetim Sistemi Kullanım Kılavuzu Bölüm 2
Canvas Öğrenme Yönetim Sistemi Kullanım Kılavuzu Bölüm 2Canvas Öğrenme Yönetim Sistemi Kullanım Kılavuzu Bölüm 2
Canvas Öğrenme Yönetim Sistemi Kullanım Kılavuzu Bölüm 2
 
JDK and Eclipse Installation and Configuration
JDK and Eclipse Installation and ConfigurationJDK and Eclipse Installation and Configuration
JDK and Eclipse Installation and Configuration
 
CSS - Sunum Bileşenleri
CSS - Sunum BileşenleriCSS - Sunum Bileşenleri
CSS - Sunum Bileşenleri
 

Similar to Threads and Game Programming In Java

Polybot Onboarding Process
Polybot Onboarding ProcessPolybot Onboarding Process
Polybot Onboarding ProcessNina Park
 
X unit testing framework with c# and vs code
X unit testing framework with c# and vs codeX unit testing framework with c# and vs code
X unit testing framework with c# and vs codeShashank Tiwari
 
Containers Lab
Containers Lab Containers Lab
Containers Lab Dev_Events
 
Hacking the Kinect with GAFFTA Day 1
Hacking the Kinect with GAFFTA Day 1Hacking the Kinect with GAFFTA Day 1
Hacking the Kinect with GAFFTA Day 1benDesigning
 
How To Create .ipa files for TestFlight
How To Create .ipa files for TestFlightHow To Create .ipa files for TestFlight
How To Create .ipa files for TestFlightKuldeep Singh
 
Keynote + Next Gen UIs.pptx
Keynote + Next Gen UIs.pptxKeynote + Next Gen UIs.pptx
Keynote + Next Gen UIs.pptxEqraKhattak
 
Streamlining React Component Development and Sharing with bit.pptx
Streamlining React Component Development and Sharing with bit.pptxStreamlining React Component Development and Sharing with bit.pptx
Streamlining React Component Development and Sharing with bit.pptxShubhamJayswal6
 
Building GPU-Accelerated Mobile Application Interfaces with Starling and Feat...
Building GPU-Accelerated Mobile Application Interfaces with Starling and Feat...Building GPU-Accelerated Mobile Application Interfaces with Starling and Feat...
Building GPU-Accelerated Mobile Application Interfaces with Starling and Feat...Joseph Labrecque
 
HoloLens Unity Build Pipelines on Azure DevOps
HoloLens Unity Build Pipelines on Azure DevOpsHoloLens Unity Build Pipelines on Azure DevOps
HoloLens Unity Build Pipelines on Azure DevOpsSarah Sexton
 
Vb.net session 15
Vb.net session 15Vb.net session 15
Vb.net session 15Niit Care
 
Scaling Your App With Docker Swarm using Terraform, Packer on Openstack
Scaling Your App With Docker Swarm using Terraform, Packer on OpenstackScaling Your App With Docker Swarm using Terraform, Packer on Openstack
Scaling Your App With Docker Swarm using Terraform, Packer on OpenstackBobby DeVeaux, DevOps Consultant
 
Revit drafting procedure
Revit drafting procedureRevit drafting procedure
Revit drafting procedureAnima M C
 

Similar to Threads and Game Programming In Java (20)

Polybot Onboarding Process
Polybot Onboarding ProcessPolybot Onboarding Process
Polybot Onboarding Process
 
Jenkins CI
Jenkins CIJenkins CI
Jenkins CI
 
X unit testing framework with c# and vs code
X unit testing framework with c# and vs codeX unit testing framework with c# and vs code
X unit testing framework with c# and vs code
 
Containers Lab
Containers Lab Containers Lab
Containers Lab
 
visualbasic.ppt
visualbasic.pptvisualbasic.ppt
visualbasic.ppt
 
Hacking the Kinect with GAFFTA Day 1
Hacking the Kinect with GAFFTA Day 1Hacking the Kinect with GAFFTA Day 1
Hacking the Kinect with GAFFTA Day 1
 
How To Create .ipa files for TestFlight
How To Create .ipa files for TestFlightHow To Create .ipa files for TestFlight
How To Create .ipa files for TestFlight
 
DotNetNuke
DotNetNukeDotNetNuke
DotNetNuke
 
Keynote + Next Gen UIs.pptx
Keynote + Next Gen UIs.pptxKeynote + Next Gen UIs.pptx
Keynote + Next Gen UIs.pptx
 
Chatbots
ChatbotsChatbots
Chatbots
 
Streamlining React Component Development and Sharing with bit.pptx
Streamlining React Component Development and Sharing with bit.pptxStreamlining React Component Development and Sharing with bit.pptx
Streamlining React Component Development and Sharing with bit.pptx
 
Building GPU-Accelerated Mobile Application Interfaces with Starling and Feat...
Building GPU-Accelerated Mobile Application Interfaces with Starling and Feat...Building GPU-Accelerated Mobile Application Interfaces with Starling and Feat...
Building GPU-Accelerated Mobile Application Interfaces with Starling and Feat...
 
HoloLens Unity Build Pipelines on Azure DevOps
HoloLens Unity Build Pipelines on Azure DevOpsHoloLens Unity Build Pipelines on Azure DevOps
HoloLens Unity Build Pipelines on Azure DevOps
 
Vb.net session 15
Vb.net session 15Vb.net session 15
Vb.net session 15
 
Building Containers: How Many Ways Are Too Many?
Building Containers: How Many Ways Are Too Many?Building Containers: How Many Ways Are Too Many?
Building Containers: How Many Ways Are Too Many?
 
Scaling Your App With Docker Swarm using Terraform, Packer on Openstack
Scaling Your App With Docker Swarm using Terraform, Packer on OpenstackScaling Your App With Docker Swarm using Terraform, Packer on Openstack
Scaling Your App With Docker Swarm using Terraform, Packer on Openstack
 
Kate's.pptx
Kate's.pptxKate's.pptx
Kate's.pptx
 
Revit drafting procedure
Revit drafting procedureRevit drafting procedure
Revit drafting procedure
 
Dr. Strangelove, or how I learned to love plug-in development - SNoUG 2014
Dr. Strangelove, or how I learned to love plug-in development - SNoUG 2014Dr. Strangelove, or how I learned to love plug-in development - SNoUG 2014
Dr. Strangelove, or how I learned to love plug-in development - SNoUG 2014
 
Mini .net conf 2020
Mini .net conf 2020Mini .net conf 2020
Mini .net conf 2020
 

More from cmkandemir

Temel HTML Etiketleri ve Kullanım Örnekleri
Temel HTML Etiketleri ve Kullanım ÖrnekleriTemel HTML Etiketleri ve Kullanım Örnekleri
Temel HTML Etiketleri ve Kullanım Örneklericmkandemir
 
Yapay Zeka Nedir?
Yapay Zeka Nedir?Yapay Zeka Nedir?
Yapay Zeka Nedir?cmkandemir
 
Zekayı Anlamak
Zekayı AnlamakZekayı Anlamak
Zekayı Anlamakcmkandemir
 
PHP - Kullanıcı Girişlerinin İşlenmesi
PHP - Kullanıcı Girişlerinin İşlenmesiPHP - Kullanıcı Girişlerinin İşlenmesi
PHP - Kullanıcı Girişlerinin İşlenmesicmkandemir
 
Chapter 2-8085 Microprocessor Architecture and Microcomputer Systems
Chapter 2-8085 Microprocessor Architecture and Microcomputer SystemsChapter 2-8085 Microprocessor Architecture and Microcomputer Systems
Chapter 2-8085 Microprocessor Architecture and Microcomputer Systemscmkandemir
 
Chapter 1-Microprocessors, Microcomputers, and Assembly Language
Chapter 1-Microprocessors, Microcomputers, and Assembly LanguageChapter 1-Microprocessors, Microcomputers, and Assembly Language
Chapter 1-Microprocessors, Microcomputers, and Assembly Languagecmkandemir
 
CSS - Genel Bakış
CSS - Genel BakışCSS - Genel Bakış
CSS - Genel Bakışcmkandemir
 
Temel HTML Etiketleri - Tablo, Form
Temel HTML Etiketleri - Tablo, FormTemel HTML Etiketleri - Tablo, Form
Temel HTML Etiketleri - Tablo, Formcmkandemir
 
Temel HTML Etiketleri - Text, Image, Link, List, Image
Temel HTML Etiketleri - Text, Image, Link, List, ImageTemel HTML Etiketleri - Text, Image, Link, List, Image
Temel HTML Etiketleri - Text, Image, Link, List, Imagecmkandemir
 
Vaadin JPAContainer
Vaadin JPAContainerVaadin JPAContainer
Vaadin JPAContainercmkandemir
 

More from cmkandemir (10)

Temel HTML Etiketleri ve Kullanım Örnekleri
Temel HTML Etiketleri ve Kullanım ÖrnekleriTemel HTML Etiketleri ve Kullanım Örnekleri
Temel HTML Etiketleri ve Kullanım Örnekleri
 
Yapay Zeka Nedir?
Yapay Zeka Nedir?Yapay Zeka Nedir?
Yapay Zeka Nedir?
 
Zekayı Anlamak
Zekayı AnlamakZekayı Anlamak
Zekayı Anlamak
 
PHP - Kullanıcı Girişlerinin İşlenmesi
PHP - Kullanıcı Girişlerinin İşlenmesiPHP - Kullanıcı Girişlerinin İşlenmesi
PHP - Kullanıcı Girişlerinin İşlenmesi
 
Chapter 2-8085 Microprocessor Architecture and Microcomputer Systems
Chapter 2-8085 Microprocessor Architecture and Microcomputer SystemsChapter 2-8085 Microprocessor Architecture and Microcomputer Systems
Chapter 2-8085 Microprocessor Architecture and Microcomputer Systems
 
Chapter 1-Microprocessors, Microcomputers, and Assembly Language
Chapter 1-Microprocessors, Microcomputers, and Assembly LanguageChapter 1-Microprocessors, Microcomputers, and Assembly Language
Chapter 1-Microprocessors, Microcomputers, and Assembly Language
 
CSS - Genel Bakış
CSS - Genel BakışCSS - Genel Bakış
CSS - Genel Bakış
 
Temel HTML Etiketleri - Tablo, Form
Temel HTML Etiketleri - Tablo, FormTemel HTML Etiketleri - Tablo, Form
Temel HTML Etiketleri - Tablo, Form
 
Temel HTML Etiketleri - Text, Image, Link, List, Image
Temel HTML Etiketleri - Text, Image, Link, List, ImageTemel HTML Etiketleri - Text, Image, Link, List, Image
Temel HTML Etiketleri - Text, Image, Link, List, Image
 
Vaadin JPAContainer
Vaadin JPAContainerVaadin JPAContainer
Vaadin JPAContainer
 

Recently uploaded

Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 

Recently uploaded (20)

9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 

Threads and Game Programming In Java

  • 1. THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE | Advanced Programming Lab Notes Brick Breaker Game In Java 1 RESOURCES • Package java.awt o http://docs.oracle.com/javase/8/docs/api/java/awt/package-frame.html • Package javax.swing o http://docs.oracle.com/javase/8/docs/api/javax/swing/package-frame.html • Defining and starting thread o https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html 2 LAB SESSION 2.1 CREATE A NEW PROJECT • Select File->New->Java Project • “Under Create a Java Project” dialog box, for the Project Name field, type in BrickBreaker-add as project name 2.2 CREATE A MAIN CLASS • Under the “src” package create Main class • Add the code snippet shown below to the Main class.
  • 2. THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE | • Now you can get the screen • We will use 8 brick at each row • Each brick has a dimension 60x25 • Therefore the dimension of the frame is 490x400 • We will use pillButton image for brick image • There are 4 brick color o Blue o Green o Red o Yellow 2.3 BRICK CLASS • We need to create Brick class to create brick object • Create new class called Brick under the “src” package.
  • 3. THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE | • This class also use as a container for ball (brick breaker) and paddle (user controller) o x, y: Brick’s coordinate on the frame o width, height: width and height of the brick o pic: pill button image that display on the brick. o s: is the path for the image file 2.4 BRICKBREAKERPANEL CLASS • We need to add BrickBreakerPanel class as a container for the bricks. • First we need to create BrickBreakerPanel class as a new class under the “src” package • 2 pixel spaces between two bricks.
  • 4. THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE | • To show the bricks we need to add code that shown on the left side to Main class • Than we get the screen shown below 2.5 ANIMATE THE OBJECTS • We need to create runnable Animate class to animate the objects on the screen.
  • 5. THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE | • Animate class implements Runnable because we want to use threads. • Create the Animate class under the “src” package that implements Runnable interface. • We need to add “update” method to BrickBreakerPanel class to repaint the screen. And also need to add the “super.paintComponent(g);” in paintComponent methods to erase and repaint the screen. 2.6 ADD USER INPUTS (ADD PADDLE) • Now we‘re going to add user input. • User controls the ball by paddle during the game. Then we need to add paddle to game. • Draw the paddle on the screen
  • 6. THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE | • Add the KeyListener interface to the BirckBreakerPanel class. • Also add unimplemented methods. • To control the paddle we need to get user input from keyboard through key listener. When the user press Left key paddle going to move to the left (15 px), and when the user press the right button paddle going to move to the right (15px). • Add animate object and also the thread to the BrickBreakerPanel class constructor. • Add key listener to the BrickBreakerPanel class constructor to read the keys.
  • 7. THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE | • To start the game when the user press the “Enter” key. Add code to the keyPressed method. • Create animate object, create thread with the animate object and then start the thread to start the animation/game. • When you run the code you can see that the paddle is going to out of the screen. To prevent this; 2.7 ADD BALL • Add array list for ball object in BrickBreakerPanel Class. • Add the first ball. Width and height of the ball is 25 px.
  • 8. THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE | • Paint the ball on the screen • Run the project. 2.8 INTERACTION WITH THE PADDLE AND SCREEN BORDERS • We can specify the displacement dx and dy in each coordinate direction by the difference in 3 px. • Add dx, dy integer variables for the movement in Brick class. • In update thread, add the code snippet for the ball movement between the screen borders. If the ball intersects the paddle, ball going to flipped the Y coordinate direction.
  • 9. THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE | • Run the project. You can see that the ball moving between the screen borders but not destroy any bricks. • Ball can start the movement and left the screen. 2.9 DESTROYING THE BRICKS • Create Boolean variable “destroy” in Brick class. Default value is “False”. • If the destroy field equals to “False” then the brick will appear on the screen, otherwise won’t. • In update thread, if the ball intersects the brick and the brick is not destroyed then this brick going to be destroyed. Then change the direction of the ball. • Run the project and have fun.
  • 10. THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE | 3 CONTACT Dr. Celal Murat KANDEMİR Eskisehir Osmangazi University Faculty of Education kandemir@ogu.edu.tr twitter.com/cmkandemir