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

The following are Jave code examples for showing how to use drawString() 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: 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 2
Project: openjdk-jdk10   File: MissingGlyphTest.java   View Source Code Vote up 6 votes
@Override
protected void paintComponent(Graphics g) {
    if (font.canDisplayUpTo(text) == -1) {
        g.setColor(Color.black);
        g.setFont(font);
        g.drawString(text, 70, 110);
    }
}
 
Example 3
Project: New-Kepard   File: JWindow.java   View Source Code Vote up 6 votes
@Override
public void paintComponent(Graphics g)
{
	super.paintComponent(g);
	
	if(window.paintReady)
	{
		PaintFlightLog(g);
		
		if(paint_arg == PAINT_ARG_FLIGHT_TELEMETRY)
			PaintFlightTelemetry(g);
	}
	else
	{
		if(paint_arg == PAINT_ARG_CONNECTION_RED) g.setColor(Color.RED);
		g.drawString("Remember to start the server!", 110, 50);
		if(paint_arg == PAINT_ARG_CONNECTION_RED) g.setColor(Color.BLACK);
		
		g.drawString("Enter target apoapsis height:", 105, 165);
	}
	
}
 
Example 4
Project: pdfjumbler   File: PdfList.java   View Source Code Vote up 6 votes
private void drawCenteredText(Graphics g, String message, boolean alignCenter) {
	String[] lines = message.split("\n");
	
	FontMetrics fm = g.getFontMetrics();
	int height = lines.length * fm.getHeight();
	int width = 0;
	if (!alignCenter) {
		for (String line : lines) {
			if (fm.stringWidth(line) > width) {
				width = fm.stringWidth(line);
			}
		}
	}
	
	for (int i = 0; i < lines.length; i++) {
		g.drawString(
			lines[i],
			(getWidth() - (alignCenter ? fm.stringWidth(lines[i]) : width)) / 2,
			(getHeight() - height) / 2 + i*fm.getHeight()
		);
	}
}
 
Example 5
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 6
Project: OpenJSharp   File: ThreeD.java   View Source Code Vote up 6 votes
@Override
public void paint(Graphics g) {
    if (md != null) {
        md.mat.unit();
        md.mat.translate(-(md.xmin + md.xmax) / 2,
                -(md.ymin + md.ymax) / 2,
                -(md.zmin + md.zmax) / 2);
        md.mat.mult(amat);
        md.mat.scale(xfac, -xfac, 16 * xfac / getSize().width);
        md.mat.translate(getSize().width / 2, getSize().height / 2, 8);
        md.transformed = false;
        md.paint(g);
        setPainted();
    } else if (message != null) {
        g.drawString("Error in model:", 3, 20);
        g.drawString(message, 10, 40);
    }
}
 
Example 7
Project: openjdk-jdk10   File: LightweightEventTest.java   View Source Code Vote up 5 votes
@Override
public void paint(Graphics g) {

    super.paint(g);
    Rectangle bounds = getBounds();
    if (superIsButton) {
        return;
    }
    Dimension size = getSize();
    Color oldColor = g.getColor();

    // draw border
    g.setColor(getBackground());
    g.fill3DRect(0, 0, size.width, size.height, false);
    g.fill3DRect(3, 3, size.width - 6, size.height - 6, true);

    // draw text
    FontMetrics metrics = g.getFontMetrics();
    int centerX = size.width / 2;
    int centerY = size.height / 2;
    int textX = centerX - (metrics.stringWidth(labelString) / 2);
    int textY = centerY
            + ((metrics.getMaxAscent() + metrics.getMaxDescent()) / 2);
    g.setColor(getForeground());
    g.drawString(labelString, textX, textY);

    g.setColor(oldColor);
}
 
Example 8
Project: intermediateProgramming   File: Freeway.java   View Source Code Vote up 5 votes
public void draw(DrawingPanel panel, Graphics g) {
if(x==null) {
     return;
   }
   road.setBlock(0, 0, new byte[roadLength][1]);
   for(int i = 0;i<numberOfCars;i++) {
     if(x[i] != -1) {
   	road.setValue(x[i], 0, (byte) 1);
     }
   }
   road.draw(panel, g);
   g.drawString("Number of Steps = "+steps, 10, 20);
   g.drawString("Flow = "+ControlUtils.f3(flow/(roadLength*steps)), 10, 40);
   g.drawString("Density = "+ControlUtils.f3(((double) numberOfCars)/(roadLength)), 10, 60);
 }
 
Example 9
Project: pdfjumbler   File: PdfCellRenderer.java   View Source Code Vote up 5 votes
@Override
public void paint(Graphics g) {
	((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	super.paint(g);

	if (page == null) {
		return;
	}

	Image cachedImage;
	
	if (imageCache.containsKey(page)) {
		cachedImage = imageCache.get(page);
	} else {
		try {
			cachedImage = renderer.renderPage(page, thumbnailWidth, thumbnailHeight);
		}
		catch (IOException e) {
			throw new RuntimeException(e);
		}
		imageCache.put(page, cachedImage);
	}
	
	g.drawImage(cachedImage, padding, padding, null);
	g.setColor(Color.LIGHT_GRAY);
	g.drawRect(padding, padding, cachedImage.getWidth(null), cachedImage.getHeight(null));
	
	g.setColor(this.getForeground());
	g.drawString(
		page.toString(),
		thumbnailWidth + 2*padding,
		padding + g.getFontMetrics().getAscent()
	);
}
 
Example 10
Project: MapAnalyst   File: MapObjectImpl.java   View Source Code Vote up 5 votes
public void paintText(Graphics g, Point position) {
    if (name != null && g != null && position != null) {
        if (getFont() == null) {
            Font f = getDefaultFont();
            setFont(new Font(f.getName(), Font.BOLD, f.getSize()));
        }
        g.setColor(Color.DARK_GRAY);
        g.setFont(getFont());
        g.drawString(name, position.x+MapMarkerDot.DOT_RADIUS+2, position.y+MapMarkerDot.DOT_RADIUS);
    }
}
 
Example 11
Project: The-Mysterious-Mind-Of-Jack   File: InventButton.java   View Source Code Vote up 5 votes
public void renderTag(Graphics g) {
	if (pos.y == 110) {
		g.setColor(Color.WHITE);
		g.setFont(new Font("Times New Java", Font.BOLD, 20));
		g.drawString("" + (int)((pos.x + 40)/50), (int)(pos.x + 10), (int)(pos.y + 10));
	}
}
 
Example 12
Project: LivroJavaComoProgramar10Edicao   File: ColorJPanel.java   View Source Code Vote up 5 votes
@Override
public void paintComponent(Graphics g)
{
   super.paintComponent(g); 
   this.setBackground(Color.WHITE);

   // set new drawing color using integers
   g.setColor(new Color(255, 0, 0));
   g.fillRect(15, 25, 100, 20);
   g.drawString("Current RGB: " + g.getColor(), 130, 40);

   // set new drawing color using floats
   g.setColor(new Color(0.50f, 0.75f, 0.0f));
   g.fillRect(15, 50, 100, 20);
   g.drawString("Current RGB: " + g.getColor(), 130, 65);

   // set new drawing color using static Color objects
   g.setColor(Color.BLUE);
   g.fillRect(15, 75, 100, 20);
   g.drawString("Current RGB: " + g.getColor(), 130, 90);

   // display individual RGB values
   Color color = Color.MAGENTA;
   g.setColor(color);
   g.fillRect(15, 100, 100, 20);
   g.drawString("RGB values: " + color.getRed() + ", " +
      color.getGreen() + ", " + color.getBlue(), 130, 115);
}
 
Example 13
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 14
Project: incubator-netbeans   File: HeapView.java   View Source Code Vote up 5 votes
/**
 * Renders the text.
 */
private void paintText(Graphics g, int w, int h) {
    g.setFont(getFont());
    String text = getHeapSizeText();
    FontMetrics fm = g.getFontMetrics();
    int textWidth = fm.stringWidth(text);
    g.drawString(text, (w - maxTextWidth) / 2 + (maxTextWidth - textWidth), 
            h / 2 + fm.getAscent() / 2 - 2);
}
 
Example 15
Project: incubator-netbeans   File: ResultBar.java   View Source Code Vote up 5 votes
private void paintText(Graphics g, int w, int h) {
    g.setFont(getFont());
    String text = getString();
    FontMetrics fm = g.getFontMetrics();
    int textWidth = fm.stringWidth(text);
    g.drawString(text, (w - textWidth) / 2,
            h - fm.getDescent() - ((h - fm.getHeight()) / 2));
}
 
Example 16
Project: The-Mysterious-Mind-Of-Jack   File: InventButton.java   View Source Code Vote up 5 votes
@Override
public void render(Graphics g) {
	if (item != null) this.image = item.getImage();
	else if (item == null && !imageSet) image = null;
	else if (item == null && imageSet) this.image = setImage;
	super.render(g);
	if (amount > 0) {
		g.setColor(Color.WHITE);
		g.setFont(new Font("Times New Java", Font.PLAIN, 10));
		g.drawString("" + amount, (int)(pos.x), (int)(pos.y + 8));
	}
	if (tag) renderTag(g);
}
 
Example 17
Project: JDigitalSimulator   File: ExamplePluginQuick.java   View Source Code Vote up 4 votes
/**
 * In the paint method you may use all Graphics2D methods, to draw your component as you like. Please
 * do not do any calculations in here, because this would interfere with the general design of the Simulator.
 */
@Override public void paint(Graphics graphics) {
	graphics.setColor(Color.BLACK); /** do not forget to set a color first */
	graphics.drawRect(0, 0, getSize().width, getSize().height);
	graphics.drawString("Quick", 25, 25); /** do not draw outside of the components bounds please! */
}
 
Example 18
Project: incubator-netbeans   File: ColorComboBoxRenderer.java   View Source Code Vote up 4 votes
@Override
public void paint (Graphics g) {
    
    //AntiAliasing check
    @SuppressWarnings("unchecked") //NOI18N
    Map<?, ?> aa = (Map<?, ?>) Toolkit.getDefaultToolkit().getDesktopProperty("awt.font.desktophints"); //NOI18N

    if (aa != null) {
        ((Graphics2D) g).setRenderingHints(aa);
    }
    
    Color oldColor = g.getColor ();
    Dimension size = getSize ();
    if (isFocusOwner ()) {
        g.setColor (SystemColor.textHighlight);
    } else {
        g.setColor (getBackground ());
    }
    g.fillRect (0, 0, size.width, size.height);

    if (value != null) {
        int i = (size.height - SIZE) / 2;
        if (value.color != null) {
            g.setColor (Color.black);
            g.drawRect (i, i, SIZE, SIZE);
            g.setColor (value.color);
            g.fillRect (i + 1, i + 1, SIZE - 1, SIZE - 1);
        }
        if (value.text != null) {
            if (isFocusOwner ()) {
                g.setColor (SystemColor.textHighlightText);
            } else {
                g.setColor (getForeground ());
            }
            if (value.color != null) {
                g.drawString (value.text, i + SIZE + 5, i + SIZE);
            } else {
                g.drawString (value.text, 5, i + SIZE);
            }
        }
    }

    g.setColor (oldColor);
}
 
Example 19
Project: Servlet_Applet_Communication   File: LoginApplet.java   View Source Code Vote up 4 votes
public void paint(Graphics g) {
Font font = new Font("arial", Font.BOLD, 30);
g.setFont(font);
g.drawString("Login Status :"+response, 50, 300);
}
 
Example 20
Project: JDigitalSimulator   File: ExamplePluginFull.java   View Source Code Vote up 3 votes
/**
 * Let us implement our first method. So we will have to
 *    <code>@Override public void paint(Graphics graphics)</code> //XXX
 * of the <code>Paintable</code> interface.
 * This method is used to draw the whole component and is called for the first time when
 * generating a preview image for the component-catalog of the JDigitalSimulator.
 * You may use every possibility to paint your component provided by the java
 * <code>Graphics2D</code> classes, as described in the java documentation.
 * It is strictly recommended that you only draw into the bounds of your own Component! It is
 * a known-feature that you are able to draw outside of your component bounds (returned by
 * <code>getSize</code>, but do not do it if you do not know exactly what you are doing.
 * Note: Do <bold>not</bold> use this method for calculating anything! This method will be
 *       likely get called when something changes on the frontend, but it is not guaranteed in
 *       any ways, that it: a) is called every time b) called in fixed time intervals.
 * Please use the calculate method for this reason, described later on.
 */
@Override public void paint(Graphics graphics) {
	Dimension size = getSize(); /** you may call other (even private) methods in here! */
	graphics.setColor(Color.BLACK); /** do not assume the color does fit right away */
	graphics.drawRect(0, 0, size.width, size.height); /** only draw inside this bounds! */
	graphics.drawString(getAttributes().name, 0, 30);
	/** there is no need to call the super.paint method for any reason */
}