diff options
author | Leonard Kugis <leonard@kug.is> | 2022-04-25 18:36:30 +0200 |
---|---|---|
committer | Leonard Kugis <leonard@kug.is> | 2022-04-25 18:36:30 +0200 |
commit | 84e220b332bfffb0f2dcc39b9697a6fd6691d265 (patch) | |
tree | 0b861a9a099017ffd9bf00bae12e33ed6a309bbe /src/aufgaben/b20160425/Aufgabe3.java |
Diffstat (limited to 'src/aufgaben/b20160425/Aufgabe3.java')
-rwxr-xr-x | src/aufgaben/b20160425/Aufgabe3.java | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/src/aufgaben/b20160425/Aufgabe3.java b/src/aufgaben/b20160425/Aufgabe3.java new file mode 100755 index 0000000..2a2b242 --- /dev/null +++ b/src/aufgaben/b20160425/Aufgabe3.java @@ -0,0 +1,121 @@ +package aufgaben.b20160425;
+
+import java.awt.Color;
+import java.awt.Graphics;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+
+import javax.imageio.ImageIO;
+import javax.swing.JPanel;
+import javax.swing.JSpinner;
+import javax.swing.SpinnerNumberModel;
+import javax.swing.event.ChangeEvent;
+import javax.swing.event.ChangeListener;
+
+import aufgaben.Aufgabe;
+import misc.Utils;
+
+public class Aufgabe3 extends Aufgabe {
+
+ private final int width = 500, height = 500;
+ private boolean done = false;
+ private String name;
+ private BufferedImage image, todraw;
+ private JPanel panel;
+ private JSpinner spinner;
+
+ public Aufgabe3() {
+ name = "Interpolation";
+ this.setSize(width, height);
+ this.setTitle(name);
+ this.addWindowListener(
+ new WindowAdapter() {
+ @Override
+ public void windowClosing(WindowEvent e) {
+ System.out.println("close");
+ done = true;
+ }
+ }
+ );
+ this.setLayout(null);
+ }
+
+ @Override
+ public boolean done() {
+ return done;
+ }
+
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public void init() {
+ panel = new JPanel(){
+ public void paint(Graphics g) {
+ if(todraw != null)
+ g.drawImage(todraw, 0, 0, this.getWidth(), this.getHeight(), this);
+ }
+ };
+ panel.setBounds(0, 100, width, height-100);
+ spinner = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
+ spinner.setBounds(100, 50, 100, 20);
+ spinner.addChangeListener(new ChangeListener() {
+ public void stateChanged(ChangeEvent e) {
+ for(int x = 0; x<image.getWidth(); x++) {
+ for(int y = 0; y<image.getHeight(); y++) {
+ todraw.setRGB(x, y, getDurchschnitt(image, x, y, (int)spinner.getValue()).getRGB());
+ }
+ }
+ panel.repaint();
+ }
+ });
+ try {
+ image = ImageIO.read(new File("title.png"));
+ todraw = ImageIO.read(new File("title.png"));
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ this.add(panel);
+ this.add(spinner);
+ }
+
+ private int getGrayValueImg(int x, int y) {
+ if(image == null) return -1;
+ Color c = new Color(image.getRGB(x, y));
+ if(c.getRed() == c.getGreen() && c.getRed() == c.getBlue())
+ return c.getRed();
+ return -1;
+ }
+
+ private void setGrayValueImgProc(int x, int y, int gray) {
+ if(image == null)
+ System.out.println("err");
+ image.setRGB(x, y, new Color(gray, gray, gray).getRGB());
+ }
+
+ private Color getDurchschnitt(BufferedImage image, int x, int y, int r) {
+ Color color;
+ int[] colori = new int[3];
+ int absoluteX = 0, absoluteY = 0, c = 0;
+ for(int x1 = (-1)*r; x1<=r; x1++) {
+ for(int y1 = (-1)*r; y1<=r; y1++) {
+ absoluteX = x+x1;
+ absoluteY = y+y1;
+ if(absoluteX >= 0 && absoluteX < image.getWidth() && absoluteY >= 0 && absoluteY < image.getHeight()) {
+ color = new Color(image.getRGB(absoluteX, absoluteY));
+ colori[0] += color.getRed();
+ colori[1] += color.getGreen();
+ colori[2] += color.getBlue();
+ c++;
+ }
+ }
+ }
+ return new Color(colori[0]/c, colori[1]/c, colori[2]/c);
+ }
+
+}
|