SlideShare a Scribd company logo
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 1
cmkandemir
 
CSS Uygulamaları 1
CSS Uygulamaları 1CSS Uygulamaları 1
CSS Uygulamaları 1
cmkandemir
 
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 Java
cmkandemir
 
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
cmkandemir
 
Chapter 6 - Introduction to 8085 Instructions
Chapter 6 - Introduction to 8085 InstructionsChapter 6 - Introduction to 8085 Instructions
Chapter 6 - Introduction to 8085 Instructions
cmkandemir
 
İ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 Instructions
cmkandemir
 
PHP Temelleri
PHP TemelleriPHP Temelleri
PHP Temelleri
cmkandemir
 
openCV and Java - Face Detection
openCV and Java - Face DetectionopenCV and Java - Face Detection
openCV and Java - Face Detection
cmkandemir
 
impress.js Framework
impress.js Frameworkimpress.js Framework
impress.js Framework
cmkandemir
 
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
cmkandemir
 
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
cmkandemir
 
JDK and Eclipse Installation and Configuration
JDK and Eclipse Installation and ConfigurationJDK and Eclipse Installation and Configuration
JDK and Eclipse Installation and Configuration
cmkandemir
 
CSS - Sunum Bileşenleri
CSS - Sunum BileşenleriCSS - Sunum Bileşenleri
CSS - Sunum Bileşenleri
cmkandemir
 

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 Process
Nina Park
 
Jenkins CI
Jenkins CIJenkins CI
Jenkins CI
Viyaan Jhiingade
 
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
Shashank Tiwari
 
Containers Lab
Containers Lab Containers Lab
Containers Lab
Dev_Events
 
visualbasic.ppt
visualbasic.pptvisualbasic.ppt
visualbasic.ppt
NIDHINDASS1
 
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
benDesigning
 
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
Kuldeep Singh
 
DotNetNuke
DotNetNukeDotNetNuke
DotNetNuke
Ambati Sreedhar
 
Keynote + Next Gen UIs.pptx
Keynote + Next Gen UIs.pptxKeynote + Next Gen UIs.pptx
Keynote + Next Gen UIs.pptx
EqraKhattak
 
Chatbots
ChatbotsChatbots
Chatbots
Tessa Mero
 
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
ShubhamJayswal6
 
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 DevOps
Sarah Sexton
 
Vb.net session 15
Vb.net session 15Vb.net session 15
Vb.net session 15
Niit Care
 
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?
Vietnam Open Infrastructure User Group
 
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
Bobby DeVeaux, DevOps Consultant
 
Kate's.pptx
Kate's.pptxKate's.pptx
Kate's.pptx
Saikiran M
 
Revit drafting procedure
Revit drafting procedureRevit drafting procedure
Revit drafting procedure
Anima M C
 
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
BCC - Solutions for IBM Collaboration Software
 
Mini .net conf 2020
Mini .net conf 2020Mini .net conf 2020
Mini .net conf 2020
Marco Parenzan
 

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 Örnekleri
cmkandemir
 
Yapay Zeka Nedir?
Yapay Zeka Nedir?Yapay Zeka Nedir?
Yapay Zeka Nedir?
cmkandemir
 
Zekayı Anlamak
Zekayı AnlamakZekayı Anlamak
Zekayı Anlamak
cmkandemir
 
PHP - Kullanıcı Girişlerinin İşlenmesi
PHP - Kullanıcı Girişlerinin İşlenmesiPHP - Kullanıcı Girişlerinin İşlenmesi
PHP - Kullanıcı Girişlerinin İşlenmesi
cmkandemir
 
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
cmkandemir
 
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
cmkandemir
 
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, Form
cmkandemir
 
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
cmkandemir
 
Vaadin JPAContainer
Vaadin JPAContainerVaadin JPAContainer
Vaadin JPAContainer
cmkandemir
 

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

Accounting for Restricted Grants When and How To Record Properly
Accounting for Restricted Grants  When and How To Record ProperlyAccounting for Restricted Grants  When and How To Record Properly
Accounting for Restricted Grants When and How To Record Properly
TechSoup
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
Steve Thomason
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
MJDuyan
 
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
ImMuslim
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx
Kalna College
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
zuzanka
 
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
EduSkills OECD
 
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
Kalna College
 
MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025
khuleseema60
 
Data Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsxData Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsx
Prof. Dr. K. Adisesha
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Henry Hollis
 
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdfREASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
giancarloi8888
 
Juneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School DistrictJuneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School District
David Douglas School District
 
Electric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger HuntElectric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger Hunt
RamseyBerglund
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
TechSoup
 
220711130083 SUBHASHREE RAKSHIT Internet resources for social science
220711130083 SUBHASHREE RAKSHIT  Internet resources for social science220711130083 SUBHASHREE RAKSHIT  Internet resources for social science
220711130083 SUBHASHREE RAKSHIT Internet resources for social science
Kalna College
 
How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17
Celine George
 
skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)
Mohammad Al-Dhahabi
 

Recently uploaded (20)

Accounting for Restricted Grants When and How To Record Properly
Accounting for Restricted Grants  When and How To Record ProperlyAccounting for Restricted Grants  When and How To Record Properly
Accounting for Restricted Grants When and How To Record Properly
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
 
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
 
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
 
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
 
MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025
 
Data Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsxData Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsx
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
 
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdfREASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
 
Juneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School DistrictJuneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School District
 
Electric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger HuntElectric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger Hunt
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
 
220711130083 SUBHASHREE RAKSHIT Internet resources for social science
220711130083 SUBHASHREE RAKSHIT  Internet resources for social science220711130083 SUBHASHREE RAKSHIT  Internet resources for social science
220711130083 SUBHASHREE RAKSHIT Internet resources for social science
 
How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17
 
skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)
 

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