I have done some reasearch but i couldnt find out what my Problem is, so i hope someone can help me here. I want to make an programm what gets an Image and change its length,width and color when i click different Buttons. It works so far but i have a problem with the buttons. First i wanted to create own Button classes but they didnt work that well so i decided to implement the Buttons in my main Method in the test class like this:
JButton test = new JButton("test");
test.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
BufferedImage myPicture = ImageIO.read(new File("myImage.jpg"));
JPanel p = new DrawNew(myPicture);
center.removeAll();
center.add(p);
} catch (IOException ex) {
System.out.println("didnt work");
System.exit(0);
}
}
});
The Problem is that it dosnt do anything when i clikc the Button. i tried only center.removeAll() but it dosnt do anything. it seems like center in the Button seem different from the center in the main class. Here is the main class:
package app.exercise.test;
import app.exercise.data.JButtonExit;
import app.exercise.data.JButtonOriginal;
import app.exercise.data.JButtonGrayscale;
import app.exercise.data.JButtonPattern;
import app.exercise.data.DrawNew;
import java.awt.BorderLayout;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class AppTest {
public static void main(String[] args) {
JFrame f = new JFrame("App");
JPanel center = new JPanel();
JPanel bottom = new JPanel();
try {
BufferedImage myPicture = ImageIO.read(new File("myImage.jpg"));
JPanel p = new DrawNew(myPicture);
center.add(p);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} catch (IOException ex) {
System.out.println("didnt work");
System.exit(0);
}
JButton exitButton = new JButton("Exit");
JButtonExit j = new JButtonExit();
exitButton.addActionListener(j);
int h =1;
JButton test = new JButton("test");
test.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
BufferedImage myPicture = ImageIO.read(new File("myImage.jpg"));
JPanel p = new DrawNew(myPicture);
center.removeAll();
center.add(p);
} catch (IOException ex) {
System.out.println("didnt work");
System.exit(0);
}
}
});
JButton originalButton = new JButton("Original");
JButtonOriginal k = new JButtonOriginal();
originalButton.addActionListener(k);
JButton grayscaleButton = new JButton("Grayscale");
JButtonGrayscale l = new JButtonGrayscale();
grayscaleButton.addActionListener(l);
JButton patternButton = new JButton("Pattern");
JButtonPattern m = new JButtonPattern();
patternButton.addActionListener(m);
Box boxBottom = Box.createHorizontalBox();
boxBottom.add(Box.createHorizontalGlue());
boxBottom.add(exitButton);
Box topBox = Box.createHorizontalBox();
topBox.add(originalButton);
topBox.add(grayscaleButton);
topBox.add(patternButton);
topBox.add(test);
f.add(topBox, BorderLayout.NORTH);
f.add(center, BorderLayout.CENTER);
f.add(boxBottom, BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}
}
the DrawNew class looks like this:
package app.exercise.data;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.awt.image.ColorModel;
public class DrawNew extends JPanel {
private int height;
private int width;
private BufferedImage myPic;
public DrawNew (BufferedImage myPicture) {
height = myPicture.getHeight();
width = myPicture.getWidth();
myPic = myPicture;
}
public Dimension getPreferredSize() {
return new Dimension(width*2, height*2);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int select = 1;
switch(select) {
case 1: select = 1;
for (int i = 0; i < width; i++) {
for (int k = 0; k<height;k++) {
Color mycolor = new Color(myPic.getRGB(i,k));
g.setColor(mycolor);
g.fillRect(2*i,2*k,2,2);
}
}
break;
case 2: select = 2;
for (int i = 0; i < width; i++) {
for (int k = 0; k<height;k++) {
Color mycolor = new Color(myPic.getRGB(i,k));
int h = mycolor.getRed();
int t = mycolor.getRed();
int f = mycolor.getRed();
int mittel = (h+f+t)/3;
Color newColor = new Color (mittel,mittel,mittel);
g.setColor(newColor);
g.fillRect(2*i,2*k,2,2);
}
}
break;
case 3: select = 3;
for (int i = 0; i < width; i++) {
for (int k = 0; k<height;k++) {
Color mycolor = new Color(myPic.getRGB(i,k));
int h = mycolor.getRed();
int t = mycolor.getRed();
int f = mycolor.getRed();
int mittel = (h+f+t)/3;
if (mittel <= 51) {
g.setColor(Color.BLACK);
g.fillRect(2*i,2*k,2,2);
} else if(mittel <=102) {
g.setColor(Color.WHITE);
g.drawLine(2*i,2*k,2*i,2*k);
g.setColor(Color.BLACK);
g.drawLine(2*i+1,2*k+1,2*i+1,2*k+1);
g.drawLine(2*i+1,2*k,2*i+1,2*k);
g.drawLine(2*i,2*k+1,2*i,2*k+1);
} else if(mittel <=153) {
g.setColor(Color.BLACK);
g.drawLine(2*i,2*k,2*i,2*k);
g.drawLine(2*i+1,2*k+1,2*i+1,2*k+1);
g.setColor(Color.WHITE);
g.drawLine(2*i+1,2*k,2*i+1,2*k);
g.drawLine(2*i,2*k+1,2*i,2*k+1);
} else if(mittel <=204) {
g.setColor(Color.BLACK);
g.drawLine(2*i,2*k,2*i,2*k);
g.setColor(Color.WHITE);
g.drawLine(2*i+1,2*k,2*i+1,2*k);
g.drawLine(2*i,2*k+1,2*i,2*k+1);
g.drawLine(2*i+1,2*k+1,2*i+1,2*k+1);
} else if(mittel <=255) {
g.setColor(Color.WHITE);
g.fillRect(2*i,2*k,2,2);
}
}
}
break;
}
}
}
So my question is: How can i enter center.add(p) in my Button. it works at the start without any problems but in the button for some reason not.
I appreciate any help.