diff --git a/midterm1/.gitignore b/midterm1/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/midterm1/.gitignore @@ -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 \ No newline at end of file diff --git a/midterm1/.idea/.gitignore b/midterm1/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/midterm1/.idea/.gitignore @@ -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 diff --git a/midterm1/.idea/inspectionProfiles/Project_Default.xml b/midterm1/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..03d9549 --- /dev/null +++ b/midterm1/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/midterm1/.idea/misc.xml b/midterm1/.idea/misc.xml new file mode 100644 index 0000000..81a8e88 --- /dev/null +++ b/midterm1/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/midterm1/.idea/modules.xml b/midterm1/.idea/modules.xml new file mode 100644 index 0000000..17b90b3 --- /dev/null +++ b/midterm1/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/midterm1/.idea/vcs.xml b/midterm1/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/midterm1/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/midterm1/midterm1.iml b/midterm1/midterm1.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/midterm1/midterm1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/midterm1/src/Animation.java b/midterm1/src/Animation.java new file mode 100644 index 0000000..664320b --- /dev/null +++ b/midterm1/src/Animation.java @@ -0,0 +1,30 @@ +/** + * Animation Program that draws and moves a simple shape (e.g. butterfly, kite, etc.) on a timer event. + */ +public class Animation { + public static void main(String[] args) { + javax.swing.SwingUtilities.invokeLater(() -> { + javax.swing.JFrame frame = new javax.swing.JFrame("Animation"); + frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); + frame.setSize(400, 400); + + javax.swing.JPanel panel = new javax.swing.JPanel() { + @Override + protected void paintComponent(java.awt.Graphics g) { + super.paintComponent(g); + g.setColor(java.awt.Color.RED); + g.fillOval(getX(), getY(), 30, 30); + } + }; + frame.add(panel); + + javax.swing.Timer timer = new javax.swing.Timer(100, (e) -> { + panel.setLocation((panel.getX() + 1) % 20, (panel.getY() + 1) % 20); + panel.repaint(); + }); + timer.start(); + + frame.setVisible(true); + }); + } +} diff --git a/midterm1/src/C4Q14.java b/midterm1/src/C4Q14.java new file mode 100644 index 0000000..1341a6a --- /dev/null +++ b/midterm1/src/C4Q14.java @@ -0,0 +1,58 @@ +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +/** + * Write a program that shows a frame with three buttons labeled “Red”, + * “Green”, and “Blue”, and a label containing an icon showing a circle that is + * initially red. As the user clicks the buttons, the fill color of the circle should + * change. When you change the color, you need to invoke the repaint + * method on the label. The call to repaint ensures that the paintIcon method + * is called so that the icon can be repainted with the new color. + */ +public class C4Q14 { + public static void main(String[] args) { + javax.swing.SwingUtilities.invokeLater(() -> { + javax.swing.JFrame frame = new javax.swing.JFrame("C4Q14"); + frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); + frame.setSize(400, 400); + // frame.setLayout(new java.awt.BorderLayout()); + + javax.swing.JLabel label = new javax.swing.JLabel(new CircleIcon(java.awt.Color.RED, 300)); + frame.add(label, java.awt.BorderLayout.CENTER); + + javax.swing.JPanel panel = new javax.swing.JPanel(); + panel.setLayout(new java.awt.FlowLayout()); + + javax.swing.JButton redButton = new javax.swing.JButton("Red"); + redButton.addActionListener((e) -> { + label.setIcon(new CircleIcon(java.awt.Color.RED, 300)); + label.repaint(); + }); + panel.add(redButton); + + javax.swing.JButton greenButton = new javax.swing.JButton("Green"); + greenButton.addActionListener((e) -> { + label.setIcon(new CircleIcon(java.awt.Color.GREEN, 300)); + label.repaint(); + }); + greenButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + label.setIcon(new CircleIcon(java.awt.Color.GREEN, 300)); + label.repaint(); + } + }); + panel.add(greenButton); + + javax.swing.JButton blueButton = new javax.swing.JButton("Blue"); + blueButton.addActionListener((e) -> { + label.setIcon(new CircleIcon(java.awt.Color.BLUE, 300)); + label.repaint(); + }); + panel.add(blueButton); + + // frame.add(panel, java.awt.BorderLayout.SOUTH); + frame.setVisible(true); + }); + } +} diff --git a/midterm1/src/C4Q6.java b/midterm1/src/C4Q6.java new file mode 100644 index 0000000..826a3af --- /dev/null +++ b/midterm1/src/C4Q6.java @@ -0,0 +1,29 @@ +import java.util.ArrayList; +import java.util.Comparator; + +public class C4Q6 { + public static void main(String[] args) { + ArrayList a = new ArrayList<>(10); + a.add("a"); + a.add("aa"); + a.add("aaa"); + a.add("aaaaa"); + a.add("aaaa"); + + String max = maximum(a, (s1, s2) -> { + return Integer.compare(s1.length(), s2.length()); + }); + + System.out.println(max); + } + + public static String maximum(ArrayList a, Comparator c) { + String max = a.getFirst(); + for (String s : a) { + if (c.compare(s, max) > 0) { + max = s; + } + } + return max; + } +} \ No newline at end of file diff --git a/midterm1/src/C4Q9.java b/midterm1/src/C4Q9.java new file mode 100644 index 0000000..bfae5ce --- /dev/null +++ b/midterm1/src/C4Q9.java @@ -0,0 +1,21 @@ +import java.util.Arrays; +import java.util.stream.Stream; + +public class C4Q9 { + public static void main(String[] args) { + String[] a = {"a", "aa", "aaa", "aaaa", "aaaaa"}; + String[] res = filter(a, (s) -> { + return s.length() <= 3; + }); + + for (String s : res) { + System.out.println(s); + } + } + + public static String[] filter(String[] a, Filter f) { + return Arrays.stream(a).filter((s) -> { + return f.accept(s); + }).toArray(String[]::new); + } +} diff --git a/midterm1/src/CircleIcon.java b/midterm1/src/CircleIcon.java new file mode 100644 index 0000000..88059c4 --- /dev/null +++ b/midterm1/src/CircleIcon.java @@ -0,0 +1,29 @@ +import javax.swing.*; +import java.awt.*; + +public class CircleIcon implements Icon { + private Color color; + private int size; + + public CircleIcon(Color color, int size) { + this.color = color; + this.size = size; + } + + public void paintIcon(Component c, Graphics g, int x, int y) { + g.setColor(color); + g.fillOval(x, y, size, size); + } + + public int getIconWidth() { + return size; + } + + public int getIconHeight() { + return size; + } + + public void setColor(Color color) { + this.color = color; + } +} diff --git a/midterm1/src/Filter.java b/midterm1/src/Filter.java new file mode 100644 index 0000000..60532f0 --- /dev/null +++ b/midterm1/src/Filter.java @@ -0,0 +1,3 @@ +public interface Filter { + boolean accept(String x); +} diff --git a/midterm1/src/MidtermQ1.java b/midterm1/src/MidtermQ1.java new file mode 100644 index 0000000..8d86e56 --- /dev/null +++ b/midterm1/src/MidtermQ1.java @@ -0,0 +1,24 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.geom.Ellipse2D; +import java.util.ArrayList; + +/** + * + */ +public class MidtermQ1 { + public static void main(String[] args) { + + } +} + +class CirclePanel extends JPanel { + private Color color; + + public void paintComponent(Graphics g) { + Graphics2D g2 = (Graphics2D) g; + g2.setColor(color); + g2.draw(new Ellipse2D.Double(0, 0, 100, 100)); + + } +} \ No newline at end of file