Skip to content

Instantly share code, notes, and snippets.

@Tadukoo
Created May 4, 2020 01:20
Show Gist options
  • Save Tadukoo/07276fc7f2b6e01bcd349663388abec6 to your computer and use it in GitHub Desktop.
Save Tadukoo/07276fc7f2b6e01bcd349663388abec6 to your computer and use it in GitHub Desktop.
Tadukoo Look & Feel After Journey Post #3
package com.gmail.realtadukoo.util.lookandfeel;
import java.awt.*;
public interface Shaped{
Polygon getShape(int x, int y, int width, int height);
}
package com.gmail.realtadukoo.util.lookandfeel;
import javax.swing.border.AbstractBorder;
import javax.swing.plaf.UIResource;
import java.awt.*;
public class TadukooBorder extends AbstractBorder implements UIResource{
public static Insets borderInsets = new Insets(10, 10, 10, 10);
@Override
public Insets getBorderInsets(Component c){
return borderInsets;
}
@Override
public Insets getBorderInsets(Component c, Insets insets){
return borderInsets;
}
public void paintBorder(Component c, Graphics g, int x, int y, int w, int h){
if(!(c instanceof Shaped)){
return;
}
Polygon shape = ((Shaped) c).getShape(x, y, w, h);
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(new BasicStroke(2));
g.setColor(Color.RED);
g.drawPolygon(shape);
}
}
package com.gmail.realtadukoo.util.lookandfeel;
import javax.swing.*;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.metal.MetalButtonUI;
import java.awt.*;
import java.util.List;
public class TadukooButtonUI extends MetalButtonUI{
/**
* Returns an instance of {@code MetalButtonUI}.
*
* @param c a component
* @return an instance of {@code MetalButtonUI}
*/
public static ComponentUI createUI(JComponent c){
return new TadukooButtonUI();
}
@Override
protected void paintButtonPressed(Graphics g, AbstractButton b){
if(b.isContentAreaFilled()){
Dimension size = b.getSize();
g.setColor(getSelectColor());
if(b instanceof Shaped){
List<?> gradList = (List<?>) UIManager.get("Button.gradient");
float mid1 = ((Number) gradList.get(0)).floatValue();
float mid2 = ((Number) gradList.get(1)).floatValue();
Color c1 = (Color) gradList.get(2);
Color c2 = (Color) gradList.get(3);
Color c3 = (Color) gradList.get(4);
((Graphics2D) g).setPaint(new LinearGradientPaint(0, 0, size.width, 0, new float[]{0, mid1, mid1*2 + mid2, 1}, new Color[]{c1, c2, c1, c3}));
g.fillPolygon(((Shaped) b).getShape(0, 0, size.width, size.height));
}else{
g.fillRect(0, 0, size.width, size.height);
}
}
}
@Override
protected void paintFocus(Graphics g, AbstractButton b,
Rectangle viewRect, Rectangle textRect, Rectangle iconRect){
Rectangle focusRect = new Rectangle();
String text = b.getText();
boolean isIcon = b.getIcon() != null;
// If there is text
if ( text != null && !text.isEmpty()) {
if ( !isIcon ) {
focusRect.setBounds( textRect );
}
else {
focusRect.setBounds( iconRect.union( textRect ) );
}
}
// If there is an icon and no text
else if ( isIcon ) {
focusRect.setBounds( iconRect );
}
g.setColor(getFocusColor());
g.drawRect((focusRect.x-1), (focusRect.y-1),
focusRect.width+1, focusRect.height+1);
}
}
package com.gmail.realtadukoo.util.lookandfeel;
import javax.swing.*;
import javax.swing.plaf.basic.BasicLookAndFeel;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.metal.OceanTheme;
public class TadukooLookAndFeel extends MetalLookAndFeel{
@Override
public String getName(){
return "Tadukoo";
}
@Override
public String getID(){
return "Tadukoo";
}
@Override
public String getDescription(){
return "The Tadukoo Look and Feel";
}
@Override
public boolean isNativeLookAndFeel(){
return false;
}
@Override
public boolean isSupportedLookAndFeel(){
return true;
}
@Override
protected void initClassDefaults(UIDefaults table){
super.initClassDefaults(table);
table.put("ButtonUI", "com.gmail.realtadukoo.util.lookandfeel.TadukooButtonUI");
}
@Override
protected void initComponentDefaults(UIDefaults table){
super.initComponentDefaults(table);
table.put("Button.border", new TadukooBorder());
}
}
package com.gmail.realtadukoo.util.lookandfeel;
import java.awt.*;
public interface TadukooShape extends Shaped{
@Override
default Polygon getShape(int x, int y, int width, int height){
Polygon polygon = new Polygon();
polygon.addPoint(x + 1, y + 1);
polygon.addPoint(x + width - 10, y + 1);
polygon.addPoint(x + width - 1, y + 10);
polygon.addPoint(x + width - 1, y + height - 1);
polygon.addPoint(x + 10, y + height - 1);
polygon.addPoint(x + 1, y + height - 10);
return polygon;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment