0

I was following a tutorial about creating buttons for games in JAVA. But the code I made is not working and I cand seems to see what is wrong with it. It is supposed to create a rectangle (Which is working fine) and then uses the Mouse Adapter to detect the movement of the mouse when it's clicking on the rectangle. Am I missing a Mouse Listener? Is it needed?

 import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;

public class ButtonPress extends JFrame {

private JPanel contentPane;

Rectangle startButton = new Rectangle(150, 100, 100, 25);
Rectangle quitButton = new Rectangle(150, 150, 100, 25);


public void paint(Graphics g) {

    g.setColor(Color.CYAN);
    g.fillRect(startButton.x, startButton.y, startButton.width, startButton.height);
    g.setFont(new Font("Arial", Font.BOLD, 12));
    g.setColor(Color.GRAY);
    g.drawString("Start Game", startButton.x+20, startButton.y+17);

    g.setColor(Color.CYAN);
    g.fillRect(quitButton.x, quitButton.y, quitButton.width, quitButton.height);
    g.setFont(new Font("Arial", Font.BOLD, 12));
    g.setColor(Color.GRAY);
    g.drawString("Quit Game", quitButton.x+20, quitButton.y+17);

}

public class MouseHandler extends MouseAdapter {
    @Override
    public void mouseMoved(MouseEvent e) {

    }
    @Override
    public void mousePressed(MouseEvent e) {
        int mx = e.getX();
        int my = e.getY();
        if(mx > startButton.x && mx < startButton.x+startButton.width &&
            my > startButton.y && my < startButton.y+startButton.height) {
            JOptionPane.showMessageDialog(null, "Starting");
        }
    }
}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                ButtonPress frame = new ButtonPress();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public ButtonPress() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    }

}
  • Yes, you need to add the MouseListener to a component. How can it listen if not added? – Hovercraft Full Of Eels May 27 '18 at 14:18
  • You've other issues here: 1) don't draw directly within a JFrame or in the paint method. Instead draw within the paintComponent method of a JPanel. 2) Call the super's same painting method within your own override. 3) Rectangles have a `contains(Point p)` method which you should use. Google the tutorials for more on this. – Hovercraft Full Of Eels May 27 '18 at 14:20

0 Answers0