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