在Swing中绘制美丽的演讲泡沫 [英] Draw Beautiful Speech Bubbles in Swing

查看:177
本文介绍了在Swing中绘制美丽的演讲泡沫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在秋千上创造漂亮的气泡,但结果不是很好......我的意思是我想要更好,更漂亮的东西! $ b





>

  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() );
休息;

} 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);
面积面积=新面积(rect);
area.add(新区域(箭头));
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);
面积面积=新面积(rect);
area.add(新区域(箭头));
graphics2D.draw(area);
graphics2D.dispose();
}

}

}



<任何想法的家伙?



我想要viber中的语音泡泡(android version 5.0.0),但我不知道如何绘制类似的东西...
can任何人都可以帮助我?





谢谢!

解决方案

c> GeneralPath ...





足够接近? )



保护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(宽度,高度 - 10,宽度,高度,宽度 - 10,高度);
path.lineTo(15,height);
path.curveTo(15,height,5,height,5,height - 10);
path.lineTo(5,15);
path.closePath();
graphics2D.fill(path);
}


I am trying to create beautiful speech bubbles in swing but the result is not very good...I mean I want something better and more beautiful!

here is the code i'm using:

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();
        }

    }

}

any idea guys?

I want something like speech bubbles in viber (android version 5.0.0) but I don't know how to draw something like that... can anyone help me?

Thanks!

解决方案

finally I draw it with GeneralPath...

close enough? :))

@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);
}

这篇关于在Swing中绘制美丽的演讲泡沫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆