LoginSignup
1
0

More than 5 years have passed since last update.

Java2D 画面描画

Last updated at Posted at 2015-11-27

Java2Dでの画面描画を行いました。

y=xの直線
r=100の円
r=100の楕円

スクリーンショット 2015-11-27 11.41.54.png

コード

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

class Test extends JPanel{

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

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

            frame.setBackground(Color.white);
            frame.setResizable(false);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setBounds(0, 0, 400, 400);
            frame.setTitle("2D描画");
            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;
            g2.setColor(Color.black);

            g.drawLine(200,  0,200,400);
            g.drawLine(  0,200,400,200);

            drawPoint(g2);
            drawCircle(g2);
            drawEllipse(g2);
          }

          public void drawPoint(Graphics2D g2){
              int x=400, y=0; 
              for(int i=0; i<400; i++){
                  g2.fillRect(x, y, 1, 1);
                  x -= 1;
                  y += 1;
              }
          }

          public void drawCircle(Graphics2D g2){
              double x, y, cx=200, cy=200, r=100;
              for(int i = 0; i < 720; 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<720; i++){
                  x = r*Math.sin(i)+cx;
                  y = (r*Math.cos(i))/2+cy;
                  g2.draw(new Line2D.Double(x, y, x, y));
              }
          }
}

開発環境
Mac version 10.10.1

実行方法
Eclipseでインポートさせて実行してください。

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