0
我目前正在設計遊戲的標題屏幕。在設計的標誌,我決定與彩虹邊框去周圍的邊緣:Java Swing:JComponent的彩虹邊框
我在此之後想通我想實現我的部件的標題畫面上休息了類似彩虹的邊界(具體Jbutton將)。在尋找方法時,我遇到了AbstractBorder
課。我的問題是,這甚至有可能做到這一點,如果是這樣,根據組件大小使用Abstract Border類生成彩虹邊框的最有效方法是什麼?
我目前正在設計遊戲的標題屏幕。在設計的標誌,我決定與彩虹邊框去周圍的邊緣:Java Swing:JComponent的彩虹邊框
我在此之後想通我想實現我的部件的標題畫面上休息了類似彩虹的邊界(具體Jbutton將)。在尋找方法時,我遇到了AbstractBorder
課。我的問題是,這甚至有可能做到這一點,如果是這樣,根據組件大小使用Abstract Border類生成彩虹邊框的最有效方法是什麼?
的基本方法是使用LinearGradientPaint
畫的彩虹效果,例如...
public class RainbowBorder extends AbstractBorder {
@Override
public Insets getBorderInsets(Component c, Insets insets) {
insets.bottom = insets.top = insets.left = insets.right = 1;
return insets;
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D g2d = (Graphics2D) g.create();
LinearGradientPaint lpg = new LinearGradientPaint(
new Point(x, y),
new Point(x, y + height),
new float[]{0.0f, 0.25f, 0.5f, 0.75f, 1.0f},
new Color[]{Color.YELLOW, Color.GREEN, Color.CYAN, Color.BLUE, Color.MAGENTA});
g2d.setPaint(lpg);
g2d.draw(new Rectangle2D.Double(x, y, width - 1, height - 1));
g2d.dispose();
}
}
而作爲一個概念證明
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.LinearGradientPaint;
import java.awt.Point;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.AbstractBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
public class TextOutline {
public static void main(String[] args) {
new TextOutline();
}
public TextOutline() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class RainbowBorder extends AbstractBorder {
@Override
public Insets getBorderInsets(Component c) {
return super.getBorderInsets(c); //To change body of generated methods, choose Tools | Templates.
}
@Override
public Insets getBorderInsets(Component c, Insets insets) {
insets.bottom = insets.top = insets.left = insets.right = 1;
return insets;
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D g2d = (Graphics2D) g.create();
LinearGradientPaint lpg = new LinearGradientPaint(
new Point(x, y),
new Point(x, y + height),
new float[]{0.0f, 0.25f, 0.5f, 0.75f, 1.0f},
new Color[]{Color.YELLOW, Color.GREEN, Color.CYAN, Color.BLUE, Color.MAGENTA});
g2d.setPaint(lpg);
g2d.draw(new Rectangle2D.Double(x, y, width - 1, height - 1));
g2d.dispose();
}
}
class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout());
setBorder(
new CompoundBorder(
new EmptyBorder(10, 10, 10, 10),
new CompoundBorder(
new RainbowBorder(),
new EmptyBorder(10, 10, 10, 10))
));
add(new JLabel("Rainbow and unicorns"));
}
}
}
難以置信的例子,代碼,非常感謝您的幫助! –