Follow along with the video below to see how to install our site as a web app on your home screen.
Anmerkung: This feature may not be available in some browsers.
Point2D point=new Point2D(10,10);
Point2D anchor=new Point2D(15,15);
AffineTransform at=new AffineTransform();
at.rotate(90,anchor.getX(),anchor.getY());
point.setAffineTransform(at);
wie schon gesagt einen Punkt rotieren
aber eine frage zu dem code wo stellt das programm den bezug zwischen punkten und grad zahl her
Ein Point p soll um einen double wert degrees um den Ursprung Point u rotieren.
Ja, einen Punkt rotieren :noe: Schreib' in klaren Sätzen,
Also noch klarer kann man es IMO gar nicht beschreibenJa, einen Punkt rotieren Schreib' in klaren Sätzen,
aber wie stellst du den bezug zu dem Punkten daGraue Masse aktivieren - auf Zeile 3 steh 90 - das dürfte die "Grad zahl" wie du sie nennst sein.
AUs dem Kopf etwa sowas
Java:Point2D point=new Point2D.Float(10,10); AffineTransform at=new AffineTransform(); at.rotate(Math.toDegrees(90)); Point2D rotated = new Point2D.Float(0,0); at.transform(pt, rotated);
aber wie stellst du den bezug zu dem Punkten da
Math.toDegree ist sicher falsch ;-) 90 sind schon degrees
Neinnein, ich wollte ja um 5156.62016 Grad drehen :noe:
BTW: Das "SCHIEBEN, DREHEN, ZURÜCKSCHIEBEN" braucht man nicht, dafür gibt's in AffineTransform schon eine Methode, aber... inzwischen hab' ich den Verdacht, dass hier getrollt wird... ???:L
Point2D p = ...
Point2D q = ...
// HIER SOLL p UM q GEDREHT WERDEN
public Point2D rotatePoint(Point2D point,double degrees,Point2D anchor){
AffineTransform transform=new AffineTransform();
transform.rotate(Math.toRadians(degrees),anchor.getX(),anchor.getY());
Point2D rotatedPoint=new Point2D.Float(0, 0);
transform.transform(point,rotatedPoint);
return rotatedPoint;
}
public BufferedImage rotateImage(BufferedImage img,double degrees,Point2D anchor){
AffineTransform transform=new AffineTransform();
transform.rotate(Math.toRadians(degrees),anchor.getX(),anchor.getY());
Graphics2D g=(Graphics2D)img.getGraphics();
BufferedImage copy=img;
g.clearRect(0,0,img.getWidth(),img.getHeight());
g.drawImage(copy,0,0,null);
return img;
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class JumpingJackTest
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BodyPart root = buildBody();
JumpingJackPanel panel = new JumpingJackPanel(root);
createMovement(panel, root);
f.getContentPane().add(panel);
f.setSize(800,800);
f.setVisible(true);
}
});
}
private static BufferedImage createImage(int w, int h, Color color)
{
BufferedImage image = new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setColor(color);
g.fillRect(0,0,w,h);
g.dispose();
return image;
}
private static BodyPart buildBody()
{
BodyPart torso = new BodyPart(400,400, 25,10, createImage(50,100, Color.RED));
BodyPart leftUpperLeg = new BodyPart(10,90, 10,10, createImage(20,100, Color.GREEN));
torso.addChild(leftUpperLeg);
BodyPart rightUpperLeg = new BodyPart(40,90, 10,10, createImage(20,100, Color.GREEN));
torso.addChild(rightUpperLeg);
BodyPart leftLowerLeg = new BodyPart(10,90, 10,10, createImage(20,100, Color.YELLOW));
leftUpperLeg.addChild(leftLowerLeg);
BodyPart rightLowerLeg = new BodyPart(10,90, 10,10, createImage(20,100, Color.YELLOW));
rightUpperLeg.addChild(rightLowerLeg);
return torso;
}
static void createMovement(final JComponent component, final BodyPart root)
{
Thread t = new Thread(new Runnable(){
@Override
public void run()
{
while (true)
{
increaseRotation(root,3);
component.repaint();
try
{
Thread.sleep(20);
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
}
}
}
private void increaseRotation(BodyPart bodyPart, int counter)
{
double rotationDeg = bodyPart.getRotation();
rotationDeg += counter;
bodyPart.setRotation(rotationDeg);
for (BodyPart child : bodyPart.getChildren())
{
increaseRotation(child, counter+3);
}
}
});
t.start();
}
}
class JumpingJackPanel extends JPanel
{
private BodyPart root;
public JumpingJackPanel(BodyPart root)
{
this.root = root;
}
@Override
protected void paintComponent(Graphics gr)
{
super.paintComponent(gr);
Graphics2D g = (Graphics2D)gr;
root.paint(g);
}
}
class BodyPart
{
// The image to be painted
private BufferedImage image;
// The position of this body part relative to its parent
private Point2D anchor;
// The position of the anchor point in the local coordinate
// system (i.e. the point that the image is rotated about)
private Point2D localAnchor;
// The body parts attached to this one
private List<BodyPart> children;
// The current rotation angle of this body part
private double rotationAngleDeg;
public BodyPart(int anchorX, int anchorY, int localAnchorX, int localAnchorY, BufferedImage image)
{
this.anchor = new Point2D.Float(anchorX, anchorY);
this.localAnchor = new Point2D.Float(localAnchorX, localAnchorY);
this.image = image;
this.children = new ArrayList<BodyPart>();
}
public void addChild(BodyPart child)
{
this.children.add(child);
}
public List<BodyPart> getChildren()
{
return Collections.unmodifiableList(children);
}
public void setRotation(double angleDeg)
{
this.rotationAngleDeg = angleDeg;
}
public double getRotation()
{
return rotationAngleDeg;
}
public void paint(Graphics2D g)
{
AffineTransform oldAT = g.getTransform();
g.translate(anchor.getX(), anchor.getY());
g.translate(-localAnchor.getX(), -localAnchor.getY());
g.transform(AffineTransform.getRotateInstance(
Math.toRadians(rotationAngleDeg),
localAnchor.getX(),
localAnchor.getY()));
g.drawImage(image, 0, 0, null);
g.setColor(Color.BLACK);
g.fillOval((int)localAnchor.getX()-2, (int)localAnchor.getY()-2, 4, 4);
for (BodyPart child : children)
{
child.paint(g);
}
g.setTransform(oldAT);
}
}