I want to print some text using the paintComponent(..) method.
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.drawString("Hello world", 10, 10);
}
But the text is somewhat jaggy.
How could you force text drawing with [anti-aliasing] in this method?
Thank you.
You can set double buffering by:
class MyPanel extends JPanel {
public MyPanel() {
super(true);//set Double buffering for JPanel
}
}
or simply call JComponent#setDoubleBuffered(..).
You can also set RenderingHints for Graphics2D objects like anti-aliasing and text anti-aliasing to improve Swing painting quality by:
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D graphics2D = (Graphics2D) g;
//Set anti-alias!
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// Set anti-alias for text
graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphics2D.setColor(Color.red);
graphics2D.drawString("Hello world", 10, 10);
}
Related
Im trying to draw a lightning bolt on my canvas but the line is only one pixel wide. How can I make the line thicker?
canvas.drawLine(lightningBoltArray[i].startXArray[k],lightningBoltArray[i].startYArray[k],lightningBoltArray[i].endXArray[k],lightningBoltArray[i].endYArray[k],paintHalfAlpha);
Is the trick somewhere in the settings of the applied paint?
If you're not doing something that allows you do to this you're probably painting incorrectly. Assuming that this is in a JPanel instance. It will also work for paint(Graphics g) in Canvas but most painting in Java is done using Swing
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// other stuff
float width = 3f;
BasicStroke stroke = new BasicStroke(width);
g2d.setStroke(stroke);
g2d.drawLine(...);
// more stuff
}
I am trying to draw a circle for a game I am making for a class.
This is my code:
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
// Assume x, y, and diameter are instance variables.
Ellipse2D.Double circle = new Ellipse2D.Double(50, 50, 150, 150);
}
However, when I run the code, it is not seen (My frame is 300 by 300)
I want to fill my Panel with an Rectangle to override the image. But with my code nothing happens. You know why? I dont want to use setBackground.
Graphics g = JPanel.getGraphics();
g.setColor(Color.red);
g.fillRect(0, 0, window.drawPanel.getWidth(), window.drawPanel.getHeight());
If you want to change the panel background with this way. You need to override paintComponent method like this.
JPanel jYourPanel = new JPanel(){
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//lets paint background
g2.setColor(Color.RED);
g2.fillRect(0, 0, getWidth(), getHeight());
}
}
For Java swing, I'm trying to add an image in a panel and create the image using JLabel.
JLabel imageLabel = new JLabel() {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
BufferedImage bi = ImageIO.read(imageInputStream);
double scale = 50 / bi.getHeight();
g2.scale(scale, scale);
g2.drawImage(bi, 0, 0, null);
}
}
On web
<img src="imageURL" hieght="50"/>
Why Swing draws the image, its quality is not as good as web. Why?
I'm trying to embed a TTF font and then use draw it with Grapics2D. I've been able to create the font, but I'm not exactly sure how to pass the font to setFont. I make a new font here, which throws no exceptions:
private Font pixel = Font.createFont(Font.TRUETYPE_FONT, this.getClass().getResourceAsStream("font/amora.ttf"));
But I can't figure out how to draw it with setFont();
Here's my code:
private static final long serialVersionUID = 1L;
private Timer timer;
private Char Char;
private Font pixel = Font.createFont(Font.TRUETYPE_FONT, this.getClass().getResourceAsStream("font/amora.ttf")); <<<--------
public Board() throws FontFormatException, IOException {
addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.BLACK);
setDoubleBuffered(true);
Char = new Char();
timer = new Timer(5, this);
timer.start();
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(Char.getImage(), Char.getX(), Char.getY(), this);
g.setColor(Color.white);
g.setFont( What goes here? ); // <------------
g.drawString("Amora Engine rev25 (acetech09)", 10, 20);
g.drawString(Char.getDebugStats(0), 10, 40);
g.drawString(Char.getDebugStats(1), 10, 60);
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void actionPerformed(ActionEvent e) {
Char.move();
repaint();
}
}
Any help would be greatly appreciated. Thanks.
You could just do...
g.setFont(pixel);
But you might have better sucess with
g.setFont(pixel.deriveFont(Font.BOLD, 36f));
Are variations of....
Also, don't dispose of a Graphics context you did not create...
Graphics2D g2d = (Graphics2D)g;
/*...*/
// g.dispose();
Or
Graphics2D g2d = (Graphics2D)g.create();
/*...*/
g.dispose();
I'd also be loathed to override the paint method. Assuming you're using something like JComponent or JPanel, you should use paintComponent. If you're rendering directly yo a top level container (like JFrame), then I wouldn't. There are issues with double buffering and frame borders that won't make your life fun...
I'm also concerned about new Timer(5, this) - 5 milliseconds is close enough to 0 to make little difference. You'd be better of with something like 40, which should give you something like 25fps or 17 which will give you roughly 60fps...
It should be
g.setFont( this.pixel );
If this is not working, try:
Commenting out the setFont instruction.
Replacing Font.createFont with a reference to a Java standard font.
to rule out possible issues.