Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* JAVA-Programm zur Darstellung und Manipulation
- * von Bitmap-Dateien (jpeg etc.)
- * Der User kann eine Datei auswaehlen (FileChooser-Dialog),
- * deren Groesse ermittelt wird und die dann im Rahmen dargestellt wird.
- * Unter dem Bild ist im JPanel noch Platz für JButtons, die bestimmte Bitoperationen
- * ausfueren sollen...
- */
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.imageio.*;
- import java.io.*;
- import java.awt.image.*;
- public class BitmapGrafik_Aufgabe extends JPanel {
- File f;
- Image image;
- BufferedImage bild;
- BufferedImage bild2;
- int breite = 0, hoehe = 0;
- public BitmapGrafik_Aufgabe(File datei) {
- setLayout(null);
- setBackground(Color.cyan);
- f = datei;
- try {
- bild = ImageIO.read(f);
- bild2 = ImageIO.read(f);
- } catch (IOException e) {
- }
- if (bild == null) {
- JOptionPane.showMessageDialog(null,
- "Deine Datei * " + f.getName() + " * kann nicht angezeigt werden!", "Error - Meldung von HTS EPhysik!", JOptionPane.ERROR_MESSAGE);
- System.exit(1);
- } else {
- image = bild;
- breite = bild.getWidth();
- hoehe = bild.getHeight();
- }
- JButton myB01 = new JButton("GBR");
- myB01.setBounds(10, hoehe + 40, 100, 30);
- this.add(myB01);
- myB01.addActionListener(new ActionListener() { // ActionListener für den JButton
- public void actionPerformed(ActionEvent e) {
- //Verschachtelte Schleife: Für alle Stellen [x,y]:
- for (int y = 0; y < hoehe; y++) {
- for (int x = 0; x < breite; x++) {
- //hole RGB-Wert des Pixels[x|y]:
- int pixel = bild.getRGB(x, y);
- //extrahiere Rotwert:
- int r = (pixel >> 16) & 0x000000FF;
- //extrahiere Grünwert:
- int g = (pixel >> 8) & 0x000000FF;
- //extrahiere Blauwert:
- int b = pixel & 0x000000FF;
- //RGB wird GBR
- r = g;
- g = b;
- b = r;
- //berechne RGB-Wert dazu:
- pixel = (r << 16) + (g << 8) + b;
- //setze RGB-Wert des Pixeels[x|y]
- bild.setRGB(x, y, pixel);
- }
- }
- repaint();
- }
- });
- JButton myB02 = new JButton("BRG");
- myB02.setBounds(150, hoehe + 40, 100, 30);
- this.add(myB02);
- myB02.addActionListener(new ActionListener() { // ActionListener für den JButton
- public void actionPerformed(ActionEvent e) {
- //Verschachtelte Schleife: Für alle Stellen [x,y]:
- for (int y = 0; y < hoehe; y++) {
- for (int x = 0; x < breite; x++) {
- //hole RGB-Wert des Pixels[x|y]:
- int pixel = bild.getRGB(x, y);
- //extrahiere Rotwert:
- int r = (pixel >> 16) & 0x000000FF;
- //extrahiere Grünwert:
- int g = (pixel >> 8) & 0x000000FF;
- //extrahiere Blauwert:
- int b = pixel & 0x000000FF;
- //RGB wird GBR
- r = b;
- g = g;
- b = r;
- //berechne RGB-Wert dazu:
- pixel = (r << 16) + (g << 8) + b;
- //setze RGB-Wert des Pixeels[x|y]
- bild.setRGB(x, y, pixel);
- }
- }
- repaint();
- }
- });
- JButton myB03 = new JButton(">>1");
- myB03.setBounds(290, hoehe + 40, 100, 30);
- this.add(myB03);
- myB03.addActionListener(new ActionListener() { // ActionListener für den JButton
- public void actionPerformed(ActionEvent e) {
- //Verschachtelte Schleife: Für alle Stellen [x,y]:
- for (int y = 0; y < hoehe; y++) {
- for (int x = 0; x < breite; x++) {
- //hole RGB-Wert des Pixels[x|y]:
- int pixel = bild.getRGB(x, y);
- pixel = pixel >> 1;
- bild.setRGB(x, y, pixel);
- }
- }
- repaint();
- }
- });
- JButton myB04 = new JButton("<<1");
- myB04.setBounds(430, hoehe + 40, 100, 30);
- this.add(myB04);
- myB04.addActionListener(new ActionListener() { // ActionListener für den JButton
- public void actionPerformed(ActionEvent e) {
- //Verschachtelte Schleife: Für alle Stellen [x,y]:
- for (int y = 0; y < hoehe; y++) {
- for (int x = 0; x < breite; x++) {
- //hole RGB-Wert des Pixels[x|y]:
- int pixel = bild.getRGB(x, y);
- pixel = pixel << 1;
- bild.setRGB(x, y, pixel);
- }
- }
- repaint();
- }
- });
- JButton myB05 = new JButton("Mirror");
- myB05.setBounds(570, hoehe + 40, 100, 30);
- this.add(myB05);
- myB05.addActionListener(new ActionListener() { // ActionListener für den JButton
- public void actionPerformed(ActionEvent e) {
- //Verschachtelte Schleife: Für alle Stellen [x,y]:
- for (int y = 0; y < hoehe; y++) {
- for (int x = 0; x < breite; x++) {
- //hole RGB-Wert des Pixels[x|y]:
- int pixel = bild.getRGB(x, y);
- bild.setRGB(breite - 1 - x, y, pixel);
- }
- }
- repaint();
- }
- });
- JButton myB06 = new JButton("Reverse");
- myB06.setBounds(710, hoehe + 40, 100, 30);
- this.add(myB06);
- myB06.addActionListener(new ActionListener() { // ActionListener für den JButton
- public void actionPerformed(ActionEvent e) {
- //Verschachtelte Schleife: Für alle Stellen [x,y]:
- for (int y = 0; y < hoehe; y++) {
- for (int x = 0; x < breite; x++) {
- //hole RGB-Wert des Pixels[x|y]:
- int pixel = bild.getRGB(x, y);
- bild2.setRGB(breite - 1 - x, y, pixel);
- }
- }
- tauscheBild(bild2, bild);
- repaint();
- }
- });
- JButton myB07 = new JButton("Zweier");
- myB07.setBounds(850, hoehe + 40, 100, 30);
- this.add(myB07);
- myB07.addActionListener(new ActionListener() { // ActionListener für den JButton
- public void actionPerformed(ActionEvent e) {
- int z = 0;
- //Verschachtelte Schleife: Für alle Stellen [x,y]:
- for (int y = 0; y < hoehe; y++) {
- for (int x = 0; x < breite; x++) {
- //hole RGB-Wert des Pixels[x|y]:
- int pixel = bild.getRGB(x, y);
- if (x % 2 == 0) {
- bild2.setRGB(x, y, pixel);
- z++;
- }
- }
- }
- tauscheBild(bild2, bild);
- repaint();
- }
- });
- }
- public void tauscheBild(BufferedImage b, BufferedImage b1) {
- for (int y = 0; y < hoehe; y++) {
- for (int x = 0; x < breite; x++) {
- //hole RGB-Wert des Pixels[x|y]:
- int pixel = bild2.getRGB(x, y);
- bild.setRGB(x, y, pixel);
- }
- }
- }
- public void paintComponent(Graphics g) {
- g.drawImage(image, 5, 5, this);
- }
- public static void main(String[] args) {
- JFileChooser chooser = new JFileChooser();
- chooser.setCurrentDirectory(new File("."));
- chooser.setDialogTitle("Bitte Bild auswählen");
- if (chooser.showOpenDialog(null) == JFileChooser.CANCEL_OPTION) {
- System.exit(1);
- }
- File file = new File(chooser.getCurrentDirectory(), chooser
- .getSelectedFile().getName());
- BitmapGrafik_Aufgabe myGrafik = new BitmapGrafik_Aufgabe(file);
- JFrame myFrame = new JFrame();
- myFrame.setTitle("Gewaehltes Bild: " + file.getName());
- myFrame.setPreferredSize(new Dimension(myGrafik.breite + 18,
- myGrafik.hoehe + 130));
- myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- myFrame.add(myGrafik);
- myFrame.pack();
- myFrame.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement