gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:13:15 25 4
gpt4 key购买 nike

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

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();

}
}

最佳答案

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;
}
}
不需要

class rect

要进行测试,您需要对统计信息进行一些细微的更改:
private bar bar; 更改为 private Bars bar;
通过 bars = new Bars(); 初始化它
并将操作监听器中的一行从 bars.setScale(1, index) 更改为 bars.addBar(n);

完整代码可以从 here 复制粘贴

关于java - 在这个例子中如何绘制一个条形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54849017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com