[JAVA GUI練習]在JPanel上畫出sin()映像

來源:互聯網
上載者:User

// file: SinPanel.java
import java.awt.*;
import javax.swing.*;

public class SinPanel extends JPanel {
  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Dimension panelSize = this.getSize();
    Location center = new Location(panelSize.width/2, panelSize.height/2);
    int radius = (int)((Math.min(panelSize.width, panelSize.height)/2)*0.9);

    //確定每個點的座標
    int[] x = new int[2*radius+1];
    int[] y = new int[2*radius+1];
    for(int i = 0; i < 2*radius+1; i++) {
      x[i] = center.x - radius + i;
      double y1 = Math.sin(((double)(-radius + i) / radius) * 4 * Math.PI);//這個很重要,sin()裡面必須為double值
      int y2 = (int)(y1 * 100);
      y[i] = center.y - y2;
    }

    g.setColor(Color.black);
    //畫座標軸
    g.drawLine(center.x - radius, center.y, center.x + radius, center.y);
    g.drawLine(center.x, center.y - radius, center.x, center.y + radius);
    g.drawLine(center.x + radius, center.y, center.x + radius - 10, center.y - 7);
    g.drawLine(center.x + radius, center.y, center.x + radius - 10, center.y + 7);
    g.drawLine(center.x, center.y - radius, center.x - 7, center.y - radius + 10);
    g.drawLine(center.x, center.y - radius, center.x + 7, center.y - radius + 10);

    g.drawPolyline(x, y, 2*radius+1);

    g.setColor(Color.red);
    g.setFont(new Font("ScanSerif", Font.BOLD, 12));
    g.drawString("X", center.x + radius, center.y - 10);
    g.drawString("Y", center.x + 10, center.y - radius);
  }
}

============================================================= 
// file: MyFrame.java
import java.awt.*;
import javax.swing.*;

public class MyFrame extends JFrame{
  public MyFrame() {
    Container container = this.getContentPane();
    container.setLayout(new BorderLayout());
    SinPanel sinPanel = new SinPanel();
    container.add(sinPanel, BorderLayout.CENTER);

    this.setTitle("Show Sin Fuction!");
    this.setSize(new Dimension(400,400));
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = this.getSize();
    this.setLocation((screenSize.width - frameSize.width) / 2,
                     (screenSize.height - frameSize.height) / 2);
    this.setVisible(true);
  }

  public static void main(String[] args) {
    MyFrame myFrame = new MyFrame();
  }
}

=========================================================
// file: Location.java
public class Location {
  public int x;
  public int y;

  public Location(int x, int y) {
    this.x = x;
    this.y = y;
  }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.