Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a comment to explain the code below import javax.swing.JButton; import javax.swing.JFrame; public class Main { public static void main(String[] args) { JFrame frame =

Write a comment to explain the code below

import javax.swing.JButton; import javax.swing.JFrame;

public class Main { public static void main(String[] args) { JFrame frame = new JFrame("game"); JFrame startScreen = new JFrame(); JButton start = new JButton("Click here to Start Play"); destroyBlocks panel = new destroyBlocks(frame,startScreen); start.addActionListener(listener -> { startScreen.setVisible(false); frame.setVisible(true); }); frame.getContentPane().add(panel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(false); frame.setSize(490, 600); frame.setResizable(false); startScreen.getContentPane().add(start); startScreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); startScreen.setVisible(true); startScreen.setSize(490, 600); startScreen.setResizable(false); }

} ***********************************************************************************************

import java.awt.Graphics; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.ArrayList;

import javax.swing.JFrame; import javax.swing.JPanel;

public class destroyBlocks extends JPanel implements KeyListener {

ArrayList colorableBlocks; ColorableBlocks ball; ColorableBlocks paddle; JFrame mainFrame,startScreen; Thread thread; void reset() { colorableBlocks = new ArrayList(); ball = new ColorableBlocks(237, 435, 35, 25, "ball.png"); paddle = new ColorableBlocks(175, 480, 150, 25, "rectangle.png"); for (int i = 0; i < 8; i++) colorableBlocks.add(new ColorableBlocks((i*60+2),0,60,25,"blue_stars.png")); for (int i = 0; i < 8; i++) colorableBlocks.add(new ColorableBlocks((i*60+2),25,60,25,"purple_stars.png")); for (int i = 0; i < 8; i++) colorableBlocks.add(new ColorableBlocks((i*60+2),50,60,25,"yellow_stars.png")); for (int i = 0; i < 8; i++) colorableBlocks.add(new ColorableBlocks((i*60+2),75,60,25,"red_stars.png"));

addKeyListener(this); setFocusable(true); }

destroyBlocks(JFrame frame, JFrame startScreen) { this.mainFrame = frame; this.startScreen = startScreen;

reset(); thread = new Thread(() -> { while (true) { update(); try { Thread.sleep(10); } catch (InterruptedException err) { err.printStackTrace(); } } }); thread.start(); }

public void paintComponent(Graphics g) { colorableBlocks.forEach(block -> { block.draw(g, this); }); ball.draw(g, this); paddle.draw(g, this); }

public void update() { ball.x += ball.movX; if(ball.x > (getWidth() - 25) || ball.x < 0) ball.movX *= -1; if(ball.y < 0 || ball.intersects(paddle)) ball.movY *= -1; ball.y += ball.movY; if(ball.y > getHeight()) { thread = null; reset(); mainFrame.setVisible(false); startScreen.setVisible(true); } colorableBlocks.forEach(block -> { if(ball.intersects(block) && !block.destroyed) { ball.movY *=-1; block.destroyed = true; } }); repaint();

}

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

}

@Override public void keyPressed(KeyEvent e) {

if (e.getKeyCode() == KeyEvent.VK_RIGHT && paddle.x < (getWidth() - paddle.width)) { paddle.x += 15; }

if (e.getKeyCode() == KeyEvent.VK_LEFT && paddle.x > 0) { paddle.x -= 15; }

}

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

}

}

***********************************************************************************************

import java.awt.Component; import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle; import java.io.File; import java.io.IOException;

import javax.imageio.ImageIO;

public class ColorableBlocks extends Rectangle {

Image pic; boolean destroyed; int movX,movY;

ColorableBlocks(int x, int y, int w, int h, String s) { this.x = x; this.y = y; movX = 3; movY = 3;

this.width = w; this.height = h; try { pic = ImageIO.read(new File("src/"+s)); } catch (IOException e) { e.printStackTrace(); } }

public void draw(Graphics g, Component c) { if (!destroyed) g.drawImage(pic, x, y, width, height, c); }

}

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

Database Systems Design Implementation And Management

Authors: Carlos Coronel, Steven Morris

14th Edition

978-0357673034

More Books

Students also viewed these Databases questions

Question

1. What are your creative strengths?

Answered: 1 week ago