Skip to content

Instantly share code, notes, and snippets.

@sedran
Created March 25, 2020 20:13
Show Gist options
  • Select an option

  • Save sedran/2683d9045af6118f322044e516a9ce53 to your computer and use it in GitHub Desktop.

Select an option

Save sedran/2683d9045af6118f322044e516a9ce53 to your computer and use it in GitHub Desktop.
/**
* Yazar: Serdar KUZUCU
* http://serdarkuzucu.com/
*/
package sedran.borders;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Pencere extends JFrame {
private static final long serialVersionUID = 1L;
private TextBorder border;
private Font f1 = new Font("Comic Sans MS", Font.PLAIN, 11);
private Font f2;
public static void main(String args[]) {
new Pencere();
}
public Pencere() {
super("Border(Kenarlık) Çizimi");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(3, 2));
border = new TextBorder("Asosyal");
f2 = border.getFont();
panel.setBorder(border);
JButton button1 = new JButton("Yuvarlak");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
border.setRounded(true);
repaint();
}
});
panel.add(button1);
JButton button3 = new JButton("Köşeli");
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
border.setRounded(false);
repaint();
}
});
panel.add(button3);
JButton button2 = new JButton("Mavi");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
border.setBorderColor(Color.BLUE);
repaint();
}
});
panel.add(button2);
JButton button4 = new JButton("Kırmızı");
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
border.setBorderColor(Color.RED);
repaint();
}
});
panel.add(button4);
JButton button5 = new JButton("Yazıyı Değiş");
button5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(border.getText().equals("Asosyal"))
border.setText("Bebe");
else
border.setText("Asosyal");
repaint();
}
});
panel.add(button5);
JButton button6 = new JButton("Font");
button6.setFont(f1);
button6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if( border.getFont() == f1 )
border.setFont(f2);
else
border.setFont(f1);
repaint();
}
});
panel.add(button6);
setContentPane(panel);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}
/**
* Yazar: Serdar KUZUCU
* http://serdarkuzucu.com/
*/
package sedran.borders;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.geom.Rectangle2D;
import javax.swing.border.Border;
public class TextBorder implements Border {
private String title = "";
private Font f = new Font("Tahoma", Font.PLAIN, 11);
private Insets insets = new Insets(23, 9, 9, 9);
private Color line_color = new Color(148,145,140);
private Color text_color = new Color(20,20,20);
private boolean rounded = false;
public TextBorder(String title) {
this.title = title;
}
public void setText(String text) {
this.title = text;
}
public void setBorderColor(Color c) {
line_color = c;
}
public void setTextColor(Color c) {
text_color = c;
}
public void setRounded(boolean round) {
rounded = round;
}
public void setFont(Font f) {
this.f = f;
}
public Font getFont() {
return this.f;
}
public String getText() {
return title;
}
public Insets getBorderInsets(Component c) {
return new Insets(insets.top, insets.left, insets.bottom, insets.right);
}
public boolean isBorderOpaque() {
return true;
}
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
int roundWidth = rounded ? 15 : 0;
g.setColor(line_color);
g.drawRoundRect(x+3, y+10, width-6, height-13, roundWidth, roundWidth);
g.setColor(Color.WHITE);
g.drawRoundRect(x+4, y+11, width-8, height-15, roundWidth, roundWidth);
g.setFont(f);
FontMetrics fm = g.getFontMetrics(f);
Rectangle2D textsize = fm.getStringBounds(title, g);
g.setColor(c.getBackground());
g.fillRect(x+12, y+9, (int)textsize.getWidth()+6, 4);
g.setColor(text_color);
g.drawString(title, x+15, y+fm.getAscent()+3);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment