Vista Ean
Vista Ean

Reputation: 31

Making a Button

I am here making a menu for my game where I can press 'Start' button to navigate through the screens. I am trying to implement a seperate button for the class however the way I have laid out the CustomButtons class, it works in a way where I can only make one button which has a function, in order to tackle this issue I decided to make a separate 'Buttons' method which contains the parameters for the button. I have called this within the paint component to ensure that it is being displayed to the screen however only the text 'START' is being displayed to the screen. The background color of the button, the borders, the font etc. isn't being changed alongside the call.

public class CustomButton extends JButton implements MouseListener {

    Dimension size = new Dimension(100, 50);

    boolean hover = false;
    boolean click = false;
    boolean isMethodCalled = false;

    String text = "";

    public CustomButton(String text, Button bb) {
        setVisible(true);
        setFocusable(true);
        setContentAreaFilled(false);
        setBorderPainted(false);

        this.text = text;

        addMouseListener(this);
    }

    public void Button(Graphics g) {
        g.setColor(new Color(255, 255, hover ? 180 : 102 ));
        g.fillRect(0, 0, 250, 7);
        g.fillRect(0, 0, 7, 150);

        g.setColor(Color.ORANGE); // button background color
        g.fillRect(14, 14, 222, 122);
        g.setColor(Color.WHITE); // text color 
        g.setFont(Font.decode("arial-BOLD-24"));

        FontMetrics metrics = g.getFontMetrics();
        int width = metrics.stringWidth(text);
        g.drawString(text, 17, 40);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Button menu = new Button();
    }

    public void setButtonText(String text) {
        this.text = text;
    }
    public String getButtonText(String text) {
        return text;
    }

    public void mouseEntered(MouseEvent e) {
        hover = true;
    }
    public void mouseExited(MouseEvent e) {
        hover = false;
    }
    public void mousePressed(MouseEvent e) {
        click = true;
    }
    public void mouseReleased(MouseEvent e) {
        click = false;
    }
    public void mouseClicked(MouseEvent e) {
    }
}

Anyone have any idea how I can make it so the button once it is called from the 'Buttons' method works so it is displayed exactly as it should be if all the graphics settings were to be set within the paintComponent method?

This is not what is currently happening. I do not want this to happen:

Dont want this to happen

This is what I want to happen to the button:

This is what I want

Upvotes: 3

Views: 115

Answers (1)

Prasad Karunagoda
Prasad Karunagoda

Reputation: 2148

To have the custom appearance you need, it's better that your custom button extends JLabel. Below code demonstrates how we can write a custom button by extending JLabel.

This button supports click events. We can add ActionListeners to listen to click events. It changes the background color to brown when user hovers mouse over it.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;

public class CustomButton extends JLabel implements MouseListener {

  private List<ActionListener> actionListeners = new ArrayList<>();

  public CustomButton(String text) {
    super(text);
    setOpaque(true);
    setForeground(Color.white);
    setBackground(Color.orange);
    setFont(getFont().deriveFont(40.0f));
    addMouseListener(this);
  }

  public void addActionListener(ActionListener listener) {
    actionListeners.add(listener);
  }

  public void removeActionListener(ActionListener listener) {
    actionListeners.remove(listener);
  }

  @Override
  public void mouseClicked(MouseEvent e) {
    for (ActionListener listener : actionListeners) {
      listener.actionPerformed(new ActionEvent(this, 0, "click"));
    }
  }

  @Override
  public void mousePressed(MouseEvent e) {
  }

  @Override
  public void mouseReleased(MouseEvent e) {
  }

  @Override
  public void mouseEntered(MouseEvent e) {
    setBackground(new Color(185, 122, 87));
  }

  @Override
  public void mouseExited(MouseEvent e) {
    setBackground(Color.orange);
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setBackground(Color.darkGray);
    frame.getContentPane().setLayout(new FlowLayout());

    CustomButton customButton = new CustomButton("START");
    customButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(frame, "Button clicked");
      }
    });

    frame.getContentPane().add(customButton);
    frame.setBounds(300, 200, 400, 300);
    frame.setVisible(true);
  }
}

Upvotes: 2

Related Questions