- Pada project java yang telah di buat, buatlah class baru, untuk membuat java class baru, klik kanan pada java package ➜ New ➜ Java Class. sebagai contoh saya beri nama jButtonMac.java.
- Selanjtnya pada java class yang sudah di buat masukan kode berikut
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JButton;
/**
*
* @author anerfinity
*/
public class jButtonMac extends JButton {
private Color AeMouseHover = new Color(0, 168, 255);
private Color AeMousePress = new Color(0, 151, 230);
private Color AeWarnaBackground = new Color(30, 110, 240);
private boolean enter, press;
//rgb(0, 151, 230)
public boolean isEnter() {
return enter;
}
public void setEnter(boolean enter) {
this.enter = enter;
repaint();
}
public boolean isPress() {
return press;
}
public void setPress(boolean press) {
this.press = press;
repaint();
}
public jButtonMac() {
setOpaque(false);
setBorderPainted(false);
setFocusPainted(false);
setContentAreaFilled(false);
setForeground(Color.white);
setFont(getFont().deriveFont(Font.PLAIN));
addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
super.mouseEntered(e);
setEnter(true);
}
@Override
public void mouseExited(MouseEvent e) {
super.mouseExited(e);
setEnter(false);
}
@Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
setPress(true);
}
@Override
public void mouseReleased(MouseEvent e) {
super.mouseReleased(e);
setPress(false);
}
});
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D gd = (Graphics2D) g.create();
Color warna = AeWarnaBackground;
Shape shape = new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), getHeight(), getHeight());
if (isEnter()) {
warna = AeMouseHover;
if (isPress()) {
warna = AeMousePress;
}
}
gd.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
gd.setColor(warna);
gd.fill(shape);
gd.dispose();
super.paintComponent(g);
}
public Color getAeWarnaBackground() {
return this.AeWarnaBackground;
}
public void setAeWarnaBackground(Color bgColor) {
this.AeWarnaBackground = bgColor;
}
//Warna mouse hover Button
public Color getAeMouseHover() {
return this.AeMouseHover;
}
public void setAeMouseHover(Color mousehover) {
this.AeMouseHover = mousehover;
}
//Warna mouse press Button
public Color getAeMousePress() {
return this.AeMousePress;
}
public void setAeMousePress(Color mousepress) {
this.AeMousePress = mousepress;
}
}
- Simpan dan lakukan proses Clean and build project. dapat dengan dua cara klik pada icon palu dan sapu atau klik kanan pada project dan pilih clean and Build
- Drag class jButtonMac kedalam Form aktif
- Untuk merubah Warnabackground, Mousehover dan Mousepress berada di menu properties di tab sebelah kanan
No comments