-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathCSignLabel.java
More file actions
97 lines (92 loc) · 3.23 KB
/
Copy pathCSignLabel.java
File metadata and controls
97 lines (92 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the MIT License https://raw.github.com/mit-cml/app-inventor/master/mitlicense.txt
package openblocks.codeblockutil;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* it's like a neon sign, only cooler
*
*/
public class CSignLabel extends JPanel{
private static final long serialVersionUID = 328149080428L;
//To get the shadow effect the text must be displayed multiple times at
//multiple locations. x represents the center, white label.
// o is color values (0,0,0,0.5f) and Y is black.
// o o
// o x Y o
// o Y o
// o
//offsetArrays representing the translation movement needed to get from
// the center location to a specific offset location given in {{x,y},{x,y}....}
//..........................................grey points.............................................black points
private final int[][] shadowPositionArray = {{0,-1},{1,-1}, {-1,0}, {2,0}, {-1,1}, {1,1}, {0,2}, {1,0}, {0,1}};
private final float[] shadowColorArray = {0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0, 0};
private double offsetSize = 1;
private String[] charSet;
private int FONT_SIZE = 12;
public CSignLabel(){
super();
this.charSet = new String[0];
this.setOpaque(false);
this.setFont(new Font("Ariel", Font.BOLD, FONT_SIZE));
}
public void setText(String text){
if(text != null){
text = text.toUpperCase();
List<String> characters = new ArrayList<String>();
for (int i = 0; i<text.length(); i++){
characters.add(text.substring(i, i+1));
}
charSet = characters.toArray(charSet);
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
for (int j = 0; j<charSet.length; j++){
/*String c = charSet[i];
Font font = g2.getFont().deriveFont((float)(((float)this.getHeight()) * .8));
g2.setFont(font);
FontMetrics metrics = g2.getFontMetrics();
Rectangle2D textBounds = metrics.getStringBounds(c,g2);
double textHeight = textBounds.getHeight();
double textWidth = textBounds.getWidth() > this.getWidth() ? this.getWidth()/2 : textBounds.getWidth();
*/
String c = charSet[j];
System.out.println(c);
int x = 5;
int y = (j+1) * (FONT_SIZE+3);
g.setColor(Color.black);
for (int i = 0; i < shadowPositionArray.length; i++) {
int dx = shadowPositionArray[i][0];
int dy = shadowPositionArray[i][1];
g2.setColor(new Color(0,0,0, shadowColorArray[i]));
g2.drawString(c, x+(int)((dx)*offsetSize), y+(int)((dy)*offsetSize));
}
g2.setColor(Color.white);
g2.drawString(c, x, y);
}
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new BorderLayout());
f.setSize(500, 75);
CSignLabel c = new CSignLabel();
c.setText("hello there");
c.setPreferredSize(new Dimension(400,50));
f.add(c);
f.setVisible(true);
f.repaint();
}
}