我正在尝试实现动态边框,方法是从中心开始直到它散布到底部,当鼠标进入时在组件的顶部和底部绘制两条线(确切地说是四条)。边缘。
我尝试实施java.awt.Border
,但没有任何提示!
我的代码:
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import javax.swing.border.Border;
public class BorderEffect implements Border {
public BorderEffect(Color c) {
col = c;
}
private Color col;
private Graphics g;
Component c;
private Color fade (Color base)
{
return new Color (base.getRed(),base.getGreen(),base.getBlue(),70);
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
this.g = g;
this.c = c;
}
public void go () throws InterruptedException
{
Dimension size = c.getSize();
for (int i = 0; i < size.getWidth(); i++) {
System.out.println("start");
Thread.sleep(0, 1);
GradientPaint upLeft = new GradientPaint((float) (size.getWidth()/2), 5, col, (float)((size.getWidth()/2)+i), 5, fade(col),false);
GradientPaint downLeft = new GradientPaint((float) (size.getWidth()/2), (int)size.getHeight(), col, (float)((size.getWidth()/2)+i), (int)size.getHeight(), fade(col),false);
GradientPaint upRigth = new GradientPaint((float) (size.getWidth()/2)-i, 0, fade(col), (float)((size.getWidth()/2)), 0, col,false);
GradientPaint downRigth = new GradientPaint((float) (size.getWidth()/2)-i, (int)size.getHeight(), fade(col), (float)((size.getWidth()/2)), (int)size.getHeight(), col,false);
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(upLeft);
g2.fillRect((int)(size.getWidth()/2), 0, i, 3);
g2.setPaint(upRigth);
g2.fillRect((int)(size.getWidth()/2), 0, i, 3);
g2.setPaint(downLeft);
g2.fillRect((int)(size.getWidth()/2)-i, (int)size.getHeight()-5, i, 3);
g2.setPaint(downRigth);
g2.fillRect((int)(size.getWidth()/2)-i, (int)size.getHeight()-5, i, 3);
c.repaint();
}
}
@Override
public Insets getBorderInsets(Component c) {
return new Insets(3, 0, 3, 0);
}
@Override
public boolean isBorderOpaque() {
return true;
}
}
我确实将边框添加到JPanel
中,并在go
中的mouseEntered
中调用了MouseAdapter
方法。但是什么都没有出现,我什至不知道缺陷在哪里。
我想知道的是:-
答案 0 :(得分:2)
这是我的意思的一个示例-使用Swing计时器为边框设置动画。请注意,出于安全考虑,我扩展了AbstractBorder,以防此类中包含任何内务处理代码。计时器递增索引i,然后调用重绘,然后paintBorder
方法使用i
决定要绘制的内容:
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
@SuppressWarnings("serial")
public class BorderTest extends JPanel {
private JPanel testPanel = new JPanel();
public BorderTest() {
testPanel.setPreferredSize(new Dimension(400, 300));
testPanel.setBorder(new BorderEffect2(testPanel, Color.BLUE));
testPanel.setBackground(Color.WHITE);
setPreferredSize(new Dimension(500, 400));
setLayout(new GridBagLayout());
add(testPanel);
}
private static void createAndShowGui() {
BorderTest mainPanel = new BorderTest();
JFrame frame = new JFrame("BorderTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
@SuppressWarnings("serial")
class BorderEffect2 extends AbstractBorder implements Border {
public static final int TIMER_DELAY = 10;
private int i = 0;
private JPanel testPanel;
private Color color;
private Timer timer;
public BorderEffect2(JPanel testPanel, Color color) {
this.testPanel = testPanel;
this.color = color;
testPanel.addMouseListener(new MouseAdapt());
}
private class MouseAdapt extends MouseAdapter {
@Override
public void mouseEntered(MouseEvent e) {
if (timer != null && timer.isRunning()) {
return;
}
// System.out.println("here");
timer = new Timer(TIMER_DELAY, new TimerListener());
timer.start();
}
}
private class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
i++;
if (i >= testPanel.getWidth()) {
((Timer) timer).stop();
i = 0;
}
testPanel.repaint();
}
}
private Color fade(Color base) {
return new Color(base.getRed(), base.getGreen(), base.getBlue(), 70);
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
super.paintBorder(c, g, x, y, width, height);
Dimension size = c.getSize();
GradientPaint upLeft = new GradientPaint((float) (size.getWidth() / 2), 5, color,
(float) ((size.getWidth() / 2) + i), 5, fade(color), false);
GradientPaint downLeft = new GradientPaint((float) (size.getWidth() / 2),
(int) size.getHeight(), color, (float) ((size.getWidth() / 2) + i),
(int) size.getHeight(), fade(color), false);
GradientPaint upRigth = new GradientPaint((float) (size.getWidth() / 2) - i, 0,
fade(color), (float) ((size.getWidth() / 2)), 0, color, false);
GradientPaint downRigth = new GradientPaint((float) (size.getWidth() / 2) - i,
(int) size.getHeight(), fade(color), (float) ((size.getWidth() / 2)),
(int) size.getHeight(), color, false);
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(upLeft);
g2.fillRect((int) (size.getWidth() / 2), 0, i, 3);
g2.setPaint(upRigth);
g2.fillRect((int) (size.getWidth() / 2), 0, i, 3);
g2.setPaint(downLeft);
g2.fillRect((int) (size.getWidth() / 2) - i, (int) size.getHeight() - 5, i, 3);
g2.setPaint(downRigth);
g2.fillRect((int) (size.getWidth() / 2) - i, (int) size.getHeight() - 5, i, 3);
}
@Override
public Insets getBorderInsets(Component c) {
// return super.getBorderInsets(c);
return new Insets(3, 0, 3, 0);
}
@Override
public boolean isBorderOpaque() {
return true;
}
}