Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need to add treemap to the following program, it has 2 classes Main.java and Gameplay.java. I need help assimilating treemap into the following program.

I need to add treemap to the following program, it has 2 classes Main.java and Gameplay.java. I need help assimilating treemap into the following program. Thanks! :

Main.java:

import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.Random;

import javax.swing.Timer;

import javax.swing.ImageIcon; import javax.swing.JPanel;

// 59: 07

public class Gameplay extends JPanel implements KeyListener, ActionListener {

private int [] snakexlength = new int[750]; private int [] snakeylength = new int[750];

private boolean left = false; private boolean right = false; private boolean up = false; private boolean down = false;

private ImageIcon rightmouth; private ImageIcon upmouth; private ImageIcon downmouth; private ImageIcon leftmouth;

private int lengthofsnake = 3;

private Timer timer; private int delay = 100; // speed of snake

private ImageIcon snakeimage;

private int [] enemyxpos ={25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425, 450, 475, 500, 575, 600, 625, 650, 675, 700, 725, 800, 825, 850}; private int [] enemyypos = {75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425, 475, 500, 525, 550, 575, 600, 625}; private ImageIcon enemyimage; private Random random = new Random(); private int moves = 0; private int xpos = random.nextInt(34); private int ypos = random.nextInt(23); private int score = 0;

private ImageIcon titleImage;

public Gameplay(){

addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); timer = new Timer(delay, this); timer.start();

} // snake icons are 25 x 25

public void paint(Graphics g){

if(moves == 0){ snakexlength[2] = 50; snakexlength[1] = 75; snakexlength[0] = 100;

snakeylength[2] = 100; snakeylength[1] = 100; snakeylength[0] = 100;

}

// draw title image border g.setColor(Color.white); g.drawRect(24, 10, 851, 55);

// this draws the title image

titleImage = new ImageIcon("snaketitle.jpg"); titleImage.paintIcon(this, g, 25, 11);

// this draws the border for the actual gameplay

g.setColor(Color.WHITE); g.drawRect(24, 74, 851, 577);

//this draws the background for the gameplay

g.setColor(Color.black); g.fillRect(25, 75, 850, 575); //draw scores g.setColor(Color.white); g.setFont(new Font("arial" , Font.PLAIN, 14)); g.drawString("Scores : "+score, 780,30); // draw the length of the snake g.setColor(Color.white); g.setFont(new Font("arial" , Font.PLAIN, 14)); g.drawString("Length : "+lengthofsnake, 780,50);

rightmouth = new ImageIcon("rightmouth.png"); rightmouth.paintIcon(this, g, snakexlength[0], snakeylength[0]);

for(int i = 0; i < lengthofsnake; i++){

if(i == 0 && right){ rightmouth = new ImageIcon("rightmouth.png"); rightmouth.paintIcon(this, g, snakexlength[i], snakeylength[i]); }

if(i == 0 && left){ leftmouth = new ImageIcon("leftmouth.png"); leftmouth.paintIcon(this, g, snakexlength[i], snakeylength[i]); }

if(i == 0 && down){ downmouth = new ImageIcon("downmouth.png"); downmouth.paintIcon(this, g, snakexlength[i], snakeylength[i]); }

if(i == 0 && up){ upmouth = new ImageIcon("upmouth.png"); upmouth.paintIcon(this, g, snakexlength[i], snakeylength[i]); }

if( i != 0){ // body of snake

snakeimage= new ImageIcon("snakeimage.png"); snakeimage.paintIcon(this, g, snakexlength[i], snakeylength[i]);

} } enemyimage = new ImageIcon("enemy.png"); if(enemyxpos[xpos] == snakexlength[0] && enemyypos[ypos] == snakeylength[0]){ score++; lengthofsnake++; xpos = random.nextInt(34); ypos = random.nextInt(23); }

enemyimage.paintIcon(this, g, enemyxpos[xpos], enemyypos[ypos]); for(int b = 1; b < lengthofsnake; b++){ if(snakexlength[b] == snakexlength[0] && snakeylength[b] == snakeylength[0]){ right = false; left = false; up = false; down = false; g.setColor(Color.white); g.setFont(new Font("arial", Font.BOLD, 50)); g.drawString("Game Over", 300, 300); g.setFont(new Font("arial", Font.BOLD, 20)); g.drawString("Space to RESTART", 350, 340); } } g.dispose();

}

@Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub

timer.start(); if(right){

for(int i = lengthofsnake-1; i >= 0; i--){

snakeylength[i+1] = snakeylength[i]; }

for(int i = lengthofsnake; i >= 0;i--){

if( i == 0){ snakexlength[i] = snakexlength[i] + 25; }else{ snakexlength[i] = snakexlength[i-1]; }

if(snakexlength[i] > 850){

snakexlength[i] = 25; }

}

repaint();

} if(left){

for(int i = lengthofsnake-1; i >= 0; i--){

snakeylength[i+1] = snakeylength[i]; }

for(int i = lengthofsnake; i >= 0;i--){

if( i == 0){ snakexlength[i] = snakexlength[i] - 25; }else{

snakexlength[i] = snakexlength[i-1]; }

if(snakexlength[i] < 25){

snakexlength[i] = 850; }

}

repaint();

} if(down){

for(int i = lengthofsnake-1; i >= 0; i--){

snakexlength[i+1] = snakexlength[i]; }

for(int i = lengthofsnake; i >= 0;i--){

if( i == 0){ snakeylength[i] = snakeylength[i] + 25; }else{

snakeylength[i] = snakeylength[i-1]; }

if(snakeylength[i] > 625){

snakeylength[i] = 75; }

}

repaint(); }

if(up){

for(int i = lengthofsnake-1; i >= 0; i--){

snakexlength[i+1] = snakexlength[i]; }

for(int i = lengthofsnake; i >= 0;i--){

if( i == 0){ snakeylength[i] = snakeylength[i] - 25; }else{

snakeylength[i] = snakeylength[i-1]; }

if(snakexlength[i] < 75){

snakexlength[i] = 625; }

repaint();

} } }

@Override public void keyPressed(KeyEvent arg0) { // TODO Auto-generated method stub

}

@Override public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub

if(arg0.getKeyCode() == KeyEvent.VK_SPACE){ moves = 0; score = 0; lengthofsnake = 3; repaint (); } if(arg0.getKeyCode() == KeyEvent.VK_RIGHT){

moves++; right = true; if(!left){ // keeps snake to the right side so that it cant collide w it self right = true; }else{

right = false; left = true; }

up = false; down = false; }

if(arg0.getKeyCode() == KeyEvent.VK_LEFT){

moves++; left = true; if(!right){ // keeps snake to the right side so that it cant collide w it self left = true; }else{

left = false; right = true; }

up = false; down = false; }

if(arg0.getKeyCode() == KeyEvent.VK_UP){

moves++; up = true; if(!down){ // keeps snake to the right side so that it cant collide w it self up = true; }else{

up = false; down = true; }

right = false; left = false; }

if(arg0.getKeyCode() == KeyEvent.VK_DOWN){

moves++; down = true; if(!up){ // keeps snake to the right side so that it cant collide w it self down = true; }else{

down = false; up = true; }

left = false; right = false; } }

@Override public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub

}

}

Gameplay.java:

import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.Random;

import javax.swing.Timer;

import javax.swing.ImageIcon; import javax.swing.JPanel;

// 59: 07

public class Gameplay extends JPanel implements KeyListener, ActionListener {

private int [] snakexlength = new int[750]; private int [] snakeylength = new int[750];

private boolean left = false; private boolean right = false; private boolean up = false; private boolean down = false;

private ImageIcon rightmouth; private ImageIcon upmouth; private ImageIcon downmouth; private ImageIcon leftmouth;

private int lengthofsnake = 3;

private Timer timer; private int delay = 100; // speed of snake

private ImageIcon snakeimage;

private int [] enemyxpos ={25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425, 450, 475, 500, 575, 600, 625, 650, 675, 700, 725, 800, 825, 850}; private int [] enemyypos = {75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425, 475, 500, 525, 550, 575, 600, 625}; private ImageIcon enemyimage; private Random random = new Random(); private int moves = 0; private int xpos = random.nextInt(34); private int ypos = random.nextInt(23); private int score = 0;

private ImageIcon titleImage;

public Gameplay(){

addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); timer = new Timer(delay, this); timer.start();

} // snake icons are 25 x 25

public void paint(Graphics g){

if(moves == 0){ snakexlength[2] = 50; snakexlength[1] = 75; snakexlength[0] = 100;

snakeylength[2] = 100; snakeylength[1] = 100; snakeylength[0] = 100;

}

// draw title image border g.setColor(Color.white); g.drawRect(24, 10, 851, 55);

// this draws the title image

titleImage = new ImageIcon("snaketitle.jpg"); titleImage.paintIcon(this, g, 25, 11);

// this draws the border for the actual gameplay

g.setColor(Color.WHITE); g.drawRect(24, 74, 851, 577);

//this draws the background for the gameplay

g.setColor(Color.black); g.fillRect(25, 75, 850, 575); //draw scores g.setColor(Color.white); g.setFont(new Font("arial" , Font.PLAIN, 14)); g.drawString("Scores : "+score, 780,30); // draw the length of the snake g.setColor(Color.white); g.setFont(new Font("arial" , Font.PLAIN, 14)); g.drawString("Length : "+lengthofsnake, 780,50);

rightmouth = new ImageIcon("rightmouth.png"); rightmouth.paintIcon(this, g, snakexlength[0], snakeylength[0]);

for(int i = 0; i < lengthofsnake; i++){

if(i == 0 && right){ rightmouth = new ImageIcon("rightmouth.png"); rightmouth.paintIcon(this, g, snakexlength[i], snakeylength[i]); }

if(i == 0 && left){ leftmouth = new ImageIcon("leftmouth.png"); leftmouth.paintIcon(this, g, snakexlength[i], snakeylength[i]); }

if(i == 0 && down){ downmouth = new ImageIcon("downmouth.png"); downmouth.paintIcon(this, g, snakexlength[i], snakeylength[i]); }

if(i == 0 && up){ upmouth = new ImageIcon("upmouth.png"); upmouth.paintIcon(this, g, snakexlength[i], snakeylength[i]); }

if( i != 0){ // body of snake

snakeimage= new ImageIcon("snakeimage.png"); snakeimage.paintIcon(this, g, snakexlength[i], snakeylength[i]);

} } enemyimage = new ImageIcon("enemy.png"); if(enemyxpos[xpos] == snakexlength[0] && enemyypos[ypos] == snakeylength[0]){ score++; lengthofsnake++; xpos = random.nextInt(34); ypos = random.nextInt(23); }

enemyimage.paintIcon(this, g, enemyxpos[xpos], enemyypos[ypos]); for(int b = 1; b < lengthofsnake; b++){ if(snakexlength[b] == snakexlength[0] && snakeylength[b] == snakeylength[0]){ right = false; left = false; up = false; down = false; g.setColor(Color.white); g.setFont(new Font("arial", Font.BOLD, 50)); g.drawString("Game Over", 300, 300); g.setFont(new Font("arial", Font.BOLD, 20)); g.drawString("Space to RESTART", 350, 340); } } g.dispose();

}

@Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub

timer.start(); if(right){

for(int i = lengthofsnake-1; i >= 0; i--){

snakeylength[i+1] = snakeylength[i]; }

for(int i = lengthofsnake; i >= 0;i--){

if( i == 0){ snakexlength[i] = snakexlength[i] + 25; }else{ snakexlength[i] = snakexlength[i-1]; }

if(snakexlength[i] > 850){

snakexlength[i] = 25; }

}

repaint();

} if(left){

for(int i = lengthofsnake-1; i >= 0; i--){

snakeylength[i+1] = snakeylength[i]; }

for(int i = lengthofsnake; i >= 0;i--){

if( i == 0){ snakexlength[i] = snakexlength[i] - 25; }else{

snakexlength[i] = snakexlength[i-1]; }

if(snakexlength[i] < 25){

snakexlength[i] = 850; }

}

repaint();

} if(down){

for(int i = lengthofsnake-1; i >= 0; i--){

snakexlength[i+1] = snakexlength[i]; }

for(int i = lengthofsnake; i >= 0;i--){

if( i == 0){ snakeylength[i] = snakeylength[i] + 25; }else{

snakeylength[i] = snakeylength[i-1]; }

if(snakeylength[i] > 625){

snakeylength[i] = 75; }

}

repaint(); }

if(up){

for(int i = lengthofsnake-1; i >= 0; i--){

snakexlength[i+1] = snakexlength[i]; }

for(int i = lengthofsnake; i >= 0;i--){

if( i == 0){ snakeylength[i] = snakeylength[i] - 25; }else{

snakeylength[i] = snakeylength[i-1]; }

if(snakexlength[i] < 75){

snakexlength[i] = 625; }

repaint();

} } }

@Override public void keyPressed(KeyEvent arg0) { // TODO Auto-generated method stub

}

@Override public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub

if(arg0.getKeyCode() == KeyEvent.VK_SPACE){ moves = 0; score = 0; lengthofsnake = 3; repaint (); } if(arg0.getKeyCode() == KeyEvent.VK_RIGHT){

moves++; right = true; if(!left){ // keeps snake to the right side so that it cant collide w it self right = true; }else{

right = false; left = true; }

up = false; down = false; }

if(arg0.getKeyCode() == KeyEvent.VK_LEFT){

moves++; left = true; if(!right){ // keeps snake to the right side so that it cant collide w it self left = true; }else{

left = false; right = true; }

up = false; down = false; }

if(arg0.getKeyCode() == KeyEvent.VK_UP){

moves++; up = true; if(!down){ // keeps snake to the right side so that it cant collide w it self up = true; }else{

up = false; down = true; }

right = false; left = false; }

if(arg0.getKeyCode() == KeyEvent.VK_DOWN){

moves++; down = true; if(!up){ // keeps snake to the right side so that it cant collide w it self down = true; }else{

down = false; up = true; }

left = false; right = false; } }

@Override public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

In Exercises, find the limit. lim X-8 x + 1 (x + 1)1/3 -2

Answered: 1 week ago

Question

5. Describe the relationship between history and identity.

Answered: 1 week ago