gpt4 book ai didi

java - 在 Swing 中绘制美丽的语音气泡

转载 作者:太空宇宙 更新时间:2023-11-04 11:58:48 24 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

谢谢!

最佳答案

最后我用GeneralPath绘制它...

enter image description here

足够接近吗? :))

@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.setPaint(new Color(80, 150, 180));
int width = getWidth();
int height = getHeight();
GeneralPath path = new GeneralPath();
path.moveTo(5, 10);
path.curveTo(5, 10, 7, 5, 0, 0);
path.curveTo(0, 0, 12, 0, 12, 5);
path.curveTo(12, 5, 12, 0, 20, 0);
path.lineTo(width - 10, 0);
path.curveTo(width - 10, 0, width, 0, width, 10);
path.lineTo(width, height - 10);
path.curveTo(width, height - 10, width, height, width - 10, height);
path.lineTo(15, height);
path.curveTo(15, height, 5, height, 5, height - 10);
path.lineTo(5, 15);
path.closePath();
graphics2D.fill(path);
}

关于java - 在 Swing 中绘制美丽的语音气泡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41116154/

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