用给出的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 ...)");
}
}