Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Give random color to each pixel on a window
New Posts  All Forums:Forum Nav:

Give random color to each pixel on a window

post #1 of 3
Thread Starter 
Hey guys...i am trying to make a small programm in which when you run it a small window opens that has a random colour for each pixel of the small window.
Until now i was able to find examples of code that can create a small window with all pixels white.
this is the example code in c++:
Warning: Spoiler! (Click to show)
Code:
//----- x1.c simple xwindows example -----

#include <X11/Xlib.h> // must precede most other headers!
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>

void update_screen();

int XRES=500;
int YRES=500;
int NPTS=25000;

//Need this array if we're plotting all at once using XDrawPoints
//XPoint pts[NPTS];

Display *dsp;
Window win;
GC gc;

int main(){

  dsp = XOpenDisplay( NULL );
  if( !dsp ){ return 1; }

  int screen = DefaultScreen(dsp);
  unsigned int white = WhitePixel(dsp,screen);
  unsigned int black = BlackPixel(dsp,screen);

  win = XCreateSimpleWindow(dsp,
                            DefaultRootWindow(dsp),
                            0, 0,   // origin
                            XRES, YRES, // size
                            0, black, // border width/clr
                            white);   // backgrd clr


  Atom wmDelete=XInternAtom(dsp, "WM_DELETE_WINDOW", True);
  XSetWMProtocols(dsp, win, &wmDelete, 1);

  gc = XCreateGC(dsp, win,
                 0,       // mask of values
                 NULL);   // array of values

  XSetForeground(dsp, gc, black);

  XEvent evt;
  long eventMask = StructureNotifyMask;
  eventMask |= ButtonPressMask|ButtonReleaseMask|KeyPressMask|KeyReleaseMask;
  XSelectInput(dsp, win, eventMask);

  KeyCode keyQ;
  keyQ = XKeysymToKeycode(dsp, XStringToKeysym("Q"));

  XMapWindow(dsp, win);

  // wait until window appears
  do { XNextEvent(dsp,&evt); } while (evt.type != MapNotify);

  srand(time(0)); // only 1 sec resolution so use once per run
  update_screen();

  int loop = 1;

  while (loop) {
    // for repeated fine seeding use:
    // struct timeval ti
    // gettimeofday(&ti,NULL);
    // srand((ti.tv_sec * 1000) + (ti.tv_usec / 1000));

    XNextEvent(dsp, &evt);

    switch (evt.type) {

      case (ButtonRelease) :

        update_screen();
        break;

      case (KeyRelease) :

        if (evt.xkey.keycode == keyQ) loop = 0;
        else update_screen();
        break;

      case (ConfigureNotify) :

        // Check if window has been resized
        if (evt.xconfigure.width != XRES || evt.xconfigure.height != YRES)
        {
          XRES = evt.xconfigure.width;
          YRES = evt.xconfigure.height;
          update_screen();
        }
        break;

      case (ClientMessage) :

        if (evt.xclient.data.l[0] == wmDelete) loop = 0;
        break;

      default :

        break;
    }
  } 

  XDestroyWindow(dsp, win);
  XCloseDisplay(dsp);

  return 0;
}

void update_screen()
{
  XClearWindow(dsp, win);

  XDrawLine(dsp, win, gc, 0, YRES/2, XRES-1, YRES/2); //from-to
  XDrawLine(dsp, win, gc, XRES/2, 0, XRES/2, YRES-1); //from-to

  long i;
  for (i=0; i<NPTS; i++) {
    XDrawPoint(dsp, win, gc, rand()%XRES, rand()%YRES);
    //pts[i].x = rand()%XRES;
    //pts[i].y = rand()%YRES;
  }

  //XClearWindow(dsp,win);
  //XDrawPoints(dsp, win, gc, pts, NPTS, CoordModeOrigin);

  return;
}

anybody here knows if i can use the SetPixel function in this programm and use a random RGB color to each pixel of the window? If no, what other ways is it possible to make this work?
Alienware 17
(5 items)
 
Sacred Moon
(18 items)
 
 
CPUGraphicsHard DriveOS
Intel Core i7 4700MQ NVidia Geforce GTX 770M Samsung 840 Evo Windows 8.1 x64 
Mouse
Steelseries Rival 
CPUMotherboardGraphicsRAM
Intel Core 2 Quad Q9550 Gigabyte EP45-UD3P Radeon HD 6870 Corsair Dominator 4GB 1066MHz 
Hard DriveHard DriveHard DriveCooling
OCZ Vertex 2E 60GB Seagate Barracuda 500GB Seagate Barracuda 500GB XSPC Raystorm 
CoolingCoolingOSMonitor
Black Ice GTX480 Dual Laing DDC-1T w/ EK Dual Top Windows 7 Ultimate 64-bit Samsung T220HD 
KeyboardPowerCaseMouse
Logitech G15 Silverstone Strider Plus ST75F-P 750W Coolermaster CM 690 II Advanced (Modded) Razer Mamba 
Mouse Pad
Roccat Sense 
  hide details  
Reply
Alienware 17
(5 items)
 
Sacred Moon
(18 items)
 
 
CPUGraphicsHard DriveOS
Intel Core i7 4700MQ NVidia Geforce GTX 770M Samsung 840 Evo Windows 8.1 x64 
Mouse
Steelseries Rival 
CPUMotherboardGraphicsRAM
Intel Core 2 Quad Q9550 Gigabyte EP45-UD3P Radeon HD 6870 Corsair Dominator 4GB 1066MHz 
Hard DriveHard DriveHard DriveCooling
OCZ Vertex 2E 60GB Seagate Barracuda 500GB Seagate Barracuda 500GB XSPC Raystorm 
CoolingCoolingOSMonitor
Black Ice GTX480 Dual Laing DDC-1T w/ EK Dual Top Windows 7 Ultimate 64-bit Samsung T220HD 
KeyboardPowerCaseMouse
Logitech G15 Silverstone Strider Plus ST75F-P 750W Coolermaster CM 690 II Advanced (Modded) Razer Mamba 
Mouse Pad
Roccat Sense 
  hide details  
Reply
post #2 of 3
You mean something like this? Java (Click to show)
Code:
import java.awt.*;

public class Fenster_04 extends Frame
{
    private MyWindowListener wl = new MyWindowListener();

    public Fenster_04()
    {
        super("Unser viertes Fenster...");
        this.setSize(1680, 970);
        this.setResizable(false);
        this.setVisible(true);
        this.addWindowListener(wl);
    }

    public int random()
    {
        return (int) (Math.random()*256);
    }

    @Override
    public void paint(Graphics g)
    {
        for(int y = 0; y < 970; y += 1)
        {
            for(int x = 0; x < 1680; x += 1)
            {
                g.setColor(new Color(random(), random(), random()));
                g.fillRect(x, y, 1, 1);
            }
        }
    }

    public static void main(String[] args)
    {
        Fenster_04 f = new Fenster_04();
    }
}
Sorry, it's my german code, but you should be able to tell what it does smile.gif.
Petrichor
(24 items)
 
Harbringer
(12 items)
 
Jaded
(5 items)
 
CPUMotherboardGraphicsRAM
Intel i5-3570K @ 4.7GHz / 1.256V ASRock Z77 Extreme4 Gainward GTX 1060, 6GB TeamGroup 8GB, DDR3-1333 @ 1600MHz / 1.5V, 9-9-... 
Hard DriveHard DriveHard DriveOptical Drive
Samsung SSD 830 Series, 128GB Seagate Barracuda ST31000524AS, 1TB, 7200 RPM Hitachi Deskstar IDK 0S03661, 3TB, 7200 RPM LG GH22NS50 
CoolingCoolingCoolingOS
Lamptron FC5 V3 Noctua NH-D15 Noctua NF-A14 FLX (140mm) (x5)  Windows 10 Professional (64-bit) 
MonitorMonitorKeyboardPower
Dell UltraSharp U2312HM (x3) LG 40UB800V Lioncast LK20, TKL, Cherry MX-Blue, DE-Layout Corsair AX860 
CaseMouseMouse PadAudio
Fractal Define R4 Black Mionix Castor Akasa Venom XXL Denon PMA 520 AE AMP 
AudioAudioAudioAudio
Dali Zensor 3 Speakers (x2) Magnat Monitor Supreme Sub 301 A Audioengine D1 DAC / Headphone AMP Beyerdynamic DT 770 Pro 250 Ω 
CPUMotherboardRAMHard Drive
Intel Pentium G3440T ASRock E3C226D2I Kingston ValueRAM, 16GB, DDR3-1333 MHz, ECC HGST IDK Deskstar H3IK40003272SE, 4 TB, 7200RPM 
Hard DriveOSPowerCase
Seagate ST4000DM000, 4 TB, 5900RPM (x3) FreeNAS 9.10 be quiet! Straight Power E9 400W NZXT Source 210 Elite 
CPUGraphicsRAMHard Drive
Intel Core i3-2367M Intel HD Graphics 3000 4 GB 500 GB 
Monitor
11.6" WXGA non-glare LED TFT, 1366x768 
  hide details  
Reply
Petrichor
(24 items)
 
Harbringer
(12 items)
 
Jaded
(5 items)
 
CPUMotherboardGraphicsRAM
Intel i5-3570K @ 4.7GHz / 1.256V ASRock Z77 Extreme4 Gainward GTX 1060, 6GB TeamGroup 8GB, DDR3-1333 @ 1600MHz / 1.5V, 9-9-... 
Hard DriveHard DriveHard DriveOptical Drive
Samsung SSD 830 Series, 128GB Seagate Barracuda ST31000524AS, 1TB, 7200 RPM Hitachi Deskstar IDK 0S03661, 3TB, 7200 RPM LG GH22NS50 
CoolingCoolingCoolingOS
Lamptron FC5 V3 Noctua NH-D15 Noctua NF-A14 FLX (140mm) (x5)  Windows 10 Professional (64-bit) 
MonitorMonitorKeyboardPower
Dell UltraSharp U2312HM (x3) LG 40UB800V Lioncast LK20, TKL, Cherry MX-Blue, DE-Layout Corsair AX860 
CaseMouseMouse PadAudio
Fractal Define R4 Black Mionix Castor Akasa Venom XXL Denon PMA 520 AE AMP 
AudioAudioAudioAudio
Dali Zensor 3 Speakers (x2) Magnat Monitor Supreme Sub 301 A Audioengine D1 DAC / Headphone AMP Beyerdynamic DT 770 Pro 250 Ω 
CPUMotherboardRAMHard Drive
Intel Pentium G3440T ASRock E3C226D2I Kingston ValueRAM, 16GB, DDR3-1333 MHz, ECC HGST IDK Deskstar H3IK40003272SE, 4 TB, 7200RPM 
Hard DriveOSPowerCase
Seagate ST4000DM000, 4 TB, 5900RPM (x3) FreeNAS 9.10 be quiet! Straight Power E9 400W NZXT Source 210 Elite 
CPUGraphicsRAMHard Drive
Intel Core i3-2367M Intel HD Graphics 3000 4 GB 500 GB 
Monitor
11.6" WXGA non-glare LED TFT, 1366x768 
  hide details  
Reply
post #3 of 3
Thread Starter 
I'm not so familiar with Java but it looks like yes, it's similar to what i am looking for smile.gif
Alienware 17
(5 items)
 
Sacred Moon
(18 items)
 
 
CPUGraphicsHard DriveOS
Intel Core i7 4700MQ NVidia Geforce GTX 770M Samsung 840 Evo Windows 8.1 x64 
Mouse
Steelseries Rival 
CPUMotherboardGraphicsRAM
Intel Core 2 Quad Q9550 Gigabyte EP45-UD3P Radeon HD 6870 Corsair Dominator 4GB 1066MHz 
Hard DriveHard DriveHard DriveCooling
OCZ Vertex 2E 60GB Seagate Barracuda 500GB Seagate Barracuda 500GB XSPC Raystorm 
CoolingCoolingOSMonitor
Black Ice GTX480 Dual Laing DDC-1T w/ EK Dual Top Windows 7 Ultimate 64-bit Samsung T220HD 
KeyboardPowerCaseMouse
Logitech G15 Silverstone Strider Plus ST75F-P 750W Coolermaster CM 690 II Advanced (Modded) Razer Mamba 
Mouse Pad
Roccat Sense 
  hide details  
Reply
Alienware 17
(5 items)
 
Sacred Moon
(18 items)
 
 
CPUGraphicsHard DriveOS
Intel Core i7 4700MQ NVidia Geforce GTX 770M Samsung 840 Evo Windows 8.1 x64 
Mouse
Steelseries Rival 
CPUMotherboardGraphicsRAM
Intel Core 2 Quad Q9550 Gigabyte EP45-UD3P Radeon HD 6870 Corsair Dominator 4GB 1066MHz 
Hard DriveHard DriveHard DriveCooling
OCZ Vertex 2E 60GB Seagate Barracuda 500GB Seagate Barracuda 500GB XSPC Raystorm 
CoolingCoolingOSMonitor
Black Ice GTX480 Dual Laing DDC-1T w/ EK Dual Top Windows 7 Ultimate 64-bit Samsung T220HD 
KeyboardPowerCaseMouse
Logitech G15 Silverstone Strider Plus ST75F-P 750W Coolermaster CM 690 II Advanced (Modded) Razer Mamba 
Mouse Pad
Roccat Sense 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Give random color to each pixel on a window