I'm currently working on a little project for school and I'm still at the very beginning. I've just started reading into JFrame and all that stuff, so don't wonder why I may not be so familiar with everything you'll show me.
The goal for now is to have a program that gives out an image and to be able to change every single pixel of that image manually. Thus, I've written the following code:
public class JavaGraphicsTest {
private static Pixel pixel;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
pixel = new Pixel(1600, 900);
frame.getContentPane().add(pixel);
//pixel.testChange();
}
}
and:
public class Pixel extends Component {
private BufferedImage img;
private int width;
private int height;
private Graphics graphics;
public Pixel(int w, int h) {
width = w;
height = h;
}
public void create() {
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//Set any color for now
for(int wc = 0; wc < width; wc++) {
for(int hc = 0; hc < height; hc++) {
img.setRGB(wc, hc, new Color(0xAAFFBB).getRGB());
}
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
graphics = g;
create();
update();
}
public void update() {
graphics.drawImage(img, 0, 0, null);
}
public void testChange() {
for(int i = 50; i < 80; i++) {
for(int j = 80; j < 120; j++) {
img.setRGB(i, j, new Color(0xFF8876).getRGB());
}
for(int j = 460; j < 493; j++) {
img.setRGB(i, j, new Color(0xFF8876).getRGB());
}
}
}
}
Well, the code works so far (after many hours of nasty error spotting xD), but what I want to do now seems not to work so far: I want to call the method "pixel.testChange()" in the main method (it's currently commented). But as far as I have understood how JFrame works, I can't do anything with an object once I've added it to the JFrame. But who should it work then? How can I modify any active object without deleting and re-adding it?
PS: If you don't understand what the testChange method is supposed to do: It should change two blocks of the image to another color, it's basically a test to see if I successfully changed the image.
If you need further information on the project, please ask me :)
Thanks in advance, Julian