如何在这个例子中绘制一个条形图?

问题描述 投票:0回答:1

在本练习中,当您输入城市名称及其人口时,在窗口中必须绘制一个条形图,表示该城市的人口,如图所示,练习几乎完成但不起作用,有人可以告诉我我做错了什么?,为什么没有画出条形图?这使得练习对我来说有点困难的条件是如果窗口大小改变,条形和线条的大小必须改变。由于已经给出了主类,因此无法修改主类。

enter image description here

enter image description here

package Hw02_Statistics;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import hw01_clocknumbers.DigitalNumber;

public class Statistics extends JFrame {

    private JLabel              city, population, other;
    private JTextField          tcity, tpopulation;
    private JButton             add;
    private JTable              table;
    private DefaultTableModel   tablem;
    private int index=1;
    private bar bars;


    public Statistics() {
        setTitle("Statistics");
        setSize(500, 350);
        setupWidgets();
        seupEvents();
        setVisible(true);   

    }

    private void setupWidgets() {

        bars = new bar();       
        city =          new JLabel("City",JLabel.LEFT);
        population =    new JLabel("Population",JLabel.LEFT);
        tcity =         new JTextField();
        other =         new JLabel();
        tpopulation =   new JTextField();
        add =           new JButton("Add");
        tablem =        new DefaultTableModel(new Object[] {"Index", "City", "Population"}, 0);
        table =         new JTable(tablem);
        JPanel norte =  new JPanel(new GridLayout(5,1));
        JPanel sur =    new JPanel(new GridLayout(1,2));

        add(norte, BorderLayout.NORTH);
        add(sur, BorderLayout.CENTER);

        norte.add(city);
        norte.add(tcity);
        norte.add(population);
        norte.add(tpopulation);
        norte.add(add);
        sur.add(new JScrollPane(table));
        sur.add(bars);          
    }

    private void seupEvents() {

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        add.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                if (!tcity.getText().equals("") && !tpopulation.getText().equals("")) {

                    Object rowinfo[]    = {index, tcity.getText(), tpopulation.getText()}; 
                    tablem.addRow(rowinfo); 
                    int n= Integer.parseInt(tpopulation.getText());
                    bars.setScale(1, index);    
                    tcity.setText("");
                    tpopulation.setText("");
                    index++;
                }   
            }
        });

    }

    public static void main(String[] args) {
        new Statistics();
    }
}

这是我使用的第二堂课

package Hw02_Statistics;

import java.awt.Graphics;
import java.awt.GridLayout;

import javax.swing.JComponent;

import hw01_clocknumbers.DigitalNumber;

public class bar extends JComponent {

    private int  digit;
    private Integer scale=0;
    private int index;
    private rect    rects[];

    public bar () {

    }

    public void paint(Graphics g) {
        int w   =getWidth();
        int h   =getHeight();

        rects   = new rect[4];

        rects[index] = new rect(scale, index, w, h);

//      System.out.println(w); 243
//      System.out.println(h); 183

        g.drawLine(w/12, h/9, w/12, 8*h/9);
        g.drawLine(w/12, 8*h/9, 12*w/12, 8*h/9);

    }

    public void setScale(Integer scale, int index) {
        this.index=index-1;
        this.scale=scale;
        repaint();

    }
}

这是我使用的最后一个类,它是不起作用的类

package Hw02_Statistics;

import java.awt.Graphics;

import javax.swing.JComponent;

public class rect extends JComponent{

    private int scale;
    private int index;
    private int w, h;

    public rect(int scale, int index, int w, int h) {
        this.scale = scale;
        this.index= index;
        this.w =w;
        this.h=h;       
    }

    public void paint(Graphics e) {

        e.fillRect(6*w/12+w*index/12,h/9,scale*w/12,scale*7*h/9);
        System.out.println("Aaaa");

        repaint();

    }
}
java swing actionlistener jcomponent custom-painting
1个回答
1
投票

class bar(根据java命名约定应该称为Bar)被重命名为Bars并更改为保持并绘制所有条形:

class Bars extends JComponent {
    //collection of bar heights
    private final List<Integer>  barsValues;
    private static final int W = 20; //bar width
    public Bars () {
        barsValues   = new ArrayList<>();
    }

    //override paintComponent rather than paint
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g); //always call super
        int w  = getWidth();
        int h  = getHeight();
        //use double to avoid rounding errors
        double minX = w/12 , maxX = 12*minX, maxY = h/9, minY = 8 * maxY;
        double graphHeight = minY - maxY;
        //draw axis
        g.drawLine((int)minX, (int)maxY, (int)minX, (int)minY);
        g.drawLine((int)minX, (int)minY, (int)maxX, (int)minY);

        //draw bars
        if(barsValues.size() == 0) return;
        double x = minX + W ;
        int maxHeight =  getMaxHeight(); //calculate scale based on max height
        double scale = maxHeight > 0 ?  graphHeight / getMaxHeight() : 1;
        for(int barValue : barsValues){
            double barHeight = scale*barValue;
            g.fillRect((int)x ,(int)(minY - barHeight), W, (int)barHeight);
            x += 2*W;
        }
    }

    //add bar values. valid values should be > 0
    void addBar(int height) {
        barsValues.add(height);
        repaint();
    }

    //get max height
    int getMaxHeight(){
        int max = 0;
        for (int value : barsValues){
            if(value > max) {
                max = value;
            }
        }
        return max;
    }
}

不需要课程rect

要测试你需要在qazxsw poi做一些小改动: 将qazxsw poi改为qazxsw poi; 由Statistics初始化它 并将动作侦听器中的一行从private bar bars;更改为private Bars bars

完整的代码可以从bars = new Bars();复制粘贴

© www.soinside.com 2019 - 2024. All rights reserved.