midterm2: init 5.1

This commit is contained in:
Yuri Tatishchev 2025-04-16 22:42:26 -07:00
parent 8dd6533d47
commit 1be8ca6bf4
Signed by: CaZzzer
SSH Key Fingerprint: SHA256:sqXB3fe0LMpfH+IeM/vlmxKdso52kssrIJBlwKXVe1U
12 changed files with 311 additions and 0 deletions

29
midterm2/.gitignore vendored Normal file
View File

@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

8
midterm2/.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
</profile>
</component>

6
midterm2/.idea/misc.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="openjdk-22" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
midterm2/.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/midterm2.iml" filepath="$PROJECT_DIR$/midterm2.iml" />
</modules>
</component>
</project>

6
midterm2/.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

11
midterm2/midterm2.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,83 @@
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
/**
* A class that implements an Observer object that displays a barchart view of
* a data model.
*/
public class BarFrame extends JFrame implements ChangeListener {
/**
* Constructs a BarFrame object
*
* @param dataModel the data that is displayed in the barchart
*/
public BarFrame(DataModel dataModel) {
this.dataModel = dataModel;
a = dataModel.getData();
setLocation(0, 200);
setLayout(new BorderLayout());
Icon barIcon = new 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.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));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
/**
* Called when the data in the model is changed.
*
* @param e the event representing the change
*/
public void stateChanged(ChangeEvent e) {
a = dataModel.getData();
repaint();
}
private ArrayList<Double> a;
private DataModel dataModel;
private static final int ICON_WIDTH = 200;
private static final int ICON_HEIGHT = 200;
}

View File

@ -0,0 +1,51 @@
import java.util.ArrayList;
import javax.swing.event.*;
/**
* A Subject class for the observer pattern.
*/
public class DataModel {
/**
* Constructs a DataModel object
*
* @param d the data to model
*/
public DataModel(ArrayList<Double> d) {
data = d;
listeners = new ArrayList<ChangeListener>();
}
/**
* Constructs a DataModel object
*
* @return the data in an ArrayList
*/
public ArrayList<Double> getData() {
return (ArrayList<Double>) (data.clone());
}
/**
* Attach a listener to the Model
*
* @param c the listener
*/
public void attach(ChangeListener c) {
listeners.add(c);
}
/**
* Change the data in the model at a particular location
*
* @param location the index of the field to change
* @param value the new value
*/
public void update(int location, double value) {
data.set(location, new Double(value));
for (ChangeListener l : listeners) {
l.stateChanged(new ChangeEvent(this));
}
}
ArrayList<Double> data;
ArrayList<ChangeListener> listeners;
}

14
midterm2/src/Main.java Normal file
View File

@ -0,0 +1,14 @@
/**
* Write a program that contains two frames, one with a column of text fields
* containing numbers, and another that draws a bar graph showing the values of
* the numbers. When the user edits one of the numbers, the graph should be
* redrawn. Use the observer pattern. Store the data in a model. Attach the graph
* view as a listener. When a number is updated, the number view should update the
* model, and the model should tell the graph view that a change has occured. As a
* result, the graph view should repaint itself.
*/
public class Main {
public static void main(String[] args) {
}
}

View File

@ -0,0 +1,28 @@
import java.util.ArrayList;
/**
* A class for testing an implementation of the Observer pattern.
*/
public class ObserverTester {
/**
* Creates a DataModel and attaches barchart and textfield listeners
*
* @param args unused
*/
public static void main(String[] args) {
ArrayList<Double> data = new ArrayList<Double>();
data.add(33.0);
data.add(44.0);
data.add(22.0);
data.add(22.0);
DataModel model = new DataModel(data);
TextFrame frame = new TextFrame(model);
BarFrame barFrame = new BarFrame(model);
model.attach(barFrame);
}
}

View File

@ -0,0 +1,61 @@
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
/**
* A class for displaying the model as a column of textfields in a frame.
*/
public class TextFrame extends JFrame {
/**
* Constructs a JFrame that contains the textfields containing the data
* in the model.
*
* @param d the model to display
*/
public TextFrame(DataModel d) {
dataModel = d;
final Container contentPane = this.getContentPane();
setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
ArrayList<Double> a = dataModel.getData();
fieldList = new JTextField[a.size()];
// A listener for action events in the text fields
ActionListener l = new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Figure out which field generated the event
JTextField c = (JTextField) e.getSource();
int i = 0;
int count = fieldList.length;
while (i < count && fieldList[i] != c)
i++;
String text = c.getText().trim();
try {
double value = Double.parseDouble(text);
dataModel.update(i, value);
} catch (Exception exc) {
c.setText("Error. No update");
}
}
};
final int FIELD_WIDTH = 11;
for (int i = 0; i < a.size(); i++) {
JTextField textField = new JTextField(a.get(i).toString(), FIELD_WIDTH);
textField.addActionListener(l);
add(textField);
fieldList[i] = textField;
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
DataModel dataModel;
JTextField[] fieldList;
}