如何设置高质量的jlabel文本

时间:2015-12-14 22:07:19

标签: java swing

我正在尝试编写一些机制来创建mems。在使用文本绘制图像后,文本质量非常低。我不知道我做错了什么。这是我的以下绘制方法:

   private BufferedImage generateImage(File file) throws IOException {

    Image image = ImageIO.read(file);
    final int imgWidth = image.getWidth(null);
    final int imgHeight = image.getHeight(null);
    int labelHeight = (imgHeight / 2);
    int labelWidth = (imgWidth - 25);
    int xMargin = ((imgWidth - labelWidth) / 2);
    int yMargin = (imgHeight - (labelHeight + 10));
    Font font = new Font("Arial Black", Font.BOLD, 42);
    String upperText = this.doggle.getUppertext().toUpperCase();
    String bottomText = this.doggle.getBottomtext().toUpperCase();

    ImagePanel background = new ImagePanel(image);
    background.setSize(imgWidth, imgHeight);

    ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
    String absoluteDiskPath = servletContext.getRealPath("/resources/images/logo.png");
    BufferedImage imageLogo = ImageIO.read(new File(absoluteDiskPath));

    ImageIcon icon = new ImageIcon(imageLogo);
    JLabel imageLogoPanel = new JLabel ();
    imageLogoPanel.setIcon(icon);
    imageLogoPanel.setSize(imageLogo.getWidth(), imageLogo.getHeight());
    imageLogoPanel.setVerticalAlignment(SwingConstants.BOTTOM);

    JLabel textUpper = new JLabel();
    textUpper.setText(upperText);
    textUpper.setVerticalAlignment(SwingConstants.TOP);
    textUpper.setSize(labelWidth, labelHeight);
    textUpper.setPreferredSize(new Dimension(labelWidth, (imgHeight / 2)));
    textUpper.setFont(font);
    textUpper.setForeground(Color.decode("#" + this.doggle.getUppercolor()));


    JLabel textUpperShadow = new JLabel(upperText, JLabel.CENTER);
    textUpperShadow.setVerticalAlignment(SwingConstants.TOP);
    textUpperShadow.setSize(labelWidth, labelHeight);
    textUpperShadow.setPreferredSize(new Dimension(labelWidth, (imgHeight / 2)));
    textUpperShadow.setFont(font);
    textUpperShadow.setForeground(Color.BLACK);

    JLabel textLower = new JLabel(bottomText, JLabel.CENTER);
    textLower.setVerticalAlignment(SwingConstants.BOTTOM);
    textLower.setSize(labelWidth, labelHeight);
    textLower.setPreferredSize(new Dimension(labelWidth, (imgHeight / 2)));
    textLower.setFont(font);
    textLower.setForeground(Color.decode("#" + this.doggle.getBottomcolor()));

    JLabel textLowerShadow = new JLabel(bottomText, JLabel.CENTER);
    textLowerShadow.setVerticalAlignment(SwingConstants.BOTTOM);
    textLowerShadow.setSize(labelWidth, labelHeight);
    textLowerShadow.setPreferredSize(new Dimension(labelWidth, (imgHeight / 2)));
    textLowerShadow.setFont(font);
    textLowerShadow.setForeground(Color.BLACK);

    background.add(textUpper);
    background.add(textUpperShadow);
    background.add(textLower);
    background.add(textLowerShadow);
    background.add(imageLogoPanel);

    textUpper.setLocation(xMargin, 5);
    textUpperShadow.setLocation((xMargin + 2), 7);
    textLower.setLocation(xMargin, yMargin - 16);
    textLowerShadow.setLocation((xMargin + 2), ((yMargin - 16) + 2));
    imageLogoPanel.setLocation((xMargin - 5), ((yMargin * 2) - 8));

    BufferedImage img = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = img.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    background.printAll(g2d);
    g2d.dispose();

    return img;

}

这是图像的某个部分(顶部的文字是实际文字,底部文字是它应该是什么样子)

我可以做些什么来改善文字的视觉质量?我将非常感谢您的帮助。

enter image description here

0 个答案:

没有答案