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)
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 have a problem in Java. I'm trying to create a 2D shape and rotate it but if I try to rotate it with the Graphics2D's rotate method, the whole canvas gets rotated.
I would like to only rotate a specific shape, not the whole canvas.
My current render method looks like this:
#Override
public void render(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(super.color);
g2d.rotate(Math.toRadians(5));
g2d.fillRect(100, 100, 50, 50);
}
How should I go about doing this?
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());
}
}
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);
}
I have a JPanel with a Grid Layout. In the "cells" of the grid I can put different elements (for example JButtons). There is no problems with that. But now I want to put a filled circle in some of the cells. I also would like to relate an ActionListener with these circles. In more details, if I click the circle it disappears from the current cell and appears in another one. How can I do it in Java? I am using Swing.
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(x, y, diameter, diameter);
g2d.fill(circle);
...
}
Here are some docs about paintComponent (link).
You should override that method in your JPanel and do something similar to the code snippet above.
In your ActionListener you should specify x, y, diameter and call repaint().
/***Your Code***/
public void paintComponent(Graphics g){
/***Your Code***/
g.setColor(Color.RED);
g.fillOval(50,50,20,20);
}
g.fillOval(x-axis,y-axis,width,height);