如何使用Graphics类和fill.rect创建条形图/图形?

时间:2012-10-02 22:51:17

标签: graphics graph

    /* Written by Brandon Colchin */
import javax.swing.*;
import java.awt.*; 
import java.util.*;

public class Bar extends JApplet
{
   public void paint (Graphics x)
   {
      super.paint( x);

        //declare variables
        int Mon, Tues, Wed, Thurs, Fri;

        //create scanner
        System.out.println("Please enter the unrealistic amount of math problems you do each day");
        Scanner bar = new Scanner(System.in);
        Mon = bar.nextInt();
        Tues = bar.nextInt();
        Wed = bar.nextInt();
        Thurs = bar.nextInt();
        Fri = bar.nextInt();


        //create edges
        x.drawLine(50, 50, 50, 300);
        x.drawLine(50, 300, 350, 300);

        //label edges
        x.drawString("100", 25, 60);
        x.drawString("90", 30, 85);
        x.drawString("80", 30, 110);
        x.drawString("70", 30, 135);
        x.drawString("60", 30, 160);
        x.drawString("50", 30, 185);
        x.drawString("40", 30, 210);
        x.drawString("30", 30, 235);
        x.drawString("20", 30, 260);
        x.drawString("10", 30, 290);
        x.drawString("Mon", 65, 315);
        x.drawString("Tues", 130, 315);
        x.drawString("Wed", 195, 315);
        x.drawString("Thurs", 260, 315);
        x.drawString("Fri", 325, 315);


        //draw bars
        x.setColor(Color.BLUE);
        x.fillRect( 60, (Mon), 40, Mon );
        x.setColor(Color.RED);
        x.fillRect( 125, (Tues), 40, Tues );
        x.setColor(Color.CYAN);
        x.fillRect( 190, (Wed), 40, Wed );
        x.setColor(Color.YELLOW);
        x.fillRect( 255, (Thurs), 40, Thurs );
        x.setColor(Color.WHITE);
        x.fillRect( 320, (Fri), 40, Fri );`

输出是一个图表,左边是100-10,底部是Mon-Fri,还有5个矩形。

这是针对编程类的,要求是:轴必须位于图的左侧和底部,5个条的5个数字必须是用户输入的。

我不知道将y点和高度相乘的因素是使图形从图形左侧的相应数字开始(例如,如果用户输入60,图形高度将与60相匹配)图的左侧和底部将停在x轴边缘处)

同样,当我在运行程序后提示输入5个数字时,它会让我输入10个数字,即使我只有5次下一个。奇怪的是前5个数字输入没有,只有最后5个数字用于周一至周五。

需要任何帮助

0 个答案:

没有答案