Guys I'm trying to create a 2d basic game for my homework the game rules are easy and simple which is there are 2 images (ball and droplets) if you click on the ball it will give us +2 points and if you click on droplets it will give us +1 points but if droplets reach the ground we lose -5 points so we need to click on droplets as much as we can and the ball can hit the borders of the frame it will bounce back and my teacher want from me to add menuBar with different menuItem which I did there is no problem with that
But I can't figure out how to add mouse listener to an image I tried to turn them into a Label but this time I had another problem which is how to print a label they're not string or line or shape or 2d objects
So this is the code that I wrote
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
public class Game extends JPanel implements ActionListener,MouseListener {
/*dx = droplet's x position, dy = droplet's y position
*bx = ball's x position, by = ball's y position
*/
int dxPosition,dyPosition;
int ballVelocity,dropletVelocity;
int bxPosition,byPosition;
boolean ballToRight,ballToDown;
boolean dropletToDown;
int result;
Image droplet,ball;
Font lifeFont;
JTextField txtLife;
int lifePoint = 10;
Timer timer;
public Game() {
setLayout(null);
lifeFont = new Font("TimesRoma",Font.ITALIC,30);
txtLife = new JTextField("Life Points: "+lifePoint);
txtLife.setFont(lifeFont);
txtLife.setEditable(false);
txtLife.setBounds(0,720,8000,50);
txtLife.setOpaque(true);
txtLife.addMouseListener(this);
add(txtLife);
//Adding the images
droplet = new ImageIcon("droplet.png").getImage();
ball = new ImageIcon("ball.png").getImage();
ballVelocity = 10;
dropletVelocity = 10;
timer = new Timer(30,this);
timer.start();
setSize(800,800);
setBackground(Color.DARK_GRAY);
setVisible(true);
}
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
//Creating and putting the 2D images on the panel
Graphics2D g2D = (Graphics2D) g;
g2D.drawImage(ball, bxPosition, byPosition, this);
g2D.drawImage(droplet, dxPosition, dyPosition, this);
}
public static void main(String[] args) {
Game animateGame = new Game();
JFrame myFrame = new JFrame("Game Play");
myFrame.setSize(animateGame.getSize());
myFrame.setVisible(true);
myFrame.setLocationRelativeTo(null);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.add(animateGame);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource().equals(timer)) {
//When the timer start call these methods to work
ballHorizontal();
ballVertical();
dropletVertical();
}
//and repainted it
repaint();
}
public void ballHorizontal() {
if(bxPosition<750 && ballToRight == true)
bxPosition += ballVelocity;
else if(bxPosition>2){
ballToRight = false;
bxPosition -= ballVelocity;
}
else
ballToRight = true;
}
public void ballVertical() {
if(byPosition<665 && ballToDown==true)
byPosition += ballVelocity+2;
else if(byPosition>15){
ballToDown = false;
byPosition -= ballVelocity;
}
else
ballToDown = true;
}
public void dropletVertical() {
if(dyPosition<665 && dropletToDown==true)
dyPosition += dropletVelocity;
else if(dyPosition>15){
dropletToDown = false;
dyPosition -= dropletVelocity;
}
else
dropletToDown = true;
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if(e.getSource().equals(txtLife))
lifePoint += 2;
txtLife.setText("Life Points: "+ lifePoint);
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
These are the photos of the current version
My question is how can I make it change whenever I click the ball or droplet it will give me extra life points, when droplet and ball collide it will reduce life points and when droplets hit the ground it will reduce life points as well so when we click to droplet it will disappear and start falling over from somewhere in the x coordinate