klasscho

GuiButton

May 31st, 2020
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. package com.packadge.gui;
  2.  
  3. import com.packadge.DrawUtils;
  4. import com.packadge.Game;
  5.  
  6. import javax.swing.plaf.nimbus.State;
  7. import java.awt.*;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.MouseEvent;
  10. import java.util.ArrayList;
  11.  
  12. public class GuiButton {
  13.  
  14.     private State currentState = State.RELEASED;
  15.     private Rectangle clickBox;
  16.     private ArrayList<ActionListener> actionListeners;
  17.     private String text = "";
  18.  
  19.     private Color released;
  20.     private Color hover;
  21.     private Color pressed;
  22.     private Font font = Game.main.deriveFont(22f);
  23.  
  24.     public GuiButton (int x, int y, int width, int height){
  25.         clickBox = new Rectangle(x, y, width, height);
  26.         actionListeners = new ArrayList<ActionListener>();
  27.         released = new Color(173, 177, 179);
  28.         hover = new Color(150, 156, 158);
  29.         pressed = new Color(111, 116, 117);
  30.     }
  31.  
  32.     public void update(){}
  33.  
  34.     public void render(Graphics2D g){
  35.         if (currentState == State.RELEASED){
  36.             g.setColor(released);
  37.             g.fill(clickBox);
  38.         }
  39.         else if (currentState == State.HOVER){
  40.             g.setColor(hover);
  41.             g.fill(clickBox);
  42.         }
  43.         else {
  44.             g.setColor(pressed);
  45.             g.fill(clickBox);
  46.         }
  47.         g.setColor(Color.white);
  48.         g.setFont(font);
  49.         g.drawString(text, clickBox.x + clickBox.width/2 - DrawUtils.getMessageWidth(text, font, g)/2, clickBox.y + clickBox.height/2 + DrawUtils.getMessageHeight(text, font, g)/2);
  50.     }
  51.  
  52.     void addActionListener(ActionListener listener){
  53.         actionListeners.add(listener);
  54.     }
  55.  
  56.     public void mousePressed(MouseEvent e){
  57.         if (clickBox.contains(e.getPoint())){
  58.             currentState = State.PRESSED;
  59.         }
  60.     }
  61.  
  62.     public void mouseReleased(MouseEvent e){
  63.         if (clickBox.contains(e.getPoint())){
  64.             for (ActionListener al : actionListeners){
  65.                 al.actionPerformed(null);
  66.             }
  67.         }
  68.         currentState = State.RELEASED;
  69.     }
  70.     public void mouseDragged(MouseEvent e){
  71.         if (clickBox.contains(e.getPoint())){
  72.             currentState = State.PRESSED;
  73.         }
  74.         else{
  75.             currentState = State.RELEASED;
  76.         }
  77.     }
  78.     public void mouseMoved(MouseEvent e){
  79.         if (clickBox.contains(e.getPoint())){
  80.             currentState = State.HOVER;
  81.         }
  82.         else{
  83.             currentState = State.RELEASED;
  84.         }
  85.     }
  86.  
  87.     public int getX(){
  88.         return clickBox.x;
  89.     }
  90.  
  91.     public int getY(){
  92.         return clickBox.y;
  93.     }
  94.     public int getWidth(){
  95.         return clickBox.width;
  96.     }
  97.     public int getHeight(){
  98.         return clickBox.height;
  99.     }
  100.  
  101.     public void  setText(String text){
  102.         this.text = text;
  103.     }
  104.  
  105.     private enum State{
  106.         RELEASED,
  107.         HOVER,
  108.         PRESSED
  109.     }
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment