Please write a comment for each line to explain the purpose of the code. import java.awt.Component; import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle; import java.io.File; import
Please write a comment for each line to explain the purpose of the code.
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); }
} ***************************************
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
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 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); }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started