用Java在指定点进行绘画

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

以下是代码。

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

可以执行。

广告
将在 10 秒后关闭
bannerAds