gpt4 book ai didi

java - 带有背景图像的 JTextArea 的内部填充

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:42:59 27 4
gpt4 key购买 nike

我的最终目标是拥有一个带有背景图像的 JTextArea。我在网上找到了向我展示如何执行此操作的代码,但现在我遇到了文本位于图像顶部的问题。

这就是我的意思:

Text overlapping image

有什么方法可以添加一种向内缩进,使文本不与图像边缘重叠?


这是原始评论气泡图像。

Comment bubble


代码如下:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.GrayFilter;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class myBackgroundSample {

String file;

public myBackgroundSample(String i) {
file = i;
setItUp();
}

public void setItUp() {
final ImageIcon imageIcon = new ImageIcon(file);
JTextArea textArea = new JTextArea() {
Image image = imageIcon.getImage();

public void paint(Graphics g) {
setOpaque(false);
g.drawImage(image, 0, 0, this);
super.paint(g);
}
};
JFrame frame = new JFrame("Background Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane(textArea);
Container content = frame.getContentPane();
content.add(scrollPane, BorderLayout.CENTER);
frame.setSize(400, 400);
frame.setVisible(true);
}

public static void main(String[] args) {
// TODO Auto-generated method stub
String right = "chat1.jpg";
myBackgroundSample temp = new myBackgroundSample(right);

}
}

最佳答案

使用扩展 AbstractBorder 的自定义边框.像这样:

Text Bubble Border

获得准确的形状和颜色作为练习留给读者。 :)

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.border.AbstractBorder;

class TextBubbleBorder extends AbstractBorder {

private Color color;
private int thickness = 4;
private int radii = 8;
private int pointerSize = 7;
private Insets insets = null;
private BasicStroke stroke = null;
private int strokePad;
private int pointerPad = 4;
RenderingHints hints;

TextBubbleBorder(
Color color) {
new TextBubbleBorder(color, 4, 8, 7);
}

TextBubbleBorder(
Color color, int thickness, int radii, int pointerSize) {
this.thickness = thickness;
this.radii = radii;
this.pointerSize = pointerSize;
this.color = color;

stroke = new BasicStroke(thickness);
strokePad = thickness/2;

hints = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

int pad = radii + strokePad;
int bottomPad = pad + pointerSize + strokePad;
insets = new Insets(pad,pad,bottomPad,pad);
}

@Override
public Insets getBorderInsets(Component c) {
return insets;
}

@Override
public Insets getBorderInsets(Component c, Insets insets) {
return getBorderInsets(c);
}

@Override
public void paintBorder(
Component c,
Graphics g,
int x, int y,
int width, int height) {

Graphics2D g2 = (Graphics2D)g;

int bottomLineY = height-thickness-pointerSize;

RoundRectangle2D.Double bubble = new RoundRectangle2D.Double(
0+strokePad,
0+strokePad,
width-thickness,
bottomLineY,
radii,
radii
);

Polygon pointer = new Polygon();

// left point
pointer.addPoint(
strokePad+radii+pointerPad,
bottomLineY);
// right point
pointer.addPoint(
strokePad+radii+pointerPad+pointerSize,
bottomLineY);
// bottom point
pointer.addPoint(
strokePad+radii+pointerPad+(pointerSize/2),
height-strokePad);

Area area = new Area(bubble);
area.add(new Area(pointer));

g2.setRenderingHints(hints);

Area spareSpace = new Area(new Rectangle(0,0,width,height));
spareSpace.subtract(area);
g2.setClip(spareSpace);
g2.clearRect(0,0,width,height);
g2.setClip(null);

g2.setColor(color);
g2.setStroke(stroke);
g2.draw(area);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JLabel l = new JLabel(
"The quick brown fox jumped over the lazy dog!");

l.setBorder(new TextBubbleBorder(Color.MAGENTA.darker(),2,4,0));
l.setOpaque(true);
l.setBackground(Color.BLACK);
JOptionPane.showMessageDialog(null, l);
}
});
}
}

关于java - 带有背景图像的 JTextArea 的内部填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8462414/

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