1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

java 回転行列

Last updated at Posted at 2015-11-27

今回は回転行列を作成しました。
前回はキャストを使って円を表示していましたが、今回上手くならなかったので、キャスト自体は使わずにdouble型を使って作成しました。

下のは実行した時の画像です。
スクリーンショット 2015-11-27 11.48.34.png

下のがコードです。

Graphics2DTest1.java

import javax.swing.*;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.geom.Line2D;

public class Graphics2DTest1 extends JPanel{
	double x = 1;
	double y = 400;
	
	double r = 100;
	double cx, cy;
	double rx,ry;
	
	
	double movex;
	double movey;
	
	double theta = 30;

  public static void main(String[] args){
    JFrame frame = new JFrame();

    Graphics2DTest1 app = new Graphics2DTest1();
    frame.setBackground(Color.BLACK); 
    frame.getContentPane().add(app);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(0, 0, 400, 400);
    frame.setTitle("タイトル");
    frame.setVisible(true);
    Insets insets = frame.getInsets();
    frame.setSize(400 + insets.left + insets.right, 400 + insets.top + insets.bottom);
  }

  public void paintComponent(Graphics g){
	 Graphics2D g2 = (Graphics2D)g;
	 g.setColor(Color.white);
	 g.drawLine(0,200,400,200);
	 g.drawLine(200,0,200,400);
	 //array(g);
	 //Circle(g);
	 //oval(g);
	 rotation(g2);
  }
  
  public void array(Graphics2D g){
	  for(int i = 0; i <= 400; i++){
		  g.draw(new Line2D.Double(x + i ,y - i,x + i,y - i));
		  movex += 1;
		  movey -= 1;
	  }
   }
   
  
  public void Circle(Graphics2D g){
	  for(int i = 0; i < 360; i++){
		  cy = (int) (200 + Math.sin(i) * r);
		  cx = (int) (200 + Math.cos(i) * r);
		  g.draw(new Line2D.Double(cx, cy, cx, cy));
	  }
  }
  
  public void oval(Graphics2D g){
	  for(int i = 0; i < 360; i++){
		  cy = (int) (200 + Math.sin(i) * r / 2);
		  cx = (int) (200 + Math.cos(i) * r);
		  g.draw(new Line2D.Double(cx, cy, cx, cy));
	  }
  }
  
  public void rotation(Graphics2D g){
	  g.setColor(Color.yellow);
	  for(int i = 0; i < 12; i++){
		  double rx = 200 + (r * Math.cos((theta * i) / 180 * Math.PI));
		  double ry = 200 + (r * Math.sin((theta * i) / 180 * Math.PI));
		  g.draw(new Line2D.Double(rx, ry, rx, ry));
	  //g2.draw(new Line2D.Double(x, y, x, y));
		  System.out.println(i + "回目");
		  System.out.println("rx" + rx);
		  System.out.println("ry" + ry);
	  }
   }
}

作成環境 eclipse

ターミナルで実行する場合は下のコマンドを打ってください。
$ javac Graphics2DTest1.java
$ java Graphics2DTest1

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?