JAVA Swing聊天气泡

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

			// Define rendering hint, font name, font style and font size
			graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			graphics2D.setFont(new Font("宋体", Font.BOLD + Font.ITALIC, 40));
			graphics2D.setPaint(Color.ORANGE);

			// Draw Hello World String
			graphics2D.drawString("测试!", 15, 38);
			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);
			// Define rendering hint, font name, font style and font size
			graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			graphics2D.setFont(new Font("宋体", Font.BOLD + Font.ITALIC, 40));
			graphics2D.setPaint(Color.ORANGE);

			// Draw Hello World String
			graphics2D.drawString("测试!", 15, 38);
			graphics2D.dispose();
		}

	}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值