SlideShare a Scribd company logo
1 of 8
Download to read offline
I wanted to change the clouds/rectangles into an actuall image it doesnt matter the image.
import javax.swing.*;
import java.awt.*;
/**
* Created by Thomas on 11/27/2016.
*/
public class Renderer extends JPanel{
//private static final long serialVersionUID = 1L;
protected void paintComponent(Graphics g) {
Main.main.repaint(g);
}
public static int clamp(int greenValue, int i, int j) {
// TODO Auto-generated method stub
return 0;
}
}
OTHER PART:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
/**
* Created by Thomas on 11/27/2016.
*/
public class Main implements ActionListener, KeyListener{
public static Main main;
public final int WIDTH = 1400;
public final int HEIGHT = 600;
public HUD Hud;
public Renderer renderer;
public Rectangle character;
public ArrayList cloud;
public Random rand;
public boolean start = false, gameover = false;
public int tick;
public Main() {
JFrame jFrame = new JFrame();
Timer timer = new Timer(20, this);
renderer = new Renderer();
rand = new Random();
jFrame.setTitle("Example");
jFrame.add(renderer);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setSize(WIDTH, HEIGHT);
jFrame.addKeyListener(this);
jFrame.setVisible(true);
cloud = new ArrayList();
character = new Rectangle(200, 220, 20, 20);
addCloud(true);
addCloud(true);
addCloud(true);
addCloud(true);
addCloud(true);
addCloud(true);
addCloud(true);
addCloud(true);
timer.start();
}
public void repaint(Graphics g) {
g.setColor(Color.black);
g.fillRect(0,0, WIDTH, HEIGHT);
g.setColor(Color.blue);
g.fillRect(0, HEIGHT - 100, WIDTH, 100);
g.setColor(Color.green);
g.fillRect(character.x, character.y, character.width, character.height);
if (character.y >= HEIGHT - 100 || character.y < 0) {
gameover = true;
}
for (Rectangle rect : cloud) {
g.setColor(Color.white);
g.fillRect(rect.x, rect.y, rect.width, rect.height);
}
g.setColor(Color.WHITE);
g.setFont(new Font("Times New Roman", 1 ,100));
if (!start) {
g.drawString("Press to start!", 450, HEIGHT / 2);
}
else if (gameover) {
g.drawString("Game Over!", 450, HEIGHT / 2);
}
}
public void addCloud(boolean start) {
int width = 400;
int height = 200;
if (start) {
cloud.add(new Rectangle(WIDTH + width + cloud.size() * 300, rand.nextInt(HEIGHT-120),
80, 100));
}
else {
cloud.add(new Rectangle(cloud.get(cloud.size() - 1).x + 300, rand.nextInt(HEIGHT-120), 80,
100));
}
}
public void flap() {
if (gameover) {
character = new Rectangle(300, 400, 40, 40);
cloud.clear();
addCloud(true);
addCloud(true);
addCloud(true);
addCloud(true);
addCloud(true);
addCloud(true);
addCloud(true);
addCloud(true);
gameover = false;
}
if (!start) {
start = true;
}
else if (!gameover) {
character.y -= 70;
tick = 0;
}
}
@Override
public void actionPerformed(ActionEvent e) {
int speed = 15;
//System.out.println("Space");
if (start) {
for (int i = 0; i < cloud.size(); i++) {
Rectangle rect = cloud.get(i);
rect.x -= speed;
}
for (int i = 0; i < cloud.size(); i++) {
Rectangle rect = cloud.get(i);
if (rect.x + rect.width < 0) {
cloud.remove(rect);
addCloud(false);
}
}
for (Rectangle rect : cloud) {
if (rect.intersects(character)) {
gameover = true;
character.x -= speed;
}
}
tick ++;
if (tick %2 == 0 && character.y < HEIGHT-100)
character.y += tick;
if (gameover && character.y >= HEIGHT - 100) {
character.x -= speed;
}
}
renderer.repaint();
}
public static void main(String args[]) {
main = new Main();
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
flap();
}
}
}
Solution
import java.awt.*; import java.awt.event.*; import java.util.*; public class Animate01
extends Frame implements Runnable { private Image offScreenImage; private
Image backGroundImage; private Image[] gifImages = new Image[6];
//offscreen graphics context private Graphics offScreenGraphicsCtx; private
Thread animationThread; private MediaTracker mediaTracker; private SpriteManager
spriteManager; //Animation display rate, 12fps private int animationDelay = 83; private
Random rand = new Random(System. currentTimeMillis()); public
static void main( String[] args){ new Animate01(); }//end main //--------
-------------------------// Animate01() {//constructor // Load and track the images
mediaTracker = new MediaTracker(this); //Get and track the background //
image backGroundImage = Toolkit.getDefaultToolkit().
getImage("background02.gif"); mediaTracker.addImage( backGroundImage,
0); //Get and track 6 images to use // for sprites gifImages[0] =
Toolkit.getDefaultToolkit(). getImage("redball.gif"); mediaTracker.addImage(
gifImages[0], 0); gifImages[1] = Toolkit.getDefaultToolkit().
getImage("greenball.gif"); mediaTracker.addImage( gifImages[1], 0);
gifImages[2] = Toolkit.getDefaultToolkit(). getImage("blueball.gif");
mediaTracker.addImage( gifImages[2], 0); gifImages[3] =
Toolkit.getDefaultToolkit(). getImage("yellowball.gif"); mediaTracker.addImage(
gifImages[3], 0); gifImages[4] = Toolkit.getDefaultToolkit().
getImage("purpleball.gif"); mediaTracker.addImage( gifImages[4], 0);
gifImages[5] = Toolkit.getDefaultToolkit(). getImage("orangeball.gif");
mediaTracker.addImage( gifImages[5], 0); //Block and wait for all images
to // be loaded try { mediaTracker.waitForID(0); }catch (InterruptedException
e) { System.out.println(e); }//end catch //Base the Frame size on the size //
of the background image. //These getter methods return -1 if // the size is not yet known.
//Insets will be used later to // limit the graphics area to the // client area of the Frame.
int width = backGroundImage.getWidth(this); int height =
backGroundImage.getHeight(this); //While not likely, it may be // possible that the
size isn't // known yet. Do the following // just in case. //Wait until size is known
while(width == -1 || height == -1){ System.out.println( "Waiting for image");
width = backGroundImage. getWidth(this); height = backGroundImage.
getHeight(this); }//end while loop //Display the frame
setSize(width,height); setVisible(true); setTitle( "Copyright 2001, R.G.Baldwin");
//Create and start animation thread animationThread = new Thread(this);
animationThread.start(); //Anonymous inner class window // listener to terminate the
// program. this.addWindowListener( new WindowAdapter(){ public
void windowClosing( WindowEvent e){ System.exit(0);}}); }//end
constructor //---------------------------------// public void run() { //Create and add
sprites to the // sprite manager spriteManager = new SpriteManager( new
BackgroundImage( this, backGroundImage)); //Create 15 sprites from 6 gif //
files. for (int cnt = 0; cnt < 15; cnt++){ Point position = spriteManager.
getEmptyPosition(new Dimension( gifImages[0].getWidth(this), gifImages[0].
getHeight(this))); spriteManager.addSprite( makeSprite(position, cnt %
6)); }//end for loop //Loop, sleep, and update sprite // positions once each 83
// milliseconds long time = System.currentTimeMillis(); while (true) {//infinite
loop spriteManager.update(); repaint(); try { time += animationDelay;
Thread.sleep(Math.max(0,time - System.currentTimeMillis())); }catch
(InterruptedException e) { System.out.println(e); }//end catch }//end while loop
}//end run method //---------------------------------// private Sprite makeSprite( Point
position, int imageIndex) { return new Sprite( this, gifImages[imageIndex],
position, new Point(rand.nextInt() % 5, rand.nextInt() % 5)); }//end
makeSprite() //---------------------------------// //Overridden graphics update method //
on the Frame public void update(Graphics g) { //Create the offscreen graphics //
context if (offScreenGraphicsCtx == null) { offScreenImage =
createImage(getSize().width, getSize().height); offScreenGraphicsCtx =
offScreenImage.getGraphics(); }//end if // Draw the sprites offscreen
spriteManager.drawScene( offScreenGraphicsCtx); // Draw the scene onto the
screen if(offScreenImage != null){ g.drawImage( offScreenImage, 0, 0, this);
}//end if }//end overridden update method //---------------------------------//
//Overridden paint method on the // Frame public void paint(Graphics g) { //Nothing
required here. All // drawing is done in the update // method above. }//end overridden
paint method }//end class Animate01 //===================================//
class BackgroundImage{ private Image image; private Component component;
private Dimension size; public BackgroundImage( Component component,
Image image) { this.component = component; size = component.getSize();
this.image = image; }//end construtor public Dimension getSize(){ return size;
}//end getSize() public Image getImage(){ return image; }//end getImage()
public void setImage(Image image){ this.image = image; }//end setImage() public
void drawBackgroundImage( Graphics g) { g.drawImage( image,
0, 0, component); }//end drawBackgroundImage() }//end class BackgroundImage
//=========================== class SpriteManager extends Vector { private
BackgroundImage backgroundImage; public SpriteManager(
BackgroundImage backgroundImage) { this.backgroundImage =
backgroundImage; }//end constructor //---------------------------------// public Point
getEmptyPosition( Dimension spriteSize){ Rectangle trialSpaceOccupied =
new Rectangle(0, 0, spriteSize.width, spriteSize.height);
Random rand = new Random( System.currentTimeMillis()); boolean empty =
false; int numTries = 0; // Search for an empty position while (!empty &&
numTries++ < 100){ // Get a trial position trialSpaceOccupied.x =
Math.abs(rand.nextInt() % backgroundImage. getSize().width);
trialSpaceOccupied.y = Math.abs(rand.nextInt() % backgroundImage.
getSize().height); // Iterate through existing // sprites, checking if position
// is empty boolean collision = false; for(int cnt = 0;cnt < size();
cnt++){ Rectangle testSpaceOccupied = ((Sprite)elementAt(cnt)).
getSpaceOccupied(); if (trialSpaceOccupied. intersects(
testSpaceOccupied)){ collision = true; }//end if }//end for loop empty =
!collision; }//end while loop return new Point( trialSpaceOccupied.x,
trialSpaceOccupied.y); }//end getEmptyPosition() //---------------------------------//
public void update() { Sprite sprite; //Iterate through sprite list for (int cnt = 0;cnt
< size(); cnt++){ sprite = (Sprite)elementAt(cnt); //Update a
sprite's position sprite.updatePosition(); //Test for collision. Positive // result
indicates a collision int hitIndex = testForCollision(sprite); if (hitIndex >=
0){ //a collision has occurred bounceOffSprite(cnt,hitIndex); }//end if
}//end for loop }//end update

More Related Content

Similar to I wanted to change the cloudsrectangles into an actuall image it do.pdf

Implementing a Simple Game using libGDX
Implementing a Simple Game using libGDXImplementing a Simple Game using libGDX
Implementing a Simple Game using libGDXJussi Pohjolainen
 
Task Write a Java program to implement a simple graphics editor tha.pdf
Task Write a Java program to implement a simple graphics editor tha.pdfTask Write a Java program to implement a simple graphics editor tha.pdf
Task Write a Java program to implement a simple graphics editor tha.pdfcronkwurphyb44502
 
HTML5 - Daha Flash bir web?
HTML5 - Daha Flash bir web?HTML5 - Daha Flash bir web?
HTML5 - Daha Flash bir web?Ankara JUG
 
HTML5って必要?
HTML5って必要?HTML5って必要?
HTML5って必要?GCS2013
 
Matching Game In Java
Matching Game In JavaMatching Game In Java
Matching Game In Javacmkandemir
 
The 2016 Android Developer Toolbox [MOBILIZATION]
The 2016 Android Developer Toolbox [MOBILIZATION]The 2016 Android Developer Toolbox [MOBILIZATION]
The 2016 Android Developer Toolbox [MOBILIZATION]Nilhcem
 
Webgl para JavaScripters
Webgl para JavaScriptersWebgl para JavaScripters
Webgl para JavaScriptersgerbille
 
Following are the changes mentioned in bold in order to obtain the r.pdf
Following are the changes mentioned in bold in order to obtain the r.pdfFollowing are the changes mentioned in bold in order to obtain the r.pdf
Following are the changes mentioned in bold in order to obtain the r.pdfanithareadymade
 
The 2016 Android Developer Toolbox [NANTES]
The 2016 Android Developer Toolbox [NANTES]The 2016 Android Developer Toolbox [NANTES]
The 2016 Android Developer Toolbox [NANTES]Nilhcem
 
Android Best Practices
Android Best PracticesAndroid Best Practices
Android Best PracticesYekmer Simsek
 
Reloj en java
Reloj en javaReloj en java
Reloj en javacathe26
 
Creating Games for Asha - platform
Creating Games for Asha - platformCreating Games for Asha - platform
Creating Games for Asha - platformJussi Pohjolainen
 
Mobile Game and Application with J2ME - Collision Detection
Mobile Gameand Application withJ2ME  - Collision DetectionMobile Gameand Application withJ2ME  - Collision Detection
Mobile Game and Application with J2ME - Collision DetectionJenchoke Tachagomain
 
need help with code I wrote. This code is a maze gui, and i need hel.pdf
need help with code I wrote. This code is a maze gui, and i need hel.pdfneed help with code I wrote. This code is a maze gui, and i need hel.pdf
need help with code I wrote. This code is a maze gui, and i need hel.pdfarcotstarsports
 

Similar to I wanted to change the cloudsrectangles into an actuall image it do.pdf (20)

Implementing a Simple Game using libGDX
Implementing a Simple Game using libGDXImplementing a Simple Game using libGDX
Implementing a Simple Game using libGDX
 
Task Write a Java program to implement a simple graphics editor tha.pdf
Task Write a Java program to implement a simple graphics editor tha.pdfTask Write a Java program to implement a simple graphics editor tha.pdf
Task Write a Java program to implement a simple graphics editor tha.pdf
 
HTML5 - Daha Flash bir web?
HTML5 - Daha Flash bir web?HTML5 - Daha Flash bir web?
HTML5 - Daha Flash bir web?
 
HTML5って必要?
HTML5って必要?HTML5って必要?
HTML5って必要?
 
A More Flash Like Web?
A More Flash Like Web?A More Flash Like Web?
A More Flash Like Web?
 
Matching Game In Java
Matching Game In JavaMatching Game In Java
Matching Game In Java
 
The 2016 Android Developer Toolbox [MOBILIZATION]
The 2016 Android Developer Toolbox [MOBILIZATION]The 2016 Android Developer Toolbox [MOBILIZATION]
The 2016 Android Developer Toolbox [MOBILIZATION]
 
Webgl para JavaScripters
Webgl para JavaScriptersWebgl para JavaScripters
Webgl para JavaScripters
 
662305 LAB13
662305 LAB13662305 LAB13
662305 LAB13
 
Following are the changes mentioned in bold in order to obtain the r.pdf
Following are the changes mentioned in bold in order to obtain the r.pdfFollowing are the changes mentioned in bold in order to obtain the r.pdf
Following are the changes mentioned in bold in order to obtain the r.pdf
 
Open Cv Tutorial Ii
Open Cv Tutorial IiOpen Cv Tutorial Ii
Open Cv Tutorial Ii
 
Open Cv Tutorial Ii
Open Cv Tutorial IiOpen Cv Tutorial Ii
Open Cv Tutorial Ii
 
The 2016 Android Developer Toolbox [NANTES]
The 2016 Android Developer Toolbox [NANTES]The 2016 Android Developer Toolbox [NANTES]
The 2016 Android Developer Toolbox [NANTES]
 
Android Best Practices
Android Best PracticesAndroid Best Practices
Android Best Practices
 
Java awt
Java awtJava awt
Java awt
 
Sbaw091117
Sbaw091117Sbaw091117
Sbaw091117
 
Reloj en java
Reloj en javaReloj en java
Reloj en java
 
Creating Games for Asha - platform
Creating Games for Asha - platformCreating Games for Asha - platform
Creating Games for Asha - platform
 
Mobile Game and Application with J2ME - Collision Detection
Mobile Gameand Application withJ2ME  - Collision DetectionMobile Gameand Application withJ2ME  - Collision Detection
Mobile Game and Application with J2ME - Collision Detection
 
need help with code I wrote. This code is a maze gui, and i need hel.pdf
need help with code I wrote. This code is a maze gui, and i need hel.pdfneed help with code I wrote. This code is a maze gui, and i need hel.pdf
need help with code I wrote. This code is a maze gui, and i need hel.pdf
 

More from feelinggifts

Chapter 22. Problem 3BCP0 Bookmark Show all steps ON Problem Eminent.pdf
Chapter 22. Problem 3BCP0 Bookmark Show all steps ON Problem Eminent.pdfChapter 22. Problem 3BCP0 Bookmark Show all steps ON Problem Eminent.pdf
Chapter 22. Problem 3BCP0 Bookmark Show all steps ON Problem Eminent.pdffeelinggifts
 
D Question 11 1 pts A fundamental feature of money that BitCoin fulfi.pdf
D Question 11 1 pts A fundamental feature of money that BitCoin fulfi.pdfD Question 11 1 pts A fundamental feature of money that BitCoin fulfi.pdf
D Question 11 1 pts A fundamental feature of money that BitCoin fulfi.pdffeelinggifts
 
connect instructions I helg dule 7 Connect seve & Exit ..pdf
connect instructions I helg dule 7 Connect seve & Exit ..pdfconnect instructions I helg dule 7 Connect seve & Exit ..pdf
connect instructions I helg dule 7 Connect seve & Exit ..pdffeelinggifts
 
COMPUTER ETHICSDiscuss the local and global impact of malware and .pdf
COMPUTER ETHICSDiscuss the local and global impact of malware and .pdfCOMPUTER ETHICSDiscuss the local and global impact of malware and .pdf
COMPUTER ETHICSDiscuss the local and global impact of malware and .pdffeelinggifts
 
You’re helping a group of ethnographers analyze some oral history da.pdf
You’re helping a group of ethnographers analyze some oral history da.pdfYou’re helping a group of ethnographers analyze some oral history da.pdf
You’re helping a group of ethnographers analyze some oral history da.pdffeelinggifts
 
A man with type B blood type claims to be biological father of a typ.pdf
A man with type B blood type claims to be biological father of a typ.pdfA man with type B blood type claims to be biological father of a typ.pdf
A man with type B blood type claims to be biological father of a typ.pdffeelinggifts
 
why should filtrate be used to transfer the residue when the desired.pdf
why should filtrate be used to transfer the residue when the desired.pdfwhy should filtrate be used to transfer the residue when the desired.pdf
why should filtrate be used to transfer the residue when the desired.pdffeelinggifts
 
Winch of the following is NOT one of the reasons an IO Exception is t.pdf
Winch of the following is NOT one of the reasons an IO Exception is t.pdfWinch of the following is NOT one of the reasons an IO Exception is t.pdf
Winch of the following is NOT one of the reasons an IO Exception is t.pdffeelinggifts
 
Why is attending to the full scope of drivers critical in implementi.pdf
Why is attending to the full scope of drivers critical in implementi.pdfWhy is attending to the full scope of drivers critical in implementi.pdf
Why is attending to the full scope of drivers critical in implementi.pdffeelinggifts
 
Which of the following is not a function of interest groups A. to i.pdf
Which of the following is not a function of interest groups A. to i.pdfWhich of the following is not a function of interest groups A. to i.pdf
Which of the following is not a function of interest groups A. to i.pdffeelinggifts
 
When explaining civil law one would say that it is concemed with disp.pdf
When explaining civil law one would say that it is concemed with disp.pdfWhen explaining civil law one would say that it is concemed with disp.pdf
When explaining civil law one would say that it is concemed with disp.pdffeelinggifts
 
What is the difference between a short and long branchSolution.pdf
What is the difference between a short and long branchSolution.pdfWhat is the difference between a short and long branchSolution.pdf
What is the difference between a short and long branchSolution.pdffeelinggifts
 
What is the maximum file size in a FAT32 systemSolutionFirst .pdf
What is the maximum file size in a FAT32 systemSolutionFirst .pdfWhat is the maximum file size in a FAT32 systemSolutionFirst .pdf
What is the maximum file size in a FAT32 systemSolutionFirst .pdffeelinggifts
 
View transaction list Journal entry worksheet The company paid $510 c.pdf
View transaction list Journal entry worksheet The company paid $510 c.pdfView transaction list Journal entry worksheet The company paid $510 c.pdf
View transaction list Journal entry worksheet The company paid $510 c.pdffeelinggifts
 
The numeric data types in C++ can be broken into two general categori.pdf
The numeric data types in C++ can be broken into two general categori.pdfThe numeric data types in C++ can be broken into two general categori.pdf
The numeric data types in C++ can be broken into two general categori.pdffeelinggifts
 
The capillaries at the alveoli Form a respiratory membrane Are spe.pdf
The capillaries at the alveoli  Form a respiratory membrane  Are spe.pdfThe capillaries at the alveoli  Form a respiratory membrane  Are spe.pdf
The capillaries at the alveoli Form a respiratory membrane Are spe.pdffeelinggifts
 
Select the word or phrase which best matches the description of it..pdf
Select the word or phrase which best matches the description of it..pdfSelect the word or phrase which best matches the description of it..pdf
Select the word or phrase which best matches the description of it..pdffeelinggifts
 
Robin Hood has hired you as his new Strategic Consultant to help him.pdf
Robin Hood has hired you as his new Strategic Consultant to help him.pdfRobin Hood has hired you as his new Strategic Consultant to help him.pdf
Robin Hood has hired you as his new Strategic Consultant to help him.pdffeelinggifts
 
Research indicates that online news seekers tend to access topics of.pdf
Research indicates that online news seekers tend to access topics of.pdfResearch indicates that online news seekers tend to access topics of.pdf
Research indicates that online news seekers tend to access topics of.pdffeelinggifts
 
Read Case Study Room 406 and answer the following questions in 2.pdf
Read Case Study Room 406 and answer the following questions in 2.pdfRead Case Study Room 406 and answer the following questions in 2.pdf
Read Case Study Room 406 and answer the following questions in 2.pdffeelinggifts
 

More from feelinggifts (20)

Chapter 22. Problem 3BCP0 Bookmark Show all steps ON Problem Eminent.pdf
Chapter 22. Problem 3BCP0 Bookmark Show all steps ON Problem Eminent.pdfChapter 22. Problem 3BCP0 Bookmark Show all steps ON Problem Eminent.pdf
Chapter 22. Problem 3BCP0 Bookmark Show all steps ON Problem Eminent.pdf
 
D Question 11 1 pts A fundamental feature of money that BitCoin fulfi.pdf
D Question 11 1 pts A fundamental feature of money that BitCoin fulfi.pdfD Question 11 1 pts A fundamental feature of money that BitCoin fulfi.pdf
D Question 11 1 pts A fundamental feature of money that BitCoin fulfi.pdf
 
connect instructions I helg dule 7 Connect seve & Exit ..pdf
connect instructions I helg dule 7 Connect seve & Exit ..pdfconnect instructions I helg dule 7 Connect seve & Exit ..pdf
connect instructions I helg dule 7 Connect seve & Exit ..pdf
 
COMPUTER ETHICSDiscuss the local and global impact of malware and .pdf
COMPUTER ETHICSDiscuss the local and global impact of malware and .pdfCOMPUTER ETHICSDiscuss the local and global impact of malware and .pdf
COMPUTER ETHICSDiscuss the local and global impact of malware and .pdf
 
You’re helping a group of ethnographers analyze some oral history da.pdf
You’re helping a group of ethnographers analyze some oral history da.pdfYou’re helping a group of ethnographers analyze some oral history da.pdf
You’re helping a group of ethnographers analyze some oral history da.pdf
 
A man with type B blood type claims to be biological father of a typ.pdf
A man with type B blood type claims to be biological father of a typ.pdfA man with type B blood type claims to be biological father of a typ.pdf
A man with type B blood type claims to be biological father of a typ.pdf
 
why should filtrate be used to transfer the residue when the desired.pdf
why should filtrate be used to transfer the residue when the desired.pdfwhy should filtrate be used to transfer the residue when the desired.pdf
why should filtrate be used to transfer the residue when the desired.pdf
 
Winch of the following is NOT one of the reasons an IO Exception is t.pdf
Winch of the following is NOT one of the reasons an IO Exception is t.pdfWinch of the following is NOT one of the reasons an IO Exception is t.pdf
Winch of the following is NOT one of the reasons an IO Exception is t.pdf
 
Why is attending to the full scope of drivers critical in implementi.pdf
Why is attending to the full scope of drivers critical in implementi.pdfWhy is attending to the full scope of drivers critical in implementi.pdf
Why is attending to the full scope of drivers critical in implementi.pdf
 
Which of the following is not a function of interest groups A. to i.pdf
Which of the following is not a function of interest groups A. to i.pdfWhich of the following is not a function of interest groups A. to i.pdf
Which of the following is not a function of interest groups A. to i.pdf
 
When explaining civil law one would say that it is concemed with disp.pdf
When explaining civil law one would say that it is concemed with disp.pdfWhen explaining civil law one would say that it is concemed with disp.pdf
When explaining civil law one would say that it is concemed with disp.pdf
 
What is the difference between a short and long branchSolution.pdf
What is the difference between a short and long branchSolution.pdfWhat is the difference between a short and long branchSolution.pdf
What is the difference between a short and long branchSolution.pdf
 
What is the maximum file size in a FAT32 systemSolutionFirst .pdf
What is the maximum file size in a FAT32 systemSolutionFirst .pdfWhat is the maximum file size in a FAT32 systemSolutionFirst .pdf
What is the maximum file size in a FAT32 systemSolutionFirst .pdf
 
View transaction list Journal entry worksheet The company paid $510 c.pdf
View transaction list Journal entry worksheet The company paid $510 c.pdfView transaction list Journal entry worksheet The company paid $510 c.pdf
View transaction list Journal entry worksheet The company paid $510 c.pdf
 
The numeric data types in C++ can be broken into two general categori.pdf
The numeric data types in C++ can be broken into two general categori.pdfThe numeric data types in C++ can be broken into two general categori.pdf
The numeric data types in C++ can be broken into two general categori.pdf
 
The capillaries at the alveoli Form a respiratory membrane Are spe.pdf
The capillaries at the alveoli  Form a respiratory membrane  Are spe.pdfThe capillaries at the alveoli  Form a respiratory membrane  Are spe.pdf
The capillaries at the alveoli Form a respiratory membrane Are spe.pdf
 
Select the word or phrase which best matches the description of it..pdf
Select the word or phrase which best matches the description of it..pdfSelect the word or phrase which best matches the description of it..pdf
Select the word or phrase which best matches the description of it..pdf
 
Robin Hood has hired you as his new Strategic Consultant to help him.pdf
Robin Hood has hired you as his new Strategic Consultant to help him.pdfRobin Hood has hired you as his new Strategic Consultant to help him.pdf
Robin Hood has hired you as his new Strategic Consultant to help him.pdf
 
Research indicates that online news seekers tend to access topics of.pdf
Research indicates that online news seekers tend to access topics of.pdfResearch indicates that online news seekers tend to access topics of.pdf
Research indicates that online news seekers tend to access topics of.pdf
 
Read Case Study Room 406 and answer the following questions in 2.pdf
Read Case Study Room 406 and answer the following questions in 2.pdfRead Case Study Room 406 and answer the following questions in 2.pdf
Read Case Study Room 406 and answer the following questions in 2.pdf
 

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
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxabhijeetpadhi001
 
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
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
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
 
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
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
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
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 

Recently uploaded (20)

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
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
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)
 
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
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptx
 
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
 
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🔝
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
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
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
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
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 

I wanted to change the cloudsrectangles into an actuall image it do.pdf

  • 1. I wanted to change the clouds/rectangles into an actuall image it doesnt matter the image. import javax.swing.*; import java.awt.*; /** * Created by Thomas on 11/27/2016. */ public class Renderer extends JPanel{ //private static final long serialVersionUID = 1L; protected void paintComponent(Graphics g) { Main.main.repaint(g); } public static int clamp(int greenValue, int i, int j) { // TODO Auto-generated method stub return 0; } } OTHER PART: import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.ArrayList; import java.util.Random; import javax.swing.*; /** * Created by Thomas on 11/27/2016. */ public class Main implements ActionListener, KeyListener{ public static Main main; public final int WIDTH = 1400; public final int HEIGHT = 600; public HUD Hud; public Renderer renderer;
  • 2. public Rectangle character; public ArrayList cloud; public Random rand; public boolean start = false, gameover = false; public int tick; public Main() { JFrame jFrame = new JFrame(); Timer timer = new Timer(20, this); renderer = new Renderer(); rand = new Random(); jFrame.setTitle("Example"); jFrame.add(renderer); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.setSize(WIDTH, HEIGHT); jFrame.addKeyListener(this); jFrame.setVisible(true); cloud = new ArrayList(); character = new Rectangle(200, 220, 20, 20); addCloud(true); addCloud(true); addCloud(true); addCloud(true); addCloud(true); addCloud(true); addCloud(true); addCloud(true); timer.start(); } public void repaint(Graphics g) { g.setColor(Color.black); g.fillRect(0,0, WIDTH, HEIGHT); g.setColor(Color.blue); g.fillRect(0, HEIGHT - 100, WIDTH, 100); g.setColor(Color.green); g.fillRect(character.x, character.y, character.width, character.height);
  • 3. if (character.y >= HEIGHT - 100 || character.y < 0) { gameover = true; } for (Rectangle rect : cloud) { g.setColor(Color.white); g.fillRect(rect.x, rect.y, rect.width, rect.height); } g.setColor(Color.WHITE); g.setFont(new Font("Times New Roman", 1 ,100)); if (!start) { g.drawString("Press to start!", 450, HEIGHT / 2); } else if (gameover) { g.drawString("Game Over!", 450, HEIGHT / 2); } } public void addCloud(boolean start) { int width = 400; int height = 200; if (start) { cloud.add(new Rectangle(WIDTH + width + cloud.size() * 300, rand.nextInt(HEIGHT-120), 80, 100)); } else { cloud.add(new Rectangle(cloud.get(cloud.size() - 1).x + 300, rand.nextInt(HEIGHT-120), 80, 100)); } } public void flap() { if (gameover) { character = new Rectangle(300, 400, 40, 40); cloud.clear(); addCloud(true); addCloud(true); addCloud(true); addCloud(true);
  • 4. addCloud(true); addCloud(true); addCloud(true); addCloud(true); gameover = false; } if (!start) { start = true; } else if (!gameover) { character.y -= 70; tick = 0; } } @Override public void actionPerformed(ActionEvent e) { int speed = 15; //System.out.println("Space"); if (start) { for (int i = 0; i < cloud.size(); i++) { Rectangle rect = cloud.get(i); rect.x -= speed; } for (int i = 0; i < cloud.size(); i++) { Rectangle rect = cloud.get(i); if (rect.x + rect.width < 0) { cloud.remove(rect); addCloud(false); } } for (Rectangle rect : cloud) { if (rect.intersects(character)) { gameover = true; character.x -= speed; } }
  • 5. tick ++; if (tick %2 == 0 && character.y < HEIGHT-100) character.y += tick; if (gameover && character.y >= HEIGHT - 100) { character.x -= speed; } } renderer.repaint(); } public static void main(String args[]) { main = new Main(); } @Override public void keyTyped(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_SPACE) { flap(); } } } Solution import java.awt.*; import java.awt.event.*; import java.util.*; public class Animate01 extends Frame implements Runnable { private Image offScreenImage; private Image backGroundImage; private Image[] gifImages = new Image[6]; //offscreen graphics context private Graphics offScreenGraphicsCtx; private Thread animationThread; private MediaTracker mediaTracker; private SpriteManager spriteManager; //Animation display rate, 12fps private int animationDelay = 83; private Random rand = new Random(System. currentTimeMillis()); public static void main( String[] args){ new Animate01(); }//end main //--------
  • 6. -------------------------// Animate01() {//constructor // Load and track the images mediaTracker = new MediaTracker(this); //Get and track the background // image backGroundImage = Toolkit.getDefaultToolkit(). getImage("background02.gif"); mediaTracker.addImage( backGroundImage, 0); //Get and track 6 images to use // for sprites gifImages[0] = Toolkit.getDefaultToolkit(). getImage("redball.gif"); mediaTracker.addImage( gifImages[0], 0); gifImages[1] = Toolkit.getDefaultToolkit(). getImage("greenball.gif"); mediaTracker.addImage( gifImages[1], 0); gifImages[2] = Toolkit.getDefaultToolkit(). getImage("blueball.gif"); mediaTracker.addImage( gifImages[2], 0); gifImages[3] = Toolkit.getDefaultToolkit(). getImage("yellowball.gif"); mediaTracker.addImage( gifImages[3], 0); gifImages[4] = Toolkit.getDefaultToolkit(). getImage("purpleball.gif"); mediaTracker.addImage( gifImages[4], 0); gifImages[5] = Toolkit.getDefaultToolkit(). getImage("orangeball.gif"); mediaTracker.addImage( gifImages[5], 0); //Block and wait for all images to // be loaded try { mediaTracker.waitForID(0); }catch (InterruptedException e) { System.out.println(e); }//end catch //Base the Frame size on the size // of the background image. //These getter methods return -1 if // the size is not yet known. //Insets will be used later to // limit the graphics area to the // client area of the Frame. int width = backGroundImage.getWidth(this); int height = backGroundImage.getHeight(this); //While not likely, it may be // possible that the size isn't // known yet. Do the following // just in case. //Wait until size is known while(width == -1 || height == -1){ System.out.println( "Waiting for image"); width = backGroundImage. getWidth(this); height = backGroundImage. getHeight(this); }//end while loop //Display the frame setSize(width,height); setVisible(true); setTitle( "Copyright 2001, R.G.Baldwin"); //Create and start animation thread animationThread = new Thread(this); animationThread.start(); //Anonymous inner class window // listener to terminate the // program. this.addWindowListener( new WindowAdapter(){ public void windowClosing( WindowEvent e){ System.exit(0);}}); }//end constructor //---------------------------------// public void run() { //Create and add sprites to the // sprite manager spriteManager = new SpriteManager( new BackgroundImage( this, backGroundImage)); //Create 15 sprites from 6 gif // files. for (int cnt = 0; cnt < 15; cnt++){ Point position = spriteManager. getEmptyPosition(new Dimension( gifImages[0].getWidth(this), gifImages[0]. getHeight(this))); spriteManager.addSprite( makeSprite(position, cnt %
  • 7. 6)); }//end for loop //Loop, sleep, and update sprite // positions once each 83 // milliseconds long time = System.currentTimeMillis(); while (true) {//infinite loop spriteManager.update(); repaint(); try { time += animationDelay; Thread.sleep(Math.max(0,time - System.currentTimeMillis())); }catch (InterruptedException e) { System.out.println(e); }//end catch }//end while loop }//end run method //---------------------------------// private Sprite makeSprite( Point position, int imageIndex) { return new Sprite( this, gifImages[imageIndex], position, new Point(rand.nextInt() % 5, rand.nextInt() % 5)); }//end makeSprite() //---------------------------------// //Overridden graphics update method // on the Frame public void update(Graphics g) { //Create the offscreen graphics // context if (offScreenGraphicsCtx == null) { offScreenImage = createImage(getSize().width, getSize().height); offScreenGraphicsCtx = offScreenImage.getGraphics(); }//end if // Draw the sprites offscreen spriteManager.drawScene( offScreenGraphicsCtx); // Draw the scene onto the screen if(offScreenImage != null){ g.drawImage( offScreenImage, 0, 0, this); }//end if }//end overridden update method //---------------------------------// //Overridden paint method on the // Frame public void paint(Graphics g) { //Nothing required here. All // drawing is done in the update // method above. }//end overridden paint method }//end class Animate01 //===================================// class BackgroundImage{ private Image image; private Component component; private Dimension size; public BackgroundImage( Component component, Image image) { this.component = component; size = component.getSize(); this.image = image; }//end construtor public Dimension getSize(){ return size; }//end getSize() public Image getImage(){ return image; }//end getImage() public void setImage(Image image){ this.image = image; }//end setImage() public void drawBackgroundImage( Graphics g) { g.drawImage( image, 0, 0, component); }//end drawBackgroundImage() }//end class BackgroundImage //=========================== class SpriteManager extends Vector { private BackgroundImage backgroundImage; public SpriteManager( BackgroundImage backgroundImage) { this.backgroundImage = backgroundImage; }//end constructor //---------------------------------// public Point getEmptyPosition( Dimension spriteSize){ Rectangle trialSpaceOccupied = new Rectangle(0, 0, spriteSize.width, spriteSize.height); Random rand = new Random( System.currentTimeMillis()); boolean empty = false; int numTries = 0; // Search for an empty position while (!empty && numTries++ < 100){ // Get a trial position trialSpaceOccupied.x =
  • 8. Math.abs(rand.nextInt() % backgroundImage. getSize().width); trialSpaceOccupied.y = Math.abs(rand.nextInt() % backgroundImage. getSize().height); // Iterate through existing // sprites, checking if position // is empty boolean collision = false; for(int cnt = 0;cnt < size(); cnt++){ Rectangle testSpaceOccupied = ((Sprite)elementAt(cnt)). getSpaceOccupied(); if (trialSpaceOccupied. intersects( testSpaceOccupied)){ collision = true; }//end if }//end for loop empty = !collision; }//end while loop return new Point( trialSpaceOccupied.x, trialSpaceOccupied.y); }//end getEmptyPosition() //---------------------------------// public void update() { Sprite sprite; //Iterate through sprite list for (int cnt = 0;cnt < size(); cnt++){ sprite = (Sprite)elementAt(cnt); //Update a sprite's position sprite.updatePosition(); //Test for collision. Positive // result indicates a collision int hitIndex = testForCollision(sprite); if (hitIndex >= 0){ //a collision has occurred bounceOffSprite(cnt,hitIndex); }//end if }//end for loop }//end update