Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.18 KB | None | 0 0
  1. /* JAVA-Programm zur Darstellung und Manipulation
  2. * von Bitmap-Dateien (jpeg etc.)
  3. * Der User kann eine Datei auswaehlen (FileChooser-Dialog),
  4. * deren Groesse ermittelt wird und die dann im Rahmen dargestellt wird.
  5. * Unter dem Bild ist im JPanel noch Platz für JButtons, die bestimmte Bitoperationen
  6. * ausfueren sollen...
  7. */
  8.  
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. import javax.swing.*;
  12. import javax.imageio.*;
  13. import java.io.*;
  14. import java.awt.image.*;
  15.  
  16. public class BitmapGrafik_Aufgabe extends JPanel {
  17.  
  18. File f;
  19. Image image;
  20. BufferedImage bild;
  21. BufferedImage bild2;
  22. int breite = 0, hoehe = 0;
  23.  
  24. public BitmapGrafik_Aufgabe(File datei) {
  25. setLayout(null);
  26. setBackground(Color.cyan);
  27.  
  28. f = datei;
  29. try {
  30. bild = ImageIO.read(f);
  31. bild2 = ImageIO.read(f);
  32. } catch (IOException e) {
  33. }
  34. if (bild == null) {
  35. JOptionPane.showMessageDialog(null,
  36. "Deine Datei * " + f.getName() + " * kann nicht angezeigt werden!", "Error - Meldung von HTS EPhysik!", JOptionPane.ERROR_MESSAGE);
  37. System.exit(1);
  38. } else {
  39. image = bild;
  40. breite = bild.getWidth();
  41. hoehe = bild.getHeight();
  42. }
  43.  
  44. JButton myB01 = new JButton("GBR");
  45. myB01.setBounds(10, hoehe + 40, 100, 30);
  46. this.add(myB01);
  47.  
  48. myB01.addActionListener(new ActionListener() { // ActionListener für den JButton
  49. public void actionPerformed(ActionEvent e) {
  50.  
  51. //Verschachtelte Schleife: Für alle Stellen [x,y]:
  52. for (int y = 0; y < hoehe; y++) {
  53. for (int x = 0; x < breite; x++) {
  54. //hole RGB-Wert des Pixels[x|y]:
  55. int pixel = bild.getRGB(x, y);
  56. //extrahiere Rotwert:
  57. int r = (pixel >> 16) & 0x000000FF;
  58. //extrahiere Grünwert:
  59. int g = (pixel >> 8) & 0x000000FF;
  60. //extrahiere Blauwert:
  61. int b = pixel & 0x000000FF;
  62. //RGB wird GBR
  63. r = g;
  64. g = b;
  65. b = r;
  66.  
  67. //berechne RGB-Wert dazu:
  68. pixel = (r << 16) + (g << 8) + b;
  69. //setze RGB-Wert des Pixeels[x|y]
  70. bild.setRGB(x, y, pixel);
  71. }
  72. }
  73. repaint();
  74. }
  75. });
  76.  
  77. JButton myB02 = new JButton("BRG");
  78. myB02.setBounds(150, hoehe + 40, 100, 30);
  79. this.add(myB02);
  80.  
  81. myB02.addActionListener(new ActionListener() { // ActionListener für den JButton
  82. public void actionPerformed(ActionEvent e) {
  83.  
  84. //Verschachtelte Schleife: Für alle Stellen [x,y]:
  85. for (int y = 0; y < hoehe; y++) {
  86. for (int x = 0; x < breite; x++) {
  87. //hole RGB-Wert des Pixels[x|y]:
  88. int pixel = bild.getRGB(x, y);
  89. //extrahiere Rotwert:
  90. int r = (pixel >> 16) & 0x000000FF;
  91. //extrahiere Grünwert:
  92. int g = (pixel >> 8) & 0x000000FF;
  93. //extrahiere Blauwert:
  94. int b = pixel & 0x000000FF;
  95. //RGB wird GBR
  96. r = b;
  97. g = g;
  98. b = r;
  99.  
  100. //berechne RGB-Wert dazu:
  101. pixel = (r << 16) + (g << 8) + b;
  102. //setze RGB-Wert des Pixeels[x|y]
  103. bild.setRGB(x, y, pixel);
  104. }
  105. }
  106. repaint();
  107. }
  108. });
  109.  
  110. JButton myB03 = new JButton(">>1");
  111. myB03.setBounds(290, hoehe + 40, 100, 30);
  112. this.add(myB03);
  113.  
  114. myB03.addActionListener(new ActionListener() { // ActionListener für den JButton
  115. public void actionPerformed(ActionEvent e) {
  116.  
  117. //Verschachtelte Schleife: Für alle Stellen [x,y]:
  118. for (int y = 0; y < hoehe; y++) {
  119. for (int x = 0; x < breite; x++) {
  120. //hole RGB-Wert des Pixels[x|y]:
  121. int pixel = bild.getRGB(x, y);
  122. pixel = pixel >> 1;
  123. bild.setRGB(x, y, pixel);
  124. }
  125. }
  126. repaint();
  127. }
  128. });
  129.  
  130. JButton myB04 = new JButton("<<1");
  131. myB04.setBounds(430, hoehe + 40, 100, 30);
  132. this.add(myB04);
  133.  
  134. myB04.addActionListener(new ActionListener() { // ActionListener für den JButton
  135. public void actionPerformed(ActionEvent e) {
  136.  
  137. //Verschachtelte Schleife: Für alle Stellen [x,y]:
  138. for (int y = 0; y < hoehe; y++) {
  139. for (int x = 0; x < breite; x++) {
  140. //hole RGB-Wert des Pixels[x|y]:
  141. int pixel = bild.getRGB(x, y);
  142. pixel = pixel << 1;
  143. bild.setRGB(x, y, pixel);
  144. }
  145. }
  146. repaint();
  147. }
  148. });
  149.  
  150. JButton myB05 = new JButton("Mirror");
  151. myB05.setBounds(570, hoehe + 40, 100, 30);
  152. this.add(myB05);
  153.  
  154. myB05.addActionListener(new ActionListener() { // ActionListener für den JButton
  155. public void actionPerformed(ActionEvent e) {
  156.  
  157. //Verschachtelte Schleife: Für alle Stellen [x,y]:
  158. for (int y = 0; y < hoehe; y++) {
  159. for (int x = 0; x < breite; x++) {
  160. //hole RGB-Wert des Pixels[x|y]:
  161. int pixel = bild.getRGB(x, y);
  162. bild.setRGB(breite - 1 - x, y, pixel);
  163. }
  164. }
  165. repaint();
  166. }
  167. });
  168.  
  169. JButton myB06 = new JButton("Reverse");
  170. myB06.setBounds(710, hoehe + 40, 100, 30);
  171. this.add(myB06);
  172.  
  173. myB06.addActionListener(new ActionListener() { // ActionListener für den JButton
  174. public void actionPerformed(ActionEvent e) {
  175.  
  176. //Verschachtelte Schleife: Für alle Stellen [x,y]:
  177. for (int y = 0; y < hoehe; y++) {
  178. for (int x = 0; x < breite; x++) {
  179. //hole RGB-Wert des Pixels[x|y]:
  180. int pixel = bild.getRGB(x, y);
  181. bild2.setRGB(breite - 1 - x, y, pixel);
  182. }
  183. }
  184. tauscheBild(bild2, bild);
  185. repaint();
  186. }
  187. });
  188.  
  189. JButton myB07 = new JButton("Zweier");
  190. myB07.setBounds(850, hoehe + 40, 100, 30);
  191. this.add(myB07);
  192.  
  193. myB07.addActionListener(new ActionListener() { // ActionListener für den JButton
  194. public void actionPerformed(ActionEvent e) {
  195. int z = 0;
  196.  
  197. //Verschachtelte Schleife: Für alle Stellen [x,y]:
  198. for (int y = 0; y < hoehe; y++) {
  199. for (int x = 0; x < breite; x++) {
  200. //hole RGB-Wert des Pixels[x|y]:
  201. int pixel = bild.getRGB(x, y);
  202.  
  203. if (x % 2 == 0) {
  204.  
  205. bild2.setRGB(x, y, pixel);
  206. z++;
  207. }
  208.  
  209. }
  210. }
  211. tauscheBild(bild2, bild);
  212. repaint();
  213. }
  214. });
  215.  
  216. }
  217.  
  218. public void tauscheBild(BufferedImage b, BufferedImage b1) {
  219. for (int y = 0; y < hoehe; y++) {
  220. for (int x = 0; x < breite; x++) {
  221. //hole RGB-Wert des Pixels[x|y]:
  222. int pixel = bild2.getRGB(x, y);
  223. bild.setRGB(x, y, pixel);
  224. }
  225. }
  226. }
  227.  
  228. public void paintComponent(Graphics g) {
  229. g.drawImage(image, 5, 5, this);
  230. }
  231.  
  232. public static void main(String[] args) {
  233.  
  234. JFileChooser chooser = new JFileChooser();
  235. chooser.setCurrentDirectory(new File("."));
  236. chooser.setDialogTitle("Bitte Bild auswählen");
  237. if (chooser.showOpenDialog(null) == JFileChooser.CANCEL_OPTION) {
  238. System.exit(1);
  239. }
  240. File file = new File(chooser.getCurrentDirectory(), chooser
  241. .getSelectedFile().getName());
  242.  
  243. BitmapGrafik_Aufgabe myGrafik = new BitmapGrafik_Aufgabe(file);
  244.  
  245. JFrame myFrame = new JFrame();
  246. myFrame.setTitle("Gewaehltes Bild: " + file.getName());
  247. myFrame.setPreferredSize(new Dimension(myGrafik.breite + 18,
  248. myGrafik.hoehe + 130));
  249. myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  250. myFrame.add(myGrafik);
  251. myFrame.pack();
  252. myFrame.setVisible(true);
  253. }
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement