Turn figure around a given point on the angle - java

I draw an oval g.fillOval (0, 0, 100, 40); How to turn it from the center of the figure at any angle?

Using Graphics2D, you could do:
Graphics2D g2 = (Graphics2D)g;
g2.rotate(angle, 51, 21);

Related

Rotate two parallel lines to create an X

The following code is the code that I am using to rotate two rectangles is below
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setColor(Color.WHITE);
//r1
Rectangle2D r1 = new Rectangle2D.Double(0, 0, 50, 4);
g2d.rotate(Math.toRadians(45));
g2d.fill(r1);
//r3
Rectangle2D r3 = new Rectangle2D.Double(0, 25, 50, 4);
g2d.rotate(Math.toRadians(-90));
g2d.fill(r3);
This create something which looks like this
Whereas I am trying to create something which looks like this
This occurs since when the rectangles are rotated, they are both rotated around the point 0,0. To fix that I tried using rotate(double theta, double x, double y). However I am having trouble using that correctly. For example when I have tried
Rectangle2D r3 = new Rectangle2D.Double(0, 25, 50, 4);
g2d.rotate(Math.toRadians(-90), 25, 25);
or
Rectangle2D r3 = new Rectangle2D.Double(0, 25, 50, 4);
g2d.rotate(Math.toRadians(-90), 0, 25);
I get similar undesired results when both the rectangles were being rotated around the point 0,0. I would appreciate any help if fixing my problem.
If you are wondering why I have done it like this, it is because I am hoping to make a effect similar to when you click on the 3 parallel lines seen here by the time I finish coding the graphic
package test;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import javax.swing.*;
public class Cross extends JPanel {
private Rectangle2D rectangle;
Cross() {
rectangle = new Rectangle2D.Double(0, 0, 50, 4);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.red);
g2.fillRect(0, 0, getWidth(), getHeight());
g2.setColor(Color.white);
AffineTransform at = g2.getTransform();
g2.translate(5, 5);
g2.rotate(Math.toRadians(45));
g2.fill(rectangle);
g2.setTransform(at);
g2.translate(5, 5 + Math.sqrt(2) * 25);
g2.rotate(Math.toRadians(-45));
g2.fill(rectangle);
g2.setTransform(at);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Cross");
frame.add(new Cross());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(128, 128);
frame.setVisible(true);
}
}
Although I think I might have done mistake somewhere with maths (it looks somewhat odd), this should give you an idea.
So it turns out that this can be done with some relatively simple maths. As the shape I am trying to make is a perfect X.
To work out the position for the rectangle we can use Pythagorean theorem.
The image above shows two steps.
Translation [2√2, 0] from the point [0, 0]
Rotate 45deg from the point [2√2, 0]
Next we need to work out the minimum point of this rectangle. Again we can use Pythagorean theorem.
This tells us where the top point of the second rectangle will be
Difference in height: 4 - 2√2
Bottom of line when straight: [0, 27√2 + (4 - 2√2)] = [0, 4 + 25√2]
Top of line when straight: [0, 25√2]
Finally we can put in the last rectangle starting at [0, 0]
Translation [0, 25√2] from the point [0, 0]
Rotate -45deg from the point [0, 25√2]
Now that the theory is out of the way, what does this look like in code? It looks similar to the code below
//Values
final static double[] r1Points = {2.828427125, 0}; //Equivilant 2√2
final static double[] r3Points = {0, 35.35533906}; //Equivilant 25√2
final static int[] widthNHeight = {50, 4}; //Width then height
final static double angle = 45.0; //Angle to rotate lines
//Declaring the rectangles
Rectangle2D r1 = new Rectangle2D.Double(r1Points[0], r1Points[1], widthNHeight[0], widthNHeight[1]);
Rectangle2D r3 = new Rectangle2D.Double(r3Points[0], r3Points[1], widthNHeight[0], widthNHeight[1]);
//r1
g2d.rotate(Math.toRadians(angle), r1Points[0], r1Points[1]); //Rotates graphic for first rectangle
g2d.fill(r1);
//r3
g2d.rotate(Math.toRadians(-angle), r1Points[0], r1Points[1]); //Rotates the graphic back to straight
g2d.rotate(Math.toRadians(-angle), r3Points[0], r3Points[1]); //Rotates graphic for second rectangle
g2d.fill(r3);

Rotating Rectangle with Color

I am starting to build a simulation in java and using a rectangle as a plane. But as I rotate the rectangle its foreground color won't stick to it. Can someone help me?
Screenshot:Here
Source Code:
private void drawTransform(Graphics g, double modifier) {
Rectangle rect = new Rectangle(130,350, 350, 15);
AffineTransform at = new AffineTransform();
at.rotate(-Math.toRadians(modifier), rect.getX(), rect.getY() + rect.height);
// Transform the shape and draw it to screen
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.red);
g2d.fillRect(130, 350, 350, 15);
g2d.draw(at.createTransformedShape(rect));
}
Use fill instead of draw:
g2d.fill(at.createTransformedShape(rect));

Java 2D Graphics rotating objects keeping original coords system?

I've already searched this topic everywhere on the Internet, but nothing.
I'm programming with JAVA and I'm painting in a JPanel some shapes like yellow stars with a sort of spaceship in the center of the screen. My purpose is to make the entire scene, apart from the spaceship which remains stationary at the center, rotate around the center (and of course around the spaceship) when I press a certain button.
Now, when I rotate the stars I make them rotate around the center point wich changes as the spaceship moves, but when I do a rotation, the coordinates system changes and the ship moves diagonally, while I want to rotate the scene keeping possible to move the spaceship vertically and horizontally.
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
// RIEMPIMENTO SFONDO
g2.setColor(new Color(10, 10, 10));
g2.fillRect(0, 0, 1366, 768);
// DEBUGGER
g2.setColor(Color.WHITE);
g2.drawString("|" + posx + "|" + posy + "| " + scale, 1250, 758);
// TRASFORMAZIONE NAVICELLA
AffineTransform at = new AffineTransform();
at.translate(683, 384);
at.scale(scale, scale);
g2.setTransform(at);
AffineTransform backup = g2.getTransform();
// DISEGNO NAVICELLA
g2.setColor(Color.LIGHT_GRAY);
int[] xp = {0, -25, -25, 0, 25, 25};
int[] yp = {-25, 0, 25, 0, 25, 0};
g2.drawPolygon(xp, yp, 6);
// TRASFORMAZIONE SPAZIO
at.translate(posx, posy);
at.rotate(Math.toRadians(rotation), -posx, -posy);
g2.setTransform(at);
// DISEGNO SPAZIO
STAR(g2, 300 + posx, 100 + posy, 200, 200, Color.YELLOW);
STAR(g2, -100 + posx, -200 + posy, 200, 200, Color.YELLOW);
g2.dispose();
}

Paint in a part of JPanel without repaint the rest

I'm trying to make a Mastermind in Java. The code isn't really difficult, but I want to have a very good interface. I have a JPanel which take all my JFrame, and I paint this JPanel with surchargind repaint() method:
//method to draw elements on the map
public void paint(Graphics g) {
super.paintComponents(g);
Graphics gr;
gr = MasterMindPane.getGraphics();
img = MasterMindPane.getToolkit().getImage("images/plateau4-8.jpg");
gr.drawImage(img, 0, 0, 600, 720, this);
gr = bouleRougePane.getGraphics();
img = bouleRougePane.getToolkit().getImage("images/bouleRouge.png");
//gr.drawImage(img, 535, 303, 45, 45, this);
gr.drawImage(img, 0, 0, 45, 45, this);
gr = bouleOrangePane.getGraphics();
img = bouleOrangePane.getToolkit().getImage("images/bouleOrange.png");
//gr.drawImage(img, 535, 303, 45, 45, this);
gr.drawImage(img, 0, 0, 45, 45, this);
}
When I click on one image, which have a Panel, I draw a yellow circle like that :
private void bouleRougePaneMouseClicked(java.awt.event.MouseEvent evt) {
Graphics2D g2d = (Graphics2D) MasterMindPane.getGraphics();
for(int i = 0; i<4; i++)
{
g2d.setColor(Color.ORANGE);
g2d.setStroke(new BasicStroke(3));
g2d.drawOval(78+i*70, 106+etape*50, 35, 35);
}
}
And when I select a hole, I want to delete the circle, which only indicates where the gamer can play.
But I don't know how to delete the circle or repaint just a part of my Image because it costs a lot to repaint all.
A very simple way is to use paintImmediately(x,y,w,h);
This repaints only the specified area that starts at pixel (x,y) with width w and height h.
You can set clip Shape to the thick oval and fill it.
Create the big oval Shape via BasicStroke with thick line.

Java awt draw circle border

Using java awt, how to make the code draw a border for a circle. The henceforth code has performance issues, and maybe it would run faster if it just painted the outline.
g.fillOval(gameLogic.getParticleXCoor(i) - 3,
gameLogic.getParticleYCoor(i) - 3,
gameLogic.getParticleSize(i) + 6,
gameLogic.getParticleSize(i) + 6);
g.setColor(gameLogic.getParticleColor(i));
g.fillOval(gameLogic.getParticleXCoor(i),
gameLogic.getParticleYCoor(i),
gameLogic.getParticleSize(i),
gameLogic.getParticleSize(i));
You could try drawOval instead of fillOval.
If you want paint circle use Ellipse2D class:
Ellipse2D.Double circleBorder = new Ellipse2D.Double(x, y, 15, 15);
After just call draw() methid from Graphics2D
g2.draw(circleBorder);
Full code for draw circle icons as example is here:
#Override
public void paintIcon(#Nonnull Component c, #Nonnull Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D) g;
RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
BasicStroke dashed =new BasicStroke(3.0f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER,10.0f);
Ellipse2D.Double circle = new Ellipse2D.Double(x+1, y+1, 14, 14);
Ellipse2D.Double circleBorder = new Ellipse2D.Double(x, y, 15, 15);
g2.setColor(getColor());
g2.setRenderingHints(hints);
g2.fill(circle);
Composite oldComposite=g2.getComposite();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0));
g2.setColor(new Color(1,1,1,1));
g2.setStroke(dashed);
g2.draw(circleBorder);
g2.setComposite(oldComposite);
}

Categories