I need to add treemap to the following program, it has 2 classes Main.java and G
ID: 3850327 • Letter: I
Question
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 javax.swing.JFrame;
public class Main {
public static void main (String [] args){
JFrame obj = new JFrame();
Gameplay gameplay = new Gameplay();
obj.setBounds(10,10,905,700);
obj.setBackground(Color.DARK_GRAY);
obj.setResizable(false);
obj.setVisible(true);
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.add(gameplay);
}
}
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
}
}
Explanation / Answer
TreeMap in java is a collection used if we want to maintain insertion order , unique values and store values according to keys. In this Game , there is no need of TreeMap as requirement of such collection is not needed. Simple array will work for this problem. If you want to know how treemap is used . Here is the Syntax:
TreeMap<int,int> tm=new TreeMap<int,int>(); // define an object of treemap "< >"is used to define generic type
//to insert value in it
tm.put(101,11);
tm.put(102,12);
tm.put(103,14);
tm.put(101,11);
it will put folowing values in treemap(101,102,103). NO DUPLICATE VALUES.
Here there is no use of such collection.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.