用Java画线

我怎样才能像这样绘制Java图形?


在此处输入图片说明


这是我的代码,必须至少绘制此图的一半


import java.awt.Color;

import java.awt.Graphics;


import javax.swing.JComponent;

import javax.swing.JFrame;


public class Main {

    public static void main(String[] a) {

        JFrame window = new JFrame();

        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        window.setBounds(30, 30, 300, 300);

        window.getContentPane().add(new MyCanvas());

        window.setVisible(true);

      }

}


class MyCanvas extends JComponent {

    private static final long serialVersionUID = 1L;


    public void paint(Graphics g) {

        int i =0;


        for ( i = 0; i < 100; i++) {

            int x=1+i*3;

            g.drawLine(x, 200, 2+(x+(i/2)), 400-((i*i)/20));

        }

    }

}

我得到这个。


胡子哥哥
浏览 386回答 3
3回答

繁花不似锦

这是我想出的,虽然没什么不同:)&nbsp; &nbsp; public void paint(Graphics g) {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 100; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int x = 1 + i * 3;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.drawLine(x, 200, x + i, 400 - i * i / 20);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.drawLine(600 - x, 200, 600 - (x + i), 400 - i * i / 20);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }我们需要重新处理功能'400-i * i / 20'。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java