gpt4 book ai didi

java - 在 Swing 中画漂亮的泡泡

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:25:53 26 4
gpt4 key购买 nike

<分区>

我正在尝试在 swing 中创建漂亮的对话泡泡,但结果不是很好......我的意思是我想要更好更漂亮的东西!

enter image description here

enter image description here

这是我使用的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Area;
import java.awt.geom.RoundRectangle2D;

public class BubbleTest {

public static void main(String[] args) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
LeftArrowBubble leftArrowBubble = new LeftArrowBubble();
JOptionPane.showMessageDialog(null, leftArrowBubble);
RightArrowBubble rightArrowBubble = new RightArrowBubble();
JOptionPane.showMessageDialog(null, rightArrowBubble);
}

private static class LeftArrowBubble extends JPanel {

private int strokeThickness = 5;
private int padding = strokeThickness / 2;
private int radius = 10;
private int arrowSize = 6;

@Override
protected void paintComponent(final Graphics g) {
final Graphics2D graphics2D = (Graphics2D) g;
RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
qualityHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics2D.setRenderingHints(qualityHints);
graphics2D.setColor(new Color(80, 150, 180));
graphics2D.setStroke(new BasicStroke(strokeThickness));
int x = padding + strokeThickness + arrowSize;
int width = getWidth() - arrowSize - (strokeThickness * 2);
int height = getHeight() - strokeThickness;
graphics2D.fillRect(x, padding, width, height);
RoundRectangle2D.Double rect = new RoundRectangle2D.Double(x, padding, width, height, radius, radius);
Polygon arrow = new Polygon();
arrow.addPoint(14, 6);
arrow.addPoint(arrowSize + 2, 10);
arrow.addPoint(14, 12);
Area area = new Area(rect);
area.add(new Area(arrow));
graphics2D.draw(area);
graphics2D.dispose();
}

}

private static class RightArrowBubble extends JPanel {

private int strokeThickness = 5;
private int padding = strokeThickness / 2;
private int arrowSize = 6;
private int radius = 10;

@Override
protected void paintComponent(final Graphics g) {
final Graphics2D graphics2D = (Graphics2D) g;
RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
qualityHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics2D.setRenderingHints(qualityHints);
graphics2D.setColor(new Color(20, 130, 230));
graphics2D.setStroke(new BasicStroke(strokeThickness));
int width = getWidth() - arrowSize - (strokeThickness * 2);
int height = getHeight() - strokeThickness;
graphics2D.fillRect(padding, padding, width, height);
RoundRectangle2D.Double rect = new RoundRectangle2D.Double(padding, padding, width, height, radius, radius);
Polygon arrow = new Polygon();
arrow.addPoint(width, 6);
arrow.addPoint(width + arrowSize, 10);
arrow.addPoint(width, 12);
Area area = new Area(rect);
area.add(new Area(arrow));
graphics2D.draw(area);
graphics2D.dispose();
}

}

}

有什么想法吗?

我想要在 viber(android 版本 5.0.0)中像对话泡泡一样的东西,但我不知道如何画那样的东西......谁能帮帮我?

enter image description here

谢谢!

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com