LoginSignup
3
4

More than 5 years have passed since last update.

java 点での描画

Last updated at Posted at 2015-11-26

点での描画を今回しました。画像では点ですが、drawLineを使って点を描画しています。
スクリーンショット 2015-11-26 14.48.43.png

下はコードです。

Graphics2DTest1.java
import javax.swing.*;
import java.awt.Graphics;
import java.awt.Insets;

public class Graphics2DTest1 extends JPanel{
    int x = 1;
    int y = 400;

    int r = 100;
    int cx, cy;


    int movex;
    int movey;

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

    Graphics2DTest1 app = new Graphics2DTest1();
    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){
     g.drawLine(0,200,400,200);
     g.drawLine(200,0,200,400);
     array(g);
     Circle(g);
     oval(g);
  }

  public void array(Graphics g){
      for(int i = 0; i <= 400; i++){
          g.drawLine(x + i ,y - i,x + i,y - i);
          movex += 1;
          movey -= 1;
      }
   }


  public void Circle(Graphics g){
      for(int i = 0; i < 360; i++){
          cy = (int) (200 + Math.sin(i) * r);
          cx = (int) (200 + Math.cos(i) * r);
          g.drawLine(cx, cy, cx, cy);
      }
  }

  public void oval(Graphics g){
      for(int i = 0; i < 360; i++){
          cy = (int) (200 + Math.sin(i) * r / 2);
          cx = (int) (200 + Math.cos(i) * r);
          g.drawLine(cx, cy, cx, cy);
      }
  }
}

キャストをして、すべてint型で作っています。点のズレなどはそのせいです。

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

で実行可能です。

3
4
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
3
4