Search code examples
javacolorsrgbpixel

Coloring Individual Pixels


i'm trying to write a program with Java what generates images by coloring individual pixels with RGB by adding up the value of every RGB-channel by one until it reaches 255 and then adds one on the next RGB channel.

Here an example:

RED is set to 0 GREEN is set to 0 BLUE is set to 0

  1. RED gets added up by one (RED ++)on the first pixel until it reaches 255.

  2. After RED reaches 255 RED gets set to 0 and GREEN gets added up by one (GREEN ++).

  3. The RED channel gets added up again like in step 1 until 255 and then step 2 follows again.

  4. If GREEN is 255 the same method is used for BLUE means that one gets added to BLUE while GREEN and RED will be set to 0 again. Then step 1 again.

  5. After all channels in the first Pixel are 255 the second pixel gets one up on its RED channel. Then it should begin by step 1 again until the second Pixel has a value of 255 on RED, what will set RED back to 0 and GREEN gets one up on the second pixel and so on and so on....

I'm sorry for my bad english and for my limited Java-knowledge.

This is my whole code so far(Stuff in comments is either not important in the project no more, not ready to use or i don't understand how to use it xD):

    import javax.swing.JFrame;
//import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
//import java.awt.image.BufferedImage;

public class Quadrat 
{   
  public static void main (String[] args)
  { int x = 100;
    int y = 0;
    int z = 0;
    int Max = 255;
    int height = 500;
    int width = 500;

    if (x < Max){
    x ++;
    } else {
        x = 0;
        y ++;
    }
    if (y > Max){
        y = 0;
        z ++;
        } 

    if (z > Max){
        z = 0;
        } 


    JFrame Bild = new JFrame("Bildergenerator");
    Bild.setSize(width,height);
    Bild.setVisible(true);


    Color Kartoffel = new Color(x, y, z, 255);
    BufferedImage Test = new BufferedImage(300, 200, 
    BufferedImage.TYPE_INT_ARGB);
    Graphics graphics = Test.getGraphics();
    graphics.setColor(Kartoffel);
    graphics.fillRect(100, 100, 100, 100);
    graphics.dispose();
    Test.setRGB(1, 1, Kartoffel.getRGB());

  }


    /*System.out.println(x);
    System.out.println(y);
    System.out.println(z);*/
    //img.setRGB(x,y,z);




/*    JFrame Test = new JFrame("Jframe1");
      JLabel Label = new JLabel("");
      Test.add(Label);
      Test.setSize(xx,yy);
      Test.setVisible(true);
      Test.setLocation(x/2-(xx/2), y/2-(yy/2));*/



  /* JFrame Hyaeaeae = new JFrame("BurrScurrSWAG");
      Hyaeaeae.setSize(1000,1000);
      Hyaeaeae.setVisible(true);
      Hyaeaeae.getContentPane().setBackground( Color.RED);*/




    }

I'll hope you can help me in any way and if so i'm very thankful! Thank you for your Attention! Have a nice day! c:


Solution

  • You need to create the timer for this as your code is just changing on the first iteration on startup after this nothing is changed so a timer to make sure increments keep on applied on variables

    package javatestapp;
    
    import java.awt.Color;
    import java.util.Timer;
    import java.util.TimerTask;
    import javax.swing.JFrame;
    
    public class JavaTestApp extends TimerTask
    {
        static int x = 100;
        static int y = 0;
        static int z = 0;
        static int Max = 255;
        static int height = 500;
        static int width = 500; 
        static JFrame frame;
    
        public static void main(String[] args)
        {
            InitFrame();
        }
    
        public static void incrementValues()
        {
            if (x < Max)
            {
               x ++;
            }
            else
            {
               x = 0;
               y ++;
            }
    
            if (y > Max)
            {
               y = 0;
               z ++;
            } 
    
            if (z > Max)
            {
              x = 0;
              y = 0;
              z = 0;
            }
        }
    
        public static void genImage()
        {
            Color color = new Color(x,y,z,255);
            frame.getContentPane().setBackground(color);
        }
    
        public static void InitFrame()
        {
            frame = new JFrame("Bildergenerator");
            frame.setSize(width,height);
            frame.setVisible(true);
    
            Timer timer = new Timer(true);
            TimerTask timerTask = new JavaTestApp();
            timer.scheduleAtFixedRate(timerTask, 0, 10);
        } 
    
        @Override
        public void run()
        {
            incrementValues();
            genImage();
        }
    }