I have drawn some Graphics in a JPanel, like circles, rectangles, etc.
But I want to draw some Graphics rotated a specific degree amount, like a rotated ellipse. What should I do?
If you are using plain Graphics, cast to Graphics2D first:
Graphics2D g2d = (Graphics2D)g;
To rotate an entire Graphics2D:
g2d.rotate(Math.toRadians(degrees));
//draw shape/image (will be rotated)
To reset the rotation (so you only rotate one thing):
AffineTransform old = g2d.getTransform();
g2d.rotate(Math.toRadians(degrees));
//draw shape/image (will be rotated)
g2d.setTransform(old);
//things you draw after here will not be rotated
Example:
class MyPanel extends JPanel {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
AffineTransform old = g2d.getTransform();
g2d.rotate(Math.toRadians(degrees));
//draw shape/image (will be rotated)
g2d.setTransform(old);
//things you draw after here will not be rotated
}
}
In your paintComponent() overridden method, cast the Graphics argument to Graphics2D, call rotate() on this Graphics2D, and draw your ellipse.
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 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 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);