1
2

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 2D 画面処理

Last updated at Posted at 2015-11-26

Java2Dの画面処理を行いました。

処理内容

・縦線、横線、斜め線
・円
・楕円

スクリーンショット 2015-11-26 18.14.45.png

コード

package J3D;

import javax.swing.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.geom.Line2D;
import java.awt.Color;

public class J3D extends JPanel{

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

    J3D app = new J3D();
    frame.getContentPane().add(app);

    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;
	
	int x = 1,y = 1;
	
	g2.draw(new Line2D.Double(200,0,200,400));
	g2.draw(new Line2D.Double(0,200,400,200));
	
	drawCircle(g2);
	drawEllipse(g2);
	for(int i = 0;i < 400; i++){
		g2.draw(new Line2D.Double(0 - (x * -i) ,400 - (y * i),200 - (x * -i),200 - (y * i)));
	}

    g.setColor(Color.blue);
   // g.drawString("Hello Java2D", 10, 50);
  }
  public void drawCircle(Graphics2D g2){
      double x , y , cx = 200, cy = 200, r = 100;
      for(int i = 0; i < 820; i++){
          x = r * Math.sin(i) + cx;
          y = r * Math.cos(i) + cy;
          g2.draw(new Line2D.Double(x, y, x + 0.1, y + 0.1));
      }
  }
  public void drawEllipse(Graphics2D g2){
      double x , y , cx = 200, cy = 200, r = 100;
      for(int i = 0; i < 820; i++){
          x = r * Math.sin(i) + cx;
          y = r * Math.cos(i) / 2 + cy;
          g2.draw(new Line2D.Double(x, y, x + 0.1, y + 0.1));
      }
  }
}

実行環境
・Mac version 10.10.2
・eclipse

1
2
1

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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?