Rabu, 13 Agustus 2025

JAVA - Membuat Tombol Sendiri



File : TMyButton.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tmybutton;
import javax.swing.*;
import javax.swing.plaf.*;
import java.awt.*;
import java.awt.event.*;
// 11. The component class MyButton is a subclass of JComponent.
class MyButton extends JComponent {    
    MyButtonModel model = null;
    String text = "";
    Font font = null;
    FontMetrics fontMetrics = null;
    int border = 10;
    boolean buttonPressed = false;
    
    // 12. MyButton constructor without any arguments.
    public MyButton() {
        this(null);
    }
    
    // 13. MyButton constructor that takes the text label argument.
    public MyButton(String text) {   
        // Assign the data model to the component.
        setModel(new DefaultMyButtonModel());
        // Initialize the text label.
        this.text = text;
        // Obtain the default font.
        font = getFont();
        // If there is no default font, assign a new font.
        if (font == null) {
            font = new Font("SanSerif", Font.BOLD, 14);
        }
        // Retrieve the font metrics of the font.
        fontMetrics = getFontMetrics(font);
        // Register a mouse listener.
        addMouseListener(new ButtonMouseListener());
        
        // Update the view of the button.
        updateUI();
    }
    // 14. Method definition of update.
    public void updateUI() {
        // Assign proper look-and-feel for the view depending on the
        // specified look-and-feel.
        if (MyUIManager.getLookAndFeel().equals("Default")) {
             super.setUI(new MyButtonUI());
        }
        else if (MyUIManager.getLookAndFeel().equals("Oval")) {
            super.setUI(new MyButtonOvalUI());
        }
    }

    // 15. This class is the UI delegate. It has been defined as an
    // inner class to simplify the program. This class provides the default 
    // view of the button.
    class MyButtonUI extends ComponentUI {
    
    // 16. Constructor.
    public MyButtonUI() {
        repaint(); // Call repaint for further actions.
    }
    
    // 17. Retrieve the minimum size of the component.
    public Dimension getMinimumSize() {
        int w = fontMetrics.stringWidth(text);
        int h = fontMetrics.getHeight();
        return new Dimension(w+2*border, h+2*border);
    }

    // 18. Retrieve the preferred size of the component.
    public Dimension getPreferredSize() {
        return getMinimumSize();
    }

    // 19. Retrieve the maximum size of the component.
    public Dimension getMaximumSize() {
        return new Dimension(Short.MAX_VALUE, Short.MAX_VALUE);
    }

    // 20. The update method definition.
    public void update(Graphics g, JComponent c) {
        if (c.isOpaque()) {
           g.setColor(c.getBackground());
           g.fillRect(0,0,getWidth(), getHeight());
        }
        paint(g, c);
    }
    
    // 21. The paint method definition.
    public void paint(Graphics g, JComponent c) {
        g.setFont(font);

        if (buttonPressed == false) {
            buttonView(g, Color.lightGray, Color.black);
        }
        else {
            buttonView(g, Color.gray, Color.white);
        }
    }
    
    // 22. The buttonView method definition.
    public void buttonView(Graphics g, Color background, Color foreground) {
        g.setColor(background);
        g.fill3DRect(0,0, getSize().width, getSize().height, true);
        g.setColor(foreground);
        int x = (getSize().width-fontMetrics.stringWidth(text))/2;
        int y = (getSize().height+fontMetrics.getHeight()
                                 -fontMetrics.getDescent())/2;
        g.drawString(text, x, y);
    }
    }


    // 23. Implementation of oval shaped look-and-feel for MyButton.
    // This view of the button is used as an alternative to its default view.
    class MyButtonOvalUI extends ComponentUI {

    // 24. Constructor.
    public MyButtonOvalUI() {
        repaint();
    }

    // 25. Retrieve the minimum size of the component.
    public Dimension getMinimumSize() {
        int w = fontMetrics.stringWidth(text);
        int h = fontMetrics.getHeight();
        return new Dimension(w+2*border, h+2*border);
    }

    // 26. Retrieve the preferred size of the component.
    public Dimension getPreferredSize() {
        return getMinimumSize();
    }

    // 27. Retrieve the maximum size of the component.
    public Dimension getMaximumSize() {
return new Dimension(Short.MAX_VALUE, Short.MAX_VALUE);
    }
    // 28. The update method definition.
    public void update(Graphics g, JComponent c) {
        if (c.isOpaque()) {
           g.setColor(c.getBackground());
           g.fillRect(0,0,getWidth(), getHeight());
        }
        paint(g, c);
    }

    // 29. The paint method definition.
    public void paint(Graphics g, JComponent c) {
        g.setFont(font);

        if (buttonPressed == false) {
            buttonView(g, Color.gray, Color.white);
        }
        else {
            buttonView(g, Color.lightGray, Color.black);
        }
    }

    // 30. The button view definition.
    public void buttonView(Graphics g, Color background, Color foreground) {
        g.setColor(background);
        g.fillOval(0,0, getSize().width, getSize().height);
        g.setColor(foreground);
        int x = (getSize().width-fontMetrics.stringWidth(text))/2;
        int y = (getSize().height+fontMetrics.getHeight()
                                 -fontMetrics.getDescent())/2;
        g.drawString(text, x, y);
    }
    }

    // 31. When the user presses or releases the mouse over a button,
    // the functionality from the implementation methods from this
    // mouse listener class are executed.
    class ButtonMouseListener extends MouseAdapter {
        public void mousePressed(MouseEvent evt) {
            requestFocus();
            buttonPressed = true;
            ((MyButton) evt.getSource()).repaint();
        }

        public void mouseReleased(MouseEvent evt) {
            buttonPressed = false;
            ((MyButton) evt.getSource()).repaint();
            ActionEvent actionEvent = new ActionEvent(
                                          MyButton.this,
                                          ActionEvent.ACTION_PERFORMED,
                                          MyButton.this.getActionCommand());
            processEvent(actionEvent);
        }
    }

    //Supports the action listener.
    ActionListener actionListener;

    // 32. Method to register an action listener with the button.
    public void addActionListener(ActionListener l) {
        actionListener = AWTEventMulticaster.add(actionListener, l);
    }
    // 33. To figure out the action events.
    protected void processEvent(AWTEvent evt) {
        if (evt instanceof ActionEvent) {
            processActionEvent((ActionEvent) evt);
        }
        else {
            super.processEvent(evt);
        }
    }
    // 34. To process the action events.
    protected void processActionEvent(ActionEvent evt) {
        if (actionListener != null) {
            // Execute the action performed from the listener class.
            actionListener.actionPerformed(evt);
        }
    }
    // 35. Method to assign the button model.
    public void setModel(MyButtonModel model) {
        this.model = model;
    }
    // 36. Retrieve the action model.
    public MyButtonModel getModel() {
        return model;
    }
    // 37. Assign the text label to the button.
    public void setText(String text) {
        this.text = text;
        invalidate();
        repaint();
    }
    // 38. Retrieve the text label of the button.
    public String getText() {
        return text;
}
    // 39. To assign the action command.
    public void setActionCommand(String actionCommand) {
getModel().setActionCommand(actionCommand);
    }
    // 40. To retrieve the action command.
    public String getActionCommand() {
        String ac = getModel().getActionCommand();
        if (ac == null) {
            ac = getText();
        }
        return ac;
    }
}

// 41. Define a simple button model interface.
interface MyButtonModel {
    public void setActionCommand(String actionCommand);
    public String getActionCommand();
}

// 42. Define the default button model class that implements the 
// MyButtonModel interface.
class DefaultMyButtonModel implements MyButtonModel {
    protected String actionCommand = null;
    // 43. Assign an action command.
    public void setActionCommand(String actionCommand) {
        this.actionCommand = actionCommand;
    }
    // 44. Retrieve the action command that has been assigned.
    public String getActionCommand() {
        return actionCommand;
    }
}
======================================================

File : MyUIManager.java

/*

 * To change this license header, choose License Headers in Project Properties.

 * To change this template file, choose Tools | Templates

 * and open the template in the editor.

 */

package tmybutton;

class MyUIManager {

    // Store the look-and-feel string.

    static String lfType = "Default";

    // 46. Assign a new look-and-feel.

    static public void setLookAndFeel(String type) {

        lfType = type;

    }

    // 47. Retrieve the current look-and-feel.

    static public String getLookAndFeel() {

        return lfType; 

    }

}


 =========================================

File : MyButton.java

/*

 * To change this license header, choose License Headers in Project Properties.

 * To change this template file, choose Tools | Templates

 * and open the template in the editor.

 */

package tmybutton;

import javax.swing.*;

import javax.swing.plaf.*;

import java.awt.*;

import java.awt.event.*;

// 11. The component class MyButton is a subclass of JComponent.

class MyButton extends JComponent {    

    MyButtonModel model = null;

    String text = "";

    Font font = null;

    FontMetrics fontMetrics = null;

    int border = 10;

    boolean buttonPressed = false;

    

    // 12. MyButton constructor without any arguments.

    public MyButton() {

        this(null);

    }

    

    // 13. MyButton constructor that takes the text label argument.

    public MyButton(String text) {   

        // Assign the data model to the component.

        setModel(new DefaultMyButtonModel());

        // Initialize the text label.

        this.text = text;

        // Obtain the default font.

        font = getFont();

        // If there is no default font, assign a new font.

        if (font == null) {

            font = new Font("SanSerif", Font.BOLD, 14);

        }

        // Retrieve the font metrics of the font.

        fontMetrics = getFontMetrics(font);

        // Register a mouse listener.

        addMouseListener(new ButtonMouseListener());

        

        // Update the view of the button.

        updateUI();

    }

    // 14. Method definition of update.

    public void updateUI() {

        // Assign proper look-and-feel for the view depending on the

        // specified look-and-feel.

        if (MyUIManager.getLookAndFeel().equals("Default")) {

             super.setUI(new MyButtonUI());

        }

        else if (MyUIManager.getLookAndFeel().equals("Oval")) {

            super.setUI(new MyButtonOvalUI());

        }

    }


    // 15. This class is the UI delegate. It has been defined as an

    // inner class to simplify the program. This class provides the default 

    // view of the button.

    class MyButtonUI extends ComponentUI {

    

    // 16. Constructor.

    public MyButtonUI() {

        repaint(); // Call repaint for further actions.

    }

    

    // 17. Retrieve the minimum size of the component.

    public Dimension getMinimumSize() {

        int w = fontMetrics.stringWidth(text);

        int h = fontMetrics.getHeight();

        return new Dimension(w+2*border, h+2*border);

    }


    // 18. Retrieve the preferred size of the component.

    public Dimension getPreferredSize() {

        return getMinimumSize();

    }


    // 19. Retrieve the maximum size of the component.

    public Dimension getMaximumSize() {

        return new Dimension(Short.MAX_VALUE, Short.MAX_VALUE);

    }


    // 20. The update method definition.

    public void update(Graphics g, JComponent c) {

        if (c.isOpaque()) {

           g.setColor(c.getBackground());

           g.fillRect(0,0,getWidth(), getHeight());

        }

        paint(g, c);

    }

    

    // 21. The paint method definition.

    public void paint(Graphics g, JComponent c) {

        g.setFont(font);


        if (buttonPressed == false) {

            buttonView(g, Color.lightGray, Color.black);

        }

        else {

            buttonView(g, Color.gray, Color.white);

        }

    }

    

    // 22. The buttonView method definition.

    public void buttonView(Graphics g, Color background, Color foreground) {

        g.setColor(background);

        g.fill3DRect(0,0, getSize().width, getSize().height, true);

        g.setColor(foreground);

        int x = (getSize().width-fontMetrics.stringWidth(text))/2;

        int y = (getSize().height+fontMetrics.getHeight()

                                 -fontMetrics.getDescent())/2;

        g.drawString(text, x, y);

    }

    }



    // 23. Implementation of oval shaped look-and-feel for MyButton.

    // This view of the button is used as an alternative to its default view.

    class MyButtonOvalUI extends ComponentUI {


    // 24. Constructor.

    public MyButtonOvalUI() {

        repaint();

    }


    // 25. Retrieve the minimum size of the component.

    public Dimension getMinimumSize() {

        int w = fontMetrics.stringWidth(text);

        int h = fontMetrics.getHeight();

        return new Dimension(w+2*border, h+2*border);

    }


    // 26. Retrieve the preferred size of the component.

    public Dimension getPreferredSize() {

        return getMinimumSize();

    }


    // 27. Retrieve the maximum size of the component.

    public Dimension getMaximumSize() {

return new Dimension(Short.MAX_VALUE, Short.MAX_VALUE);

    }

    // 28. The update method definition.

    public void update(Graphics g, JComponent c) {

        if (c.isOpaque()) {

           g.setColor(c.getBackground());

           g.fillRect(0,0,getWidth(), getHeight());

        }

        paint(g, c);

    }


    // 29. The paint method definition.

    public void paint(Graphics g, JComponent c) {

        g.setFont(font);


        if (buttonPressed == false) {

            buttonView(g, Color.gray, Color.white);

        }

        else {

            buttonView(g, Color.lightGray, Color.black);

        }

    }


    // 30. The button view definition.

    public void buttonView(Graphics g, Color background, Color foreground) {

        g.setColor(background);

        g.fillOval(0,0, getSize().width, getSize().height);

        g.setColor(foreground);

        int x = (getSize().width-fontMetrics.stringWidth(text))/2;

        int y = (getSize().height+fontMetrics.getHeight()

                                 -fontMetrics.getDescent())/2;

        g.drawString(text, x, y);

    }

    }


    // 31. When the user presses or releases the mouse over a button,

    // the functionality from the implementation methods from this

    // mouse listener class are executed.

    class ButtonMouseListener extends MouseAdapter {

        public void mousePressed(MouseEvent evt) {

            requestFocus();

            buttonPressed = true;

            ((MyButton) evt.getSource()).repaint();

        }


        public void mouseReleased(MouseEvent evt) {

            buttonPressed = false;

            ((MyButton) evt.getSource()).repaint();

            ActionEvent actionEvent = new ActionEvent(

                                          MyButton.this,

                                          ActionEvent.ACTION_PERFORMED,

                                          MyButton.this.getActionCommand());

            processEvent(actionEvent);

        }

    }


    //Supports the action listener.

    ActionListener actionListener;


    // 32. Method to register an action listener with the button.

    public void addActionListener(ActionListener l) {

        actionListener = AWTEventMulticaster.add(actionListener, l);

    }

    // 33. To figure out the action events.

    protected void processEvent(AWTEvent evt) {

        if (evt instanceof ActionEvent) {

            processActionEvent((ActionEvent) evt);

        }

        else {

            super.processEvent(evt);

        }

    }

    // 34. To process the action events.

    protected void processActionEvent(ActionEvent evt) {

        if (actionListener != null) {

            // Execute the action performed from the listener class.

            actionListener.actionPerformed(evt);

        }

    }

    // 35. Method to assign the button model.

    public void setModel(MyButtonModel model) {

        this.model = model;

    }

    // 36. Retrieve the action model.

    public MyButtonModel getModel() {

        return model;

    }

    // 37. Assign the text label to the button.

    public void setText(String text) {

        this.text = text;

        invalidate();

        repaint();

    }

    // 38. Retrieve the text label of the button.

    public String getText() {

        return text;

}

    // 39. To assign the action command.

    public void setActionCommand(String actionCommand) {

getModel().setActionCommand(actionCommand);

    }

    // 40. To retrieve the action command.

    public String getActionCommand() {

        String ac = getModel().getActionCommand();

        if (ac == null) {

            ac = getText();

        }

        return ac;

    }

}


// 41. Define a simple button model interface.

interface MyButtonModel {

    public void setActionCommand(String actionCommand);

    public String getActionCommand();

}


// 42. Define the default button model class that implements the 

// MyButtonModel interface.

class DefaultMyButtonModel implements MyButtonModel {

    protected String actionCommand = null;

    // 43. Assign an action command.

    public void setActionCommand(String actionCommand) {

        this.actionCommand = actionCommand;

    }

    // 44. Retrieve the action command that has been assigned.

    public String getActionCommand() {

        return actionCommand;

    }

}


Tidak ada komentar: