JAVA applent小程式 畫座標系 用隨機數產生曲線圖的點(有bug版)

來源:互聯網
上載者:User



這是有bug版,產生(x,y)點的時候忘了考慮x是越來越大的,即x的值是沿著正方向變大的


import java.awt.*;

import java.applet.Applet;

import java.util.Random;


public class j18draw extends Applet

{

    final int LENGTH = 50;

    final int MIN_X  = 10;

    final int MAX_X  = 450;

    final int MIN_Y  = 10;

    final int MAX_Y  = 290;


    int pointsX[];

    int pointsY[];



    public void init()

    {

        pointsX = new int[LENGTH];

        pointsY = new int[LENGTH];

        Random rdX = new Random();

        Random rdY = new Random();


        for(int i = 0; i < LENGTH; ++i)

        {

            int temp = rdX.nextInt(MAX_X + 1);                                                                                                                   //MAX_X + 1是因為nextInt(int value)產生的隨機數區間   為[0, value -1]

        pointsX[i] = temp < MIN_X ? temp + 10 : temp;   //產生X,


        temp = rdY.nextInt(MAX_Y + 1);

        pointsY[i] = temp < MIN_Y ? temp + 10 : temp;

        }

    }


    public void start()

    { 

        repaint();

    }


    public void paint(Graphics g)

    {

        g.drawLine(10, 150, 450, 150);      //橫軸

        g.drawLine(10, 290, 10, 10);        //縱軸


        for(int i = 0; i < (LENGTH - 1); ++i)

        {

            //把相鄰的兩個點連起來

            g.drawLine(pointsX[i], pointsY[i], pointsX[i + 1], pointsY[i + 1]);

        }

    }

}



------------------------------------------------------------------------------------------

在HTML中使用

<html>

<head>

<title>

JAVA小程式 畫座標系

</title>

</head>


<body>

<applet code = "j18draw.class"  width = 500  height = 300>

</applet>


</body>


</html>


在瀏覽器中的效果如所示:

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.