java graphics2d 矩形_根据java Graphics2D drawString 描绘出的字符串得到字符串所围成成的矩形...

展开全部

dfc309a22411e7747893b8b84546b0a9.pngimport javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

final class StringPaintDemo extends JFrame {

final static private Dimension SIZE = new Dimension(800, 600);

public String getTitle() {return "StringPaintDemo";}

public Dimension getPreferredSize() {return SIZE;}

public Dimension getMinimumSize() {return SIZE;}

public Dimension getMaximumSize() {return SIZE;}

public Dimension getSize() {return SIZE;}

private Canvas canvas;private JCheckBox checkBox;

private Brush brush;

StringPaintDemo() throws HeadlessException {

init();

attachListeners();

doLay();

setVisible(true);

}

private void init() {

brush=new StringBoundBrush();

canvas=new Canvas(brush);

checkBox=new JCheckBox("是否绘制文本62616964757a686964616fe4b893e5b19e31333337383964边框?");

checkBox.addChangeListener(evt->{

brush.update(null, checkBox.isSelected());

});

}

private void attachListeners() {

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(1);

}

});

}

private void doLay() {

Container container=getContentPane();

container.add(canvas,BorderLayout.CENTER);

container.add(checkBox,BorderLayout.NORTH);

pack();

}

public static void main(String... args) {

System.setProperty("swing.defaultlaf", "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

SwingUtilities.invokeLater(StringPaintDemo::new);

}

private interface Brush extends Observer {

/**

* do paint action

* @param g the graphics context in you application

*/

void paint(Graphics g);

/**

* The component that this brush hold

* @return component instance

*/

Component getOwner();

void setOwner(Component owner);

}

private static final class StringBoundBrush implements Brush{

private Component owner;

private Object contentObj;

private boolean isBorderPainted=false;

public Component getOwner() {

return owner;

}

public void setOwner(Component owner) {

this.owner=owner;

}

public void paint(Graphics g) {

Graphics2D g2= (Graphics2D)g.create();

g2.fill(getOwner().getBounds());

g2.translate(100, 100);

g2.rotate(Math.PI / 3);

g2.setColor(Color.BLUE);

g2.setFont(g2.getFont().deriveFont(33f));

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

String s = contentObj ==null?"Null?": contentObj.toString();

if (isBorderPainted) {

g2.setColor(Color.ORANGE);

g2.draw(g2.getFontMetrics().getStringBounds(s, g2));

}

g2.setColor(Color.RED);

g2.drawString(s,0,0);

g2.dispose();

}

public void update(Observable o, Object arg) {

if(arg instanceof Boolean)

isBorderPainted= (boolean)arg;

else

contentObj = arg;

if (owner!=null) owner.repaint();

}

}

/**

* you canvas

*/

private class Canvas extends JComponent{

private Brush brush;

private Canvas(Brush brush) {

super();

this.brush=brush;

brush.setOwner(this);

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

if (brush == null)return;

brush.paint(g);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值