把中文词限定在一个特定的形状中(中文词云) 80C

用给出的Textboard编写,Textboard可以将一个中文词显示在Frame上,并且可改变其坐标、大小、字体、颜色。
要求利用已经分好的词以及其频率(这个请自行假设)限定这些词在一个特定形状中如:圆、矩形、调用的图片形状。
并且重点必须突出(对比最好鲜明)
Textboard以及其测试代码TestTextboard如下
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Font;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;

class Scribbling {
String what;
int xpos;
int ypos;
double angle;
Font font;
Color col;
}

class WritePanel extends JPanel {
private Graphics2D g;
private ArrayList text = new ArrayList();

private void doWriting(Graphics g) {
    Graphics2D      g2d = (Graphics2D) g;
    Font            font = getFont();
    Color           col = g.getColor();
    double          angle = 0;
    AffineTransform orig = g2d.getTransform();

    for (Scribbling s: text) {
      if (s.angle != angle) {
        g2d.setTransform(orig);
        g2d.rotate(s.angle * Math.PI / 180.0);
        angle = s.angle;
      }
      if (s.font != null && !s.font.equals(font)) {
        g2d.setFont(s.font);
        font = g2d.getFont();
      }
      if (!s.col.equals(col)) {
        col = new Color(s.col.getRGB());
        g2d.setColor(col);
      }
      g2d.drawString(s.what, s.xpos, s.ypos);
    }
}

public void addText(String what, int xpos, int ypos,
                    double angle, Font font, Color col) {
   Scribbling s = new Scribbling();
   s.what = what;
   s.xpos = xpos;
   s.ypos = ypos;
   s.angle = angle;
   s.font = font;
   s.col = col;
   text.add(s);
}

@Override
public void paintComponent(Graphics g) {

    super.paintComponent(g);
    doWriting(g);
}

}

/**

  • Textboard is in fact a full graphical application that displays
  • text in a graphical Window
    */
    public class TextBoard extends JFrame {
    private WritePanel board;
    private double currentAngle;
    private Color currentColor;
    private Font currentFont;

    /**

    • Only constructor. *
    • @param width Width of the window in pixels
    • @param height Height of the window in pixels
    • @param col Background color
      */
      public TextBoard(int width, int height, Color col) {

      board = new WritePanel();
      // Put the WritePanel into the main window
      add(board);
      // Set its background color
      board.setOpaque(true);
      board.setBackground(col);
      // Initial settings
      currentAngle = 0;
      currentColor = Color.BLACK;
      currentFont = getFont();
      // Geometry of the main window
      setSize(width, height);
      setLocationRelativeTo(null);
      // When users close the window, we quit the application
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(false);
      }

    // Overloaded methods to write text
    /**

    • Write text to the screen
    • The text can be written at any position, it can be
    • rotated, the font and color can be changed. Every piece
    • of text is added to a list, and only written when the
    • window is drawn. *
    • @param what The text to write
    • @param xpos Horizontal position in pixels, from the left hand side
    • @param ypos Vertical position in pixels, from the top
    • @param angle Rotation angle in degrees
    • @param font Font to use to write the text
    • @param col Text color
      */
      public void write(String what, int xpos, int ypos,
      double angle, Font font, Color col) {

      if (currentAngle != angle) {
      currentAngle = angle;
      }
      if (currentFont != null && !currentFont.equals(font)) {
      currentFont = font;
      }
      if (!currentColor.equals(col)) {
      currentColor = new Color(col.getRGB());
      }
      board.addText(what, xpos, ypos, angle, font, col);
      }

    /**

    • Write text to the screen
    • Use the last set angle (0 by default), font and color
    • (black by default). *
    • @param what The text to write
    • @param xpos Horizontal position in pixels, from the left hand side
    • @param ypos Vertical position in pixels, from the top */ public void write(String what, int xpos, int ypos) { write(what, xpos, ypos, currentAngle, currentFont, currentColor); }

    /**

    • Write text to the screen
    • Use the last set font and color (black by default), set the angle. *
    • @param what The text to write
    • @param xpos Horizontal position in pixels, from the left hand side
    • @param ypos Vertical position in pixels, from the top
    • @param angle Rotation angle in degrees */ public void write(String what, int xpos, int ypos, double angle) { write(what, xpos, ypos, angle, currentFont, currentColor); }

    /**

    • Write text to the screen
    • Use the last set font, set angle and color. *
    • @param what The text to write
    • @param xpos Horizontal position in pixels, from the left hand side
    • @param ypos Vertical position in pixels, from the top
    • @param angle Rotation angle in degrees
    • @param col Text color */ public void write(String what, int xpos, int ypos, double angle, Color col) { write(what, xpos, ypos, angle, currentFont, col); }

    /**

    • Write text to the screen
    • Use the last set angle(0 by default) and font, set the color. *
    • @param what The text to write
    • @param xpos Horizontal position in pixels, from the left hand side
    • @param ypos Vertical position in pixels, from the top
    • @param col Text color */ public void write(String what, int xpos, int ypos, Color col) { write(what, xpos, ypos, currentAngle, currentFont, col); }

    /**

    • Write text to the screen
    • Use the last set angle (0 by default), set font and color. *
    • @param what The text to write
    • @param xpos Horizontal position in pixels, from the left hand side
    • @param ypos Vertical position in pixels, from the top
    • @param font Font to use to write the text
    • @param col Text color */ public void write(String what, int xpos, int ypos, Font font, Color col) { write(what, xpos, ypos, currentAngle, font, col); }

    /**

    • Show the window */ public void display() { this.setVisible(true); } }

package com.kennycason.CourseProject;
import java.awt.Font;

import java.awt.Color;

public class TestTextBoard {

public static void main(String[] args) {
    Font font;

    TextBoard tb = new TextBoard(1000, 700, Color.WHITE);
    System.out.println("-- Initialized");
    font = new Font("SansSerif", Font.BOLD, 60);
    tb.write("Hello", 160, 500, font, Color.YELLOW);
    font = new Font("SansSerif", Font.BOLD, 150);
    tb.write("chinese", 150, 300, 10, font, new Color(250, 230, 241));
    tb.write("Tally ho", 500, 500);
    font = new Font("SansSerif", Font.BOLD, 80);
    tb.write("Awesome!", 60, 200,90, font, Color.BLACK);
    System.out.println("-- Written");
    tb.display();
    System.out.println("-- Displayed (be patient, it's coming ...)");
}

}

3个回答

kalaco
kalaco https://blog.csdn.net/qq_21308937/article/details/78200699?locationNum=6&fps=1
一年多之前 回复
weixin_42181180
weixin_42181180 如果这个行的话我也就不会问了
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!

相似问题

10
如何禁止直接访问ajax,限定只接收post请求,禁止直接访问ajax页面
1
Java的Class信息中的常量池中类或接口的全限定名字符串在类的解析后还会在JVM中存在吗?
1
关于wireshark过滤器中的
1
请问怎样能够生成一个自增的流水号
5
java中,任务队列,限定长度,随时添加
1
关于归一化和numpy.log处理数据的疑问
4
可以帮忙写个正则表达式吗?格式为"机器人添加 关键词:XXXXX 回答:YYYY"?
0
OpenCV3.4.1+VS2017人脸识别Ptr<EigenFaceRecognizer> model = EigenFaceRecognizer::create ()无法解析
2
C语言在scanf("%s", );中遇到的问题
1
在Android屏幕适配的时候,出现了value -480*320中的dimen“x299”在基值文件夹中没有声明如何解决呢?
2
C++怎样给时间结构体限定输入数值
0
Geometry 使用 buffer 方法 生成缓冲区,如何限定传入的diatance的单位为米?
0
求大佬帮助,我不知道我是不是有那种情况没考虑到,到OJ上提交显示答案错误(本人c语言菜鸟)?
0
wsimport 调用cxf 出错 WSDLException: faultCode=PARSER_ERROR:WSDLException: faultCode=PARSER_ERROR:
1
redis使用@Cacheable缓存返回值,发送header【Accept:application/xml】请求返回xml数据异常
0
我的代码为什么跑到后面那段就无法继续?后面那段代码应该和前面的代码一致才对呀?
3
限定使用Socket通信,内存4G去传输10G的文本。并且读取过程需要换行,应该如何实现?
1
ASP.NET API项目如何发送带有权限的请求?
2
Spring基于XML配置时出现报错,但是我的路径应该是对的呀,求大神指导
2
用tensorflow做机器翻译时训练代码有问题