Java Code Examples for java.awt.Graphics.setFont()

The following are Jave code examples for showing how to use setFont() of the java.awt.Graphics class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: game2048_tetris   File: ClassicPanel.java   View Source Code Vote up 7 votes
/**画分数等显示信息*/
public void paintScore(Graphics g) {
	g.setColor(new Color(227, 179, 155));
	//画当前分数框
	g.fillRoundRect(180, 3, 110, 70, 11, 11);
	//画最高分框
	g.fillRoundRect(310, 3, 110, 70, 11, 11);
	g.setColor(new Color(239, 198, 0));
	//画图标
	g.fillRoundRect(25, 3, 130, 120, 12, 12);
	g.setColor(Color.white);
	g.setFont(new Font("微软雅黑",Font.PLAIN,20));
	g.drawString("当前分数", 195, 30);
	
	g.drawString(""+score, 200, 57);
	g.drawString("最高分", 335, 30);
	g.drawString(""+mostScore, 335, 57);
	g.setFont(new Font("Impact",Font.PLAIN,48));
	g.drawString("2048", 40, 50);
	g.setFont(new Font("Impact",Font.BOLD,36));
	g.drawString("4 * 4", 50, 90);
}
 
Example 2
Project: openjdk-jdk10   File: UnderlineTest.java   View Source Code Vote up 7 votes
public void paintComponent(Graphics g) {
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, fpd.width, fpd.height);

    g.setColor(Color.RED);
    FontRenderContext frc = ((Graphics2D)g).getFontRenderContext();
    LineMetrics lm = f.getLineMetrics(fps, frc);
    int h = (int)(fpd.height - 20 - lm.getAscent());
    g.drawLine(20, h, fpd.width - 20, h);
    h = fpd.height - 20;
    g.drawLine(20, h, fpd.width - 20, h);
    h = (int)(fpd.height - 20 + lm.getDescent());
    g.drawLine(20, h, fpd.width - 20, h);

    g.setColor(Color.BLACK);
    g.setFont(f);
    g.drawString(fps, 50, fpd.height - 20);
}
 
Example 3
Project: VASSAL-src   File: ColorButton.java   View Source Code Vote up 6 votes
public void paintIcon(Component c, Graphics g, int x, int y) {
  g.setColor(Color.black);
  g.drawRect(x, y, swatchWidth-1, swatchHeight-1);

  if (color != null) {
    g.setColor(color);
    g.fillRect(x+1, y+1, swatchWidth-2, swatchHeight-2);
  }
  else {
    // paint no color and a "nil" if the color is null
    g.setColor(UIManager.getColor("controlText"));
    g.setFont(FONT);
    g.drawString("nil",
      x+(swatchWidth - g.getFontMetrics(FONT).stringWidth("nil"))/ 2,
      y+(swatchHeight + g.getFontMetrics(FONT).getAscent())/2);
  }
}
 
Example 4
Project: JDigitalSimulator   File: BinaryCounter.java   View Source Code Vote up 6 votes
@Override public void paint(Graphics graphics) {
	graphics.setColor(Color.BLACK);
	graphics.drawPolyline(new int[]{9 , 9 , 5 , 5, size.width-5, size.width-5, size.width-9, size.width-9},
			new int[]{30, 25, 25, 0, 0           , 25          , 25          , 30          }, 8);
	graphics.setFont(graphics.getFont().deriveFont(10f));
	graphics.drawString("Binary", 25, 22);
	graphics.setFont(graphics.getFont().deriveFont(8f));
	graphics.drawString("C", 8, inputC.getLocation().y+4);
	graphics.drawString("R", 8, inputR.getLocation().y+4);
	for(int output=0;output<outputs.getContactsCount();output++) {
		int top = 30+output*20;
		graphics.drawRect(5, top, size.width-10, 20);
		graphics.drawString("+", size.width-12, top+13);
	}
	ContactUtilities.paintSolderingJoints(graphics, contacts);
}
 
Example 5
Project: OpenJSharp   File: NervousText.java   View Source Code Vote up 6 votes
@Override
public void paint(Graphics g) {
    int length = bannerChars.length;
    for (int i = 0, x = 0; i < length; i++) {
        int wd, ht;
        if (attributes[i] == '^') {
            wd = SMALL_WD;
            ht = SMALL_HT;
            g.setFont(smallFont);
        } else {
            wd = REGULAR_WD;
            ht = REGULAR_HT;
            g.setFont(regularFont);
        }
        int px = (int) (10 * Math.random() + x);
        int py = (int) (10 * Math.random() + ht);
        g.drawChars(bannerChars, i, 1, px, py);
        x += wd;
    }
}
 
Example 6
Project: java-course   File: BarChartComponent.java   View Source Code Vote up 6 votes
/**
 * Draws numbers on y-axis.
 * 
 * @param g
 *            The {@link Graphics} object.
 */
private void printNumbersY(Graphics g) {
    FontMetrics fm = g.getFontMetrics();
    g.setColor(Color.black);
    Font currentFont = g.getFont();
    g.setFont(new Font(currentFont.toString(), Font.BOLD, currentFont
            .getSize()));

    for (int i = 0; i < hLines; i++) {
        String text = Integer.toString(chart.getyMin() + i
                * chart.getyStep());
        g.drawString(text, xPoint - fm.stringWidth(text) - SPACE,
                rect.height - yPoint - i * hStep + (textHeight / 4));
    }

    g.setFont(currentFont);
}
 
Example 7
Project: ScreenCut   File: ScreenCut.java   View Source Code Vote up 5 votes
/**
 * 绘制文字
 */
private void drawString(Point st, String str, Color c, Graphics g, int size) {
    g.setFont(new Font("宋体", Font.PLAIN, size));
    g.setColor(c);
    FontMetrics fm = g.getFontMetrics();
    Rectangle2D r2d = fm.getStringBounds(str, g);
    g.drawString(str, st.x, st.y + (int) r2d.getHeight());
}
 
Example 8
Project: JDigitalSimulator   File: LoadableRegister.java   View Source Code Vote up 5 votes
@Override public void paint(Graphics graphics) {
	super.paint(graphics);
	graphics.drawString("L", 8, inputL.getLocation().y+4);
	graphics.setFont(graphics.getFont().deriveFont(10f));
	graphics.drawString("Loadable", 20, 14);
	ContactUtilities.paintSolderingJoints(graphics, contacts);
}
 
Example 9
Project: incubator-netbeans   File: CheckboxInplaceEditor.java   View Source Code Vote up 5 votes
/** Overridden to be able to calculate the preferred size without having
 * to be added to the AWT hierarchy */
public Dimension getPreferredSize() {
    if (isShowing()) {
        return super.getPreferredSize();
    }

    Dimension result = PropUtils.getMinimumPanelSize();
    Graphics g = PropUtils.getScratchGraphics(this);
    g.setFont(getFont());

    String txt = getText();
    Icon i = getIcon();
    FontMetrics fm = g.getFontMetrics(getFont());
    int w = fm.stringWidth(txt);
    int h = fm.getHeight();

    if (i != null) {
        w += (i.getIconWidth() + getIconTextGap());
        h = Math.max(h, result.height);
    }

    Insets ins = getInsets();

    if (ins != null) {
        w += (ins.left + ins.right);
        h += (ins.top + ins.bottom);
    }

    w += 22; //A small fudge factor to avoid truncated text

    result.width = Math.max(result.width, w);
    result.height = Math.max(result.height, h);

    return result;
}
 
Example 10
Project: oxygen-git-plugin   File: ToolbarPanel.java   View Source Code Vote up 5 votes
/**
* Create the "Push" button.
* 
* @return the "Push" button.
*/
private ToolbarButton createPushButton() {
  return new ToolbarButton(createPushAction(), false) {
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);

		String str = "";
		if (pushesAhead > 0) {
			str = "" + pushesAhead;
		}
		if (pushesAhead > 9) {
			pushButton.setHorizontalAlignment(SwingConstants.LEFT);
		} else {
			pushButton.setHorizontalAlignment(SwingConstants.CENTER);
		}
		g.setFont(g.getFont().deriveFont(Font.BOLD, 8.5f));
		FontMetrics fontMetrics = g.getFontMetrics(g.getFont());
		int stringWidth = fontMetrics.stringWidth(str);
		int stringHeight = fontMetrics.getHeight();
		g.setColor(new Color(255, 255, 255, 100));
		g.fillRect(pushButton.getWidth() - stringWidth - 1, pushButton.getHeight() - stringHeight, stringWidth,
				stringHeight);
		g.setColor(Color.BLACK);
		g.drawString(str, pushButton.getWidth() - stringWidth, pushButton.getHeight() - fontMetrics.getDescent());
	}
};
}
 
Example 11
Project: etomica   File: Image.java   View Source Code Vote up 5 votes
public static int[] getTextPixels(String text, JmolFont font3d, Object gObj,
                                  Object image, int width, int height,
                                  int ascent) {
  Graphics g = (Graphics) gObj;
  g.setColor(Color.black);
  g.fillRect(0, 0, width, height);
  g.setColor(Color.white);
  g.setFont((Font) font3d.font);
  g.drawString(text, 0, ascent);
  return grabPixels(image, width, height, null, 0, 0);
}
 
Example 12
Project: Equella   File: TaskRenderer.java   View Source Code Vote up 5 votes
private Dimension getTextArea(Graphics g, String text)
{
	Dimension d = new Dimension();
	if( g != null )
	{
		g.setFont(DEFAULT_FONT);
		Rectangle2D textSize = g.getFontMetrics().getStringBounds(text, g);
		d.width = (int) textSize.getWidth();
		d.height = (int) textSize.getHeight();
	}
	return d;
}
 
Example 13
Project: gitplex-mit   File: AvatarGenerator.java   View Source Code Vote up 5 votes
public static BufferedImage generate(int width, int height, String message,
		String fontFamily, Color background, Color foreground) {
	BufferedImage bi = new BufferedImage(width, height,
			BufferedImage.TYPE_INT_ARGB);

	Graphics ig2 = null;

	try {
		ig2 = bi.getGraphics();

		ig2.setColor(background);
		ig2.fillRect(0, 0, width, height);

		int fontSize = new Double(height * 0.5d).intValue();
		Font font = new Font(fontFamily, Font.PLAIN, fontSize);
		Map<TextAttribute, Object> map = new Hashtable<TextAttribute, Object>();
		map.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
		font = font.deriveFont(map);
		ig2.setFont(font);
		ig2.setColor(foreground);
		drawCenteredString(message.toUpperCase(), width, height, ig2);
	} finally {
		if (ig2 != null)
			ig2.dispose();
	}

	return bi;
}
 
Example 14
Project: jdk8u-jdk   File: TextRenderTests.java   View Source Code Vote up 5 votes
public void runTest(Object ctx, int numReps) {
    TextContext tctx = (TextContext)ctx;
    Graphics g = tctx.graphics;
    g.setFont(tctx.font);
    String text = tctx.text;
    do {
        g.drawString(text, 40, 40);
    } while (--numReps >= 0);
}
 
Example 15
Project: JDigitalSimulator   File: LoadableShiftRegister.java   View Source Code Vote up 5 votes
@Override public void paint(Graphics graphics) {
	super.paint(graphics);
	graphics.drawString("+", 8, inputI.getLocation().y+4);
	graphics.drawString("C", 8, inputC.getLocation().y+4);
	graphics.drawString("R", 8, inputR.getLocation().y+4);
	graphics.setFont(graphics.getFont().deriveFont(10f));
	graphics.drawString("Shift", 20, 22);
	ContactUtilities.paintSolderingJoints(graphics, contacts);
}
 
Example 16
Project: rapidminer   File: ComponentPrinter.java   View Source Code Vote up 5 votes
/**
 * 
 * @param g
 *            the graphics object
 * @param pageFormat
 *            the page format
 * @param width
 *            the downscaled width
 * @param height
 *            the downscaled height
 * @param pageIndex
 *            the page index that should be printed
 */
public int print(Graphics g, double x, double y, double width, double height, int pageIndex) {
	if (pageIndex >= components.length) {
		return NO_SUCH_PAGE;
	}

	String title = components[pageIndex].getExportName();
	if (title == null) {
		title = LicenseTools.translateProductName(ProductConstraintManager.INSTANCE.getActiveLicense());
	}
	Rectangle2D rect = TITLE_FONT.getStringBounds(title, ((Graphics2D) g).getFontRenderContext());
	g.setFont(TITLE_FONT);
	int stringX = (int) (x + width / 2 - rect.getWidth() / 2);
	int stringY = (int) (y - rect.getY());
	g.drawString(title, stringX, stringY);

	// remove string rect height from graphic pane
	height = height - rect.getHeight() * 2;

	Graphics2D translated = (Graphics2D) g.create((int) x, (int) (y + rect.getHeight() * 2), (int) width, (int) height);

	double widthFactor = width / components[pageIndex].getExportComponent().getWidth();
	double heightFactor = height / components[pageIndex].getExportComponent().getHeight();
	double scaleFactor = Math.min(widthFactor, heightFactor);
	translated.scale(scaleFactor, scaleFactor);
	components[pageIndex].getExportComponent().print(translated);
	translated.dispose();
	return PAGE_EXISTS;
}
 
Example 17
Project: myfaces-trinidad   File: BlafImageUtils.java   View Source Code Vote up 4 votes
static ImmInsets __getDialogPadding()
{
  if (_sDialogPadding != null)
    return _sDialogPadding;

  // Create a BufferedImage that we can use to rasterize some glyphs.
  int width = 40;
  int height = 40;
  BufferedImage image = new BufferedImage(40,
                                          40,
                                          BufferedImage.TYPE_INT_ARGB);

  // Get the Graphics object to use to draw into the image
  Graphics g = image.getGraphics();

  // Clear out the image
  g.setColor(Color.white);
  g.fillRect(0, 0, width, height);

  // Render our glyphs
  g.setColor(Color.black);
  g.setFont(new Font("Dialog", Font.PLAIN, 12));

  FontMetrics metrics = g.getFontMetrics();
  int baseline = metrics.getAscent();

  g.drawString("X", 0, baseline);

  // Now that we have rendered the glyphs, we examine the
  // image to see how many lines of padding we've got.
  int top = 0;
  for (int y = 0; y < height; y++)
  {
    if (!_isWhiteScanline(image, y, width))
    {
      top = y;
      break;
    }
  }

  // Just use the descent as the bottom padding
  int bottom = metrics.getDescent();

  _sDialogPadding = new ImmInsets(top, 0, bottom, 0);


  // Clean up
  g.dispose();
  image.flush();

  return _sDialogPadding;
}
 
Example 18
Project: Yidu   File: SecurityImage.java   View Source Code Vote up 4 votes
/**
 * 生成验证码图片
 * 
 * @param securityCode
 *            验证码字符
 * @return BufferedImage 图片
 */
public static BufferedImage createImage(String securityCode) {
    // 验证码长度
    int codeLength = securityCode.length();
    // 字体大小
    int fSize = 15;
    int fWidth = fSize + 1;
    // 图片宽度
    int width = codeLength * fWidth + 6;
    // 图片高度
    int height = fSize * 2 + 1;
    // 图片
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics g = image.createGraphics();
    // 设置背景色
    g.setColor(Color.WHITE);
    // 填充背景
    g.fillRect(0, 0, width, height);
    // 设置边框颜色
    g.setColor(Color.LIGHT_GRAY);
    // 边框字体样式
    g.setFont(new Font("Arial", Font.BOLD, height - 2));
    // 绘制边框
    g.drawRect(0, 0, width - 1, height - 1);
    // 绘制噪点
    Random rand = new Random();
    // 设置噪点颜色
    g.setColor(Color.LIGHT_GRAY);
    for (int i = 0; i < codeLength * 6; i++) {
        int x = rand.nextInt(width);
        int y = rand.nextInt(height);
        // 绘制1*1大小的矩形
        g.drawRect(x, y, 1, 1);
    }
    // 绘制验证码
    int codeY = height - 10;
    // 设置字体颜色和样式
    g.setColor(new Color(19, 148, 246));
    g.setFont(new Font("Georgia", Font.BOLD, fSize));
    for (int i = 0; i < codeLength; i++) {
        g.drawString(String.valueOf(securityCode.charAt(i)), i * 16 + 5, codeY);
    }
    // 关闭资源
    g.dispose();
    return image;
}
 
Example 19
Project: incubator-netbeans   File: AquaViewTabDisplayerUI.java   View Source Code Vote up 4 votes
@Override
protected void paintTabContent(Graphics g, int index, String text, int x,
                               int y, int width, int height) {
    FontMetrics fm = getTxtFontMetrics();

    // setting font already here to compute string width correctly
    g.setFont(getTxtFont());
    int textW = width;

    if (isSelected(index)) {
        Component buttons = getControlButtons();
        if( null != buttons ) {
            Dimension buttonsSize = buttons.getPreferredSize();

            if( width < buttonsSize.width+ICON_X_PAD ) {
                buttons.setVisible( false );
            } else {
                buttons.setVisible( true );
                textW = width - (buttonsSize.width + ICON_X_PAD + 2*TXT_X_PAD);
                buttons.setLocation( x + textW+2*TXT_X_PAD-2, y + (height-buttonsSize.height)/2 );
            }
        }
    } else {
        textW = width - 2 * TXT_X_PAD;
    }

    if (text.length() == 0) {
        return;
    }

    int textHeight = fm.getHeight();
    int textY;
    int textX = x + TXT_X_PAD;
    if (index == 0)
        textX = x + 5;

    if (textHeight > height) {
        textY = (-1 * ((textHeight - height) / 2)) + fm.getAscent()
                - 1;
    } else {
        textY = (height / 2) - (textHeight / 2) + fm.getAscent();
    }

    boolean slidedOut = false;
    WinsysInfoForTabbedContainer winsysInfo = displayer.getContainerWinsysInfo();
    if( null != winsysInfo && winsysInfo.isSlidedOutContainer() )
        slidedOut = false;
    if( isTabBusy( index ) && !slidedOut ) {
        Icon busyIcon = BusyTabsSupport.getDefault().getBusyIcon( isSelected( index ) );
        textW -= busyIcon.getIconWidth() - 3 - TXT_X_PAD;
        busyIcon.paintIcon( displayer, g, textX, y+(height-busyIcon.getIconHeight())/2);
        textX += busyIcon.getIconWidth() + 3;
    }

    int realTextWidth = (int)HtmlRenderer.renderString(text, g, textX, textY, textW, height, getTxtFont(),
                      UIManager.getColor("textText"), //NOI18N
                      HtmlRenderer.STYLE_TRUNCATE, false);
    realTextWidth = Math.min(realTextWidth, textW);
    if( textW > realTextWidth )
        textX += (textW - realTextWidth) / 2;


    HtmlRenderer.renderString(text, g, textX, textY, textW, height, getTxtFont(),
                      UIManager.getColor("textText"),
                      HtmlRenderer.STYLE_TRUNCATE, true);
}
 
Example 20
Project: Equella   File: FlatterCheckBoxUI.java   View Source Code Vote up 4 votes
@Override
public synchronized void paint(Graphics g, JComponent c)
{
	AbstractButton b = (AbstractButton) c;
	ButtonModel model = b.getModel();
	Dimension d = b.getSize();

	g.setFont(c.getFont());
	FontMetrics fm = g.getFontMetrics();

	Icon icon = mIconUnchecked;
	if( model.isPressed() && model.isSelected() )
	{
		icon = mIconPressedChecked;
	}
	else if( model.isPressed() && !model.isSelected() )
	{
		icon = mIconPressedUnchecked;
	}
	else if( !model.isPressed() && model.isSelected() )
	{
		icon = mIconChecked;
	}

	int x = 0;
	int y = (d.height - icon.getIconHeight()) / 2;
	icon.paintIcon(c, g, x, y);

	if( b.isEnabled() )
	{
		g.setColor(mTextNormal);
	}
	else
	{
		g.setColor(mTextDisabled);
	}

	String caption = b.getText();
	x = icon.getIconWidth() + mTextIconGap;
	y = (d.height + fm.getAscent()) / 2;
	g.drawString(caption, x, y);
}