summaryrefslogtreecommitdiff
path: root/src/aufgaben/b20160425/Aufgabe3.java
blob: 2a2b2428cfbc8218cac8074d5522585787f0dc4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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);
	}

}