Create a HistoryPanel class that extends JPanel. The constructor of HistoryPanel takes as argument a model object of type Library, which you need to store in some private instance variable. Add to the HistoryPanel class two private methods called historyMax and historyMin that take an arraylist of integers as argument and return as result the maximum and minimum number in the arraylist, respectively (you can assume that the arraylist contains at least one number). Then add to the HistoryPanel class a private method called historyRange that takes an arraylist of integers as argument and returns as result the difference between the max and min of the integers in the arraylist, or returns as result 10 if the difference between the man and min of the integers in the arraylist is strictly less than 10. Override the protected void paintComponent(Graphics g) method inherited from JPanel, and, inside your new paintComponent method, draw graphically how the total number of books borrowed by all users of the library changes over time, as follows:
时间: 2023-12-10 07:41:32 浏览: 26
Here is the code for the HistoryPanel class:
```java
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JPanel;
public class HistoryPanel extends JPanel {
private Library library;
public HistoryPanel(Library library) {
this.library = library;
}
private int historyMax(ArrayList<Integer> list) {
int max = Integer.MIN_VALUE;
for(int num : list) {
if(num > max) {
max = num;
}
}
return max;
}
private int historyMin(ArrayList<Integer> list) {
int min = Integer.MAX_VALUE;
for(int num : list) {
if(num < min) {
min = num;
}
}
return min;
}
private int historyRange(ArrayList<Integer> list) {
int difference = historyMax(list) - historyMin(list);
if(difference < 10) {
return 10;
}
return difference;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
ArrayList<Integer> history = library.getHistory();
int numPoints = history.size();
int maxValue = historyMax(history);
int minValue = historyMin(history);
int range = historyRange(history);
int xSpacing = getWidth() / numPoints;
int ySpacing = getHeight() / range;
for(int i = 0; i < numPoints - 1; i++) {
int x1 = i * xSpacing;
int y1 = (history.get(i) - minValue) * ySpacing;
int x2 = (i + 1) * xSpacing;
int y2 = (history.get(i + 1) - minValue) * ySpacing;
g.drawLine(x1, getHeight() - y1, x2, getHeight() - y2);
}
}
}
```
In the constructor, we store the Library model object in a private instance variable. The three private methods `historyMax`, `historyMin`, and `historyRange` are used to calculate the maximum, minimum, and range of values in an arraylist of integers.
In the `paintComponent` method, we first retrieve the history data from the Library object and calculate the maximum, minimum, and range of values. We then calculate the spacing between points for both the x and y axes. Finally, we loop through each point in the history and draw a line between it and the next point.
Note that we use the `getHeight` method to adjust the y-coordinates of the points so that they are drawn from the bottom of the panel upwards. This is because the origin (0,0) of the coordinate system in Swing is at the top-left corner of the panel, whereas in most mathematical systems it is at the bottom-left corner.