midterm2: solve 5.2 - improve 5.1
This commit is contained in:
parent
1be8ca6bf4
commit
34a71d5ef7
@ -1,4 +1,5 @@
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.awt.geom.*;
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.*;
|
||||
@ -21,44 +22,27 @@ public class BarFrame extends JFrame implements ChangeListener {
|
||||
setLocation(0, 200);
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
Icon barIcon = new Icon() {
|
||||
public int getIconWidth() {
|
||||
return ICON_WIDTH;
|
||||
}
|
||||
JLabel barLabel = new JLabel();
|
||||
barLabel.setIcon(new BarIcon());
|
||||
barLabel.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
int mouseX = e.getX();
|
||||
int mouseY = e.getY();
|
||||
|
||||
public int getIconHeight() {
|
||||
return ICON_HEIGHT;
|
||||
}
|
||||
double max = a.stream().max(Double::compare).orElse(1.0);
|
||||
double barHeight = ICON_HEIGHT / (double) a.size();
|
||||
|
||||
public void paintIcon(Component c, Graphics g, int x, int y) {
|
||||
Graphics2D g2 = (Graphics2D) g;
|
||||
|
||||
g2.setColor(Color.red);
|
||||
|
||||
double max = (a.get(0)).doubleValue();
|
||||
for (Double v : a) {
|
||||
double val = v.doubleValue();
|
||||
if (val > max)
|
||||
max = val;
|
||||
}
|
||||
|
||||
double barHeight = getIconHeight() / a.size();
|
||||
|
||||
int i = 0;
|
||||
for (Double v : a) {
|
||||
double value = v.doubleValue();
|
||||
|
||||
double barLength = getIconWidth() * value / max;
|
||||
|
||||
Rectangle2D.Double rectangle = new Rectangle2D.Double
|
||||
(0, barHeight * i, barLength, barHeight);
|
||||
i++;
|
||||
g2.fill(rectangle);
|
||||
// Find the nearest bar to the mouse click
|
||||
int index = (int) (mouseY / barHeight);
|
||||
if (index >= 0 && index < a.size()) {
|
||||
double newValue = max * mouseX / ICON_WIDTH;
|
||||
dataModel.update(index, newValue);
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
add(new JLabel(barIcon));
|
||||
add(barLabel);
|
||||
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
pack();
|
||||
@ -75,6 +59,35 @@ public class BarFrame extends JFrame implements ChangeListener {
|
||||
repaint();
|
||||
}
|
||||
|
||||
private class BarIcon implements Icon {
|
||||
public int getIconWidth() {
|
||||
return ICON_WIDTH;
|
||||
}
|
||||
|
||||
public int getIconHeight() {
|
||||
return ICON_HEIGHT;
|
||||
}
|
||||
|
||||
public void paintIcon(Component c, Graphics g, int x, int y) {
|
||||
Graphics2D g2 = (Graphics2D) g;
|
||||
|
||||
g2.setColor(Color.red);
|
||||
|
||||
double max = a.stream().max(Double::compare).orElse(1.0);
|
||||
double barHeight = getIconHeight() / (double) a.size();
|
||||
|
||||
int i = 0;
|
||||
for (Double value : a) {
|
||||
double barLength = getIconWidth() * value / max;
|
||||
|
||||
Rectangle2D.Double rectangle = new Rectangle2D.Double(
|
||||
0, barHeight * i, barLength, barHeight);
|
||||
i++;
|
||||
g2.fill(rectangle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ArrayList<Double> a;
|
||||
private DataModel dataModel;
|
||||
|
||||
|
@ -23,6 +23,7 @@ public class ObserverTester {
|
||||
|
||||
BarFrame barFrame = new BarFrame(model);
|
||||
|
||||
model.attach(frame);
|
||||
model.attach(barFrame);
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,14 @@
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* A class for displaying the model as a column of textfields in a frame.
|
||||
*/
|
||||
public class TextFrame extends JFrame {
|
||||
public class TextFrame extends JFrame implements ChangeListener {
|
||||
/**
|
||||
* Constructs a JFrame that contains the textfields containing the data
|
||||
* in the model.
|
||||
@ -56,6 +58,19 @@ public class TextFrame extends JFrame {
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the data in the model is changed.
|
||||
*
|
||||
* @param e the event representing the change
|
||||
*/
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
ArrayList<Double> a = dataModel.getData();
|
||||
for (int i = 0; i < a.size(); i++) {
|
||||
fieldList[i].setText(a.get(i).toString());
|
||||
}
|
||||
}
|
||||
|
||||
DataModel dataModel;
|
||||
JTextField[] fieldList;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user