该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
JBubble的定义,是否和我没有重写repaint有关?那该怎么改?
public class JBubble extends JComponent {
private String msg;
private BufferedImage image;
private JComponent jComponent;
private int width = 0;
private boolean flag;
private List stringList;
public int height = 0;
public JBubble(String msg, JComponent jComponent) {
try {
this.image = ImageIO.read(new File("src"+File.separator+"image"+File.separator+"img_yourself.png"));
} catch (IOException e) {
e.printStackTrace();
}
this.jComponent = jComponent;
this.msg = msg;
this.flag = true;
this.stringList = new ArrayList<>();
cutString(msg);
height = (int)(stringList.size()*getPoint(msg).getY());
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(e.getClickCount() == 2) {
}
}
});
}
public JBubble(String msg, String ip, JComponent jComponent) {
this(msg, jComponent);
this.flag = false;
try {
this.image = ImageIO.read(new File("src"+File.separator+"image"+File.separator+"img_"+ip+".png"));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D graphics2D = (Graphics2D) g;
if(!flag) {
graphics2D.drawImage(image, 0, 0, 50, 50, this);
graphics2D.setColor(Color.LIGHT_GRAY);
int[] xs = {60, 75, 75, 60};
int[] ys = {25, 20, 30, 25};
graphics2D.fillPolygon(xs, ys, 3);
graphics2D.setColor(Color.lightGray);
graphics2D.fillRoundRect(75, 10, width + 5, height+5, 15, 15 );
graphics2D.setColor(Color.DARK_GRAY);
height = 0;
Iterator iterator = stringList.iterator();
while(iterator.hasNext()) {
height += getPoint(msg).getY();
graphics2D.drawString((String) iterator.next(), 80, height + 10);
}
} else {
graphics2D.drawImage(image, jComponent.getWidth() - 50, 0, 50, 50, this);
graphics2D.setColor(Color.LIGHT_GRAY);
int[] xs = {jComponent.getWidth() - 60, jComponent.getWidth() - 75, jComponent.getWidth() - 75, jComponent.getWidth() - 60};
int[] ys = {25, 20, 30, 25};
graphics2D.fillPolygon(xs, ys, 3);
graphics2D.setColor(Color.lightGray);
int height = (int)(stringList.size()*getPoint(msg).getY());
graphics2D.fillRoundRect(jComponent.getWidth() - 75 - width, 10, width + 5, height+5, 15, 15 );
graphics2D.setColor(Color.DARK_GRAY);
height = 0;
Iterator iterator = stringList.iterator();
while(iterator.hasNext()) {
height += getPoint(msg).getY();
graphics2D.drawString((String) iterator.next(), jComponent.getWidth() - 70 - width, height + 10);
}
}
super.paintComponent(g);
}
@Override
public boolean contains(int x, int y) {
if(!flag) {
return (x - 25) * (x - 25) + (y - 25) * (y - 25) < 625;
}
return false;
}
public void cutString(String msg) {
if(getPoint(msg).getX() + 160 < jComponent.getWidth()) {
stringList.add(msg);
if(width == 0) {
width = (int) getPoint(msg).getX();
}
} else {
width = jComponent.getWidth() - 160;
String temp = "";
for(int i = 0; i < msg.length(); i++) {
if(getPoint(temp + msg.charAt(i)).getX() + 160 > jComponent.getWidth()) {
stringList.add(temp);
msg = msg.substring(i, msg.length());
cutString(msg);
break;
}
temp += msg.charAt(i);
}
}
}
}