📄 clipshapefrm.java
字号:
package clipshape;import java.awt.*;import java.awt.event.*;import java.awt.font.*;import java.awt.geom.*;import java.util.*;import javax.swing.*;/** * <p>Title: ClipShape</p> * <p>Description: 应用Java 2D API剪切文字图形</p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: 北京师范大学计算机系</p> * @author 张良博 * @version 1.0 */class ClipShapeFrm extends JFrame{ private JPanel panel; private Shape clipShape; private static final int WIDTH = 300; private static final int HEIGHT = 300; public ClipShapeFrm() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { //setIconImage(Toolkit.getDefaultToolkit().createImage(TransformFrm.class.getResource("[Your Icon]"))); setTitle("应用Java 2D API实现剪切文字图形"); setSize(WIDTH, HEIGHT); Container contentPane = getContentPane(); final JCheckBox checkBox = new JCheckBox("Clip"); checkBox.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ panel.repaint(); } }); contentPane.add(checkBox, BorderLayout.NORTH); panel = new JPanel() { public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; if (clipShape == null) clipShape = makeClipShape(g2); g2.draw(clipShape); if (checkBox.isSelected()) g2.clip(clipShape); final int NLINES =50; Point2D p = new Point2D.Double(0, 0); for (int i = 0; i < NLINES; i++) { double x = (2 * getWidth() * i) / NLINES; double y = (2 * getHeight() * (NLINES - 1 - i)) / NLINES; Point2D q = new Point2D.Double(x, y); g2.draw(new Line2D.Double(p, q));//设置剪切的形状,并且绘制一组线条。 } } }; contentPane.add(panel, BorderLayout.CENTER); } Shape makeClipShape(Graphics2D g2) { FontRenderContext context = g2.getFontRenderContext();//取得一个字体绘制环境 Font f = new Font("Serif", Font.PLAIN, 100); GeneralPath clipShape = new GeneralPath();//将字体的外形附加给剪切的形状 TextLayout layout = new TextLayout("Hello", f, context);//建立一个TextLayout对象 AffineTransform transform = AffineTransform.getTranslateInstance(0, 100);//将基点移到点(0,100)的变换操作 Shape outline = layout.getOutline(transform); clipShape.append(outline, false); layout = new TextLayout("Java", f, context); transform = AffineTransform.getTranslateInstance(0, 200); outline = layout.getOutline(transform); clipShape.append(outline, false); return clipShape; } protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -