0

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

enter image description here

enter image description here

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

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Emin-35
  • 71
  • 5
  • 1
    Perhaps you could reduce this to a [mre] rather than hundreds of lines of code. – khelwood Nov 14 '21 at 14:48
  • I am so sorry about that I edited the code but still it's long :( – Emin-35 Nov 14 '21 at 15:22
  • Your title mentions "moving" the image, but the description of the problem only talks about "clicking" the image. Not sure what you are asking. In any case a couple of issues: 1) painting should be done in the `paintComponent(...)` method. 2) for flexibility the images you want to paint should be contained in an ArrayList and you should have a custom object that contains a) the Pont where the images should be painted and b) the image. Then the painting method iterates through the ArrayList to paint each image. So you can easily paint more the 2. This will also make hit detection logic easier. – camickr Nov 14 '21 at 15:45
  • for examples of the above approach check out: 1) https://stackoverflow.com/questions/54028090/get-width-and-height-of-jpanel-outside-of-the-class/54028681#54028681 and 2) https://stackoverflow.com/questions/67443343/drag-a-painted-shape – camickr Nov 14 '21 at 15:45
  • Why do you need to add a mouse listener to an image? If you know the coordinates of the images and they are moving randomly on the screen then you should be able to tell when they overlap. Same is true for the mouse listener. When the coordinates of the images contain the current mouse position, then you have "touched" the image with the mouse. – WJS Nov 14 '21 at 17:33
  • Can you use a [Sahpe](https://docs.oracle.com/javase/7/docs/api/java/awt/Shape.html) instead of an image ? Using [Shap#contains](https://docs.oracle.com/javase/7/docs/api/java/awt/Shape.html#contains(double,%20double)) makes it easy to detect if the clicked point is within the shape. – c0der Nov 15 '21 at 09:51
  • I solved btw I used getters to get ball image's x and y coordinates when it's moving and I get my mouse's x and y coordinate according to where I click on the frame and if they're both are same I added +1 point to myself (`if(((ballxSmall < prvX) && (prvX < ballxBig)) && ((ballySmall < prvY) && (prvY < ballyBig))) { lifePoint += 1; txtLife.setText("Life Points: "+lifePoint); }`) Like this – Emin-35 Nov 15 '21 at 13:27
  • Now you might answer it yourself, or delete the question. In its current form, it's not much use to later visitors. – Andrew Thompson Nov 15 '21 at 22:48

0 Answers0