简体   繁体   English

平移,缩放,旋转阴阳符号

[英]Translate, Scale, Rotate Yin Yang Symbol

I need help with Translating, Rotating, and Scaling a Yin-Yang Symbol in Java. 我需要有关在Java中翻译,旋转和缩放阴阳符号的帮助。 Can anyone provide suggestions I'll post the code below. 谁能提供建议,我将在下面的代码中发布。 I'm trying to use affine transformation does anyone have a way to make sure every part follows suit when I decide to move rotate, translate, scale? 我正在尝试使用仿射变换,当我决定移动旋转,平移,缩放时,是否有人能确保每个部分都跟得上?

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D; 
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;


public class ScaleTranslationRotation extends JFrame
{

 public void paint(Graphics g){
      super.paint(g);
      Graphics2D g2d = (Graphics2D) g;
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

       int xLeft,yUpper,width, height;
       xLeft = this.getInsets().left;
       yUpper = this.getInsets().top;
       width = this.getWidth()-xLeft-this.getInsets().right;
       height = this.getHeight()-this.getInsets().top-this.getInsets().bottom;




      int xCenter = getWidth() / 2;
      int yCenter = getHeight() / 2;
      int radius = (int)(Math.min(getWidth(), getHeight()) * 0.2);
      int x = xCenter - radius;
      int y = yCenter - radius;

      AffineTransform yUp = new AffineTransform();

      g.setColor(Color.blue);
      g.fillOval(x, y, 2 * radius, 2 * radius);
      g.setColor(Color.cyan);
      g.drawOval(x, y, 2 * radius, 2 * radius);
      g.setColor(Color.cyan);
      g.fillArc(x, y, 2 * radius, 2 * radius, 270, 180);
      g.setColor(Color.blue);
      g.fillOval(x + (radius / 2), y, radius, radius);
      g.setColor(Color.cyan);
      g.fillOval(x + (radius / 2), y + radius, radius, radius);
      g.setColor(Color.cyan);
      g.fillOval(x + radius - (radius / 4), y + radius / 4, radius / 2, radius / 2);
      g.setColor(Color.blue);
      g.fillOval(x + radius - (radius / 4), y + radius + radius / 4, radius / 2, radius / 2);


  g2d.setStroke(new BasicStroke(1.0f));
  g.setColor(Color.red);
  drawSimpleCoordinateSystem(width,height, g2d);
  grid(width, height, g2d);
}

 public static void drawSimpleCoordinateSystem(int x, int y, Graphics2D g2d){

      int xOffset = 30;
      int yOffset = 50;
      int step = 20;

      String s;
      //Remember the actual font.
      Font fo = g2d.getFont();
      //Use a small font.
      g2d.setFont(new Font("sansserif",Font.PLAIN,9));
      //x-axis.
      g2d.drawLine(xOffset,yOffset,x,yOffset);
//Marks and labels for the x-axis.
  for (int i=xOffset+step; i<=x; i=i+step)
  {
   g2d.drawLine(i,yOffset-2,i,yOffset+2);
   g2d.drawString(String.valueOf(i),i-7,yOffset-7);
  }

//y-axis.
      g2d.drawLine(xOffset,yOffset,xOffset,y);

      //Marks and labels for the y-axis.
      s="  "; //for indention of numbers < 100
      for (int i=yOffset+step; i<=y; i=i+step){
      g2d.drawLine(xOffset-2,i,xOffset+2,i);
      if (i>99){s="";}
      g2d.drawString(s+String.valueOf(i),xOffset-25,i+5);
}

//Reset to the original font.
      g2d.setFont(fo);
}


 public void grid(int x, int y, Graphics2D g2d){
      int xOffset = 30;
      int yOffset = 50;
      int step = 20;
      g2d.drawLine(xOffset, yOffset, x, yOffset);
     for(int i = xOffset+step; i<= x; i = i+step){
         g2d.drawLine(i, yOffset, i, y);
     }
     g2d.drawLine(xOffset, yOffset, xOffset, y);
     for(int i = yOffset+step; i<=y; i = i+step){
     g2d.drawLine(xOffset, i, x, i);
     }

 }


public static void main(String[] args)
 {
  ScaleTranslationRotation f = new ScaleTranslationRotation();
  f.setTitle("STR");
  f.setSize(1920,1020);
  f.setVisible(true);
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
 }

Just put your code yin-yang symbol code in a method 只需将您的代码阴阳符号代码放在一个方法中

    private void drawSymbol(int x, int y, int radius, Graphics g) {

      g.setColor(Color.blue);
      g.fillOval(x, y, 2 * radius, 2 * radius);
      g.setColor(Color.cyan);
      g.drawOval(x, y, 2 * radius, 2 * radius);
      g.setColor(Color.cyan);
      g.fillArc(x, y, 2 * radius, 2 * radius, 270, 180);
      g.setColor(Color.blue);
      g.fillOval(x + (radius / 2), y, radius, radius);
      g.setColor(Color.cyan);
      g.fillOval(x + (radius / 2), y + radius, radius, radius);
      g.setColor(Color.cyan);
      g.fillOval(x + radius - (radius / 4), y + radius / 4, radius / 2, radius / 2);
      g.setColor(Color.blue);
      g.fillOval(x + radius - (radius / 4), y + radius + radius / 4, radius / 2, radius / 2);

}

And then use an Affine transformation before calling the method to perform the requested transformation. 然后在调用该方法执行请求的转换之前,使用Affine转换。 In the following I draw the symbol three times with different transformation Remember to restore the original affine transformation to the initial value: 在下面的代码中,我用不同的变换绘制了三次符号,记住将原始仿射变换恢复为初始值:

    drawSymbol(x,y,radius,g);
    //save original transformation
    AffineTransform oldt=((Graphics2D) g).getTransform();
    //create new transformation and apply it
    AffineTransform yUp = new AffineTransform(); 
    yUp.translate(0,-100);
    ((Graphics2D) g).setTransform(yUp);
    drawSymbol(x,y,radius,g);


    //create anothernew transformation and apply it
    yUp = new AffineTransform();
    yUp.translate(-100,0);
    ((Graphics2D) g).setTransform(yUp);
    drawSymbol(x,y,radius,g);

    //restore old transforamtion
    ((Graphics2D) g).setTransform(oldt);

    g2d.setStroke(new BasicStroke(1.0f));
    g.setColor(Color.red);
    drawSimpleCoordinateSystem(width,height, g2d);
    grid(width, height, g2d);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM