This discussion is archived
6 Replies Latest reply: Aug 9, 2011 11:52 AM by 875749 RSS

Writing an Image

875749 Newbie
Currently Being Moderated
Hello, all! I just recently created a post about reading images, and with the knowledge I learned from that I decided to undertake editing the same program to write images. However, I've come across a stumbling block. Listed here is the code for the class that does all the primary work:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.JFrame;
import java.util.Random;
import java.awt.Color;

public class Images extends JFrame
{
     private Screen s;
     private BufferedImage bg;
     private BufferedImage pic;
     private static BufferedImage newImage;
     private boolean loaded;
     private static Random r;
     static int color;
     static Color myColor;
     
     public static void main(String args[])
     {
          System.out.println("Getter done!");
          DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
          Images i = new Images();
          newpic();
          i.run(dm);
     }
     
     public void run(DisplayMode dm)
     {
          loaded = false;
          
          s = new Screen();
          
          try
          {
               s.setFullScreen(dm, this);
               loadpics();
               try
               {
                    Thread.sleep(5000);
               }
               catch (Exception ex) { }
          }
          finally
          {
               s.restoreScreen();
          }
     }
     
     public void loadpics()
     {
          try
          {
               bg = ImageIO.read(new File("back.jpeg"));
               pic = ImageIO.read(new File("face.png"));
          }
          catch (IOException e) { }
          
          for (int i = 0; i < 100; i += 2)
          {
               bg.setRGB(i, i, 0xFF0C00FF);
               pic.setRGB(i, i, 0xFF0C00FF);
          }
          
          loaded = true;
          repaint();
     }
     
     private static void newpic()
     {
          try
          {
               newImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
               for (int i = 0; i < 100; i++)
               {
                    for (int j = 0; j < 100; j++)
                    {
                         myColor = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
                         color = myColor.getRGB();
                         System.out.printf("Color is %d\n", color);
                         newImage.setRGB(i, j, color);
                    }
               }
          }
          catch (Exception ex) { }
          
          try
          {
               File outputFile = new File("saved.png");
               ImageIO.write(newImage, "png", outputFile);
          }
          catch (Exception ex) { }
     }
     
     public void paint(Graphics g)
     {
          if (loaded)
          {
               g.drawImage(bg, 0, 0, null);
               g.drawImage(pic, 170, 180, null);
          }
     }
}
There is another class I use that is just used to get a fullscreen window and whatnot but doesn't really have anything else to do with the problem I have specifically. What's happening is I'm trying to create and write out an image of randomly generated pixels (100 x 100) and output each color to the console just to verify that it's creating colors. After reading through the Java tutorial and gleaning as much as I could, I assembled the method newpic() and figured everything within it would work. What I tried is re-instantiating myColor, which is a Color object I'm using, to create a random color that I then put into color (an integer) so that I can then use color in the setRGB() method on the image I'm trying to write to because it will only take an integer argument (if only it supported the Color object itself...) Anyways, I'm not sure if there's something obvious or not so obvious that I did wrong, but help would be much appreciated in figuring this out. :] The program does output an image and save it where I've told it to, but the image comes up as completely black and the program does not output to the console at all, so it seems as if it is skipping the for loop that generates all the pixels but continuing on to where it saves the file. Thanks again (and sorry for the long post)!

Colton
  • 1. Re: Writing an Image
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Your code can't be compiled for testing. What is <tt>Screen</tt>?

    db

    edit And don't ever swallow exceptions. In a catch block, at minimum print the stack trace.

    Edited by: Darryl Burke
  • 2. Re: Writing an Image
    875749 Newbie
    Currently Being Moderated
    Here you are, sir. Basically it's a class that wraps fullscreen switching and some other miscellaneous stuff like exiting itself and whatnot.
    import java.awt.*;
    import javax.swing.JFrame;
    
    public class Screen
    {
         private GraphicsDevice vc;
         
         public Screen()
         {
              GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
              vc = env.getDefaultScreenDevice();
         }
         
         public void setFullScreen(DisplayMode dm, JFrame window)
         {
              window.setUndecorated(true);
              window.setResizable(false);
              vc.setFullScreenWindow(window);
              
              if (dm != null && vc.isDisplayChangeSupported())
              {
                   try
                   {
                        vc.setDisplayMode(dm);
                   } catch (Exception ex) { }
              }
         }
         
         public Window getFullScreenWindow()
         {
              return vc.getFullScreenWindow();
         }
         
         public void restoreScreen()
         {
              Window w = vc.getFullScreenWindow();
              if (w != null)
              {
                   w.dispose();
              }
              
              vc.setFullScreenWindow(null);
         }
    }
    Thank you for your prompt reply!

    Colton
  • 3. Re: Writing an Image
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Now print those stack traces and discover which variable is null because you never assigned it a value.

    db
  • 4. Re: Writing an Image
    875749 Newbie
    Currently Being Moderated
    Wow, what a frustratingly simple fix. The program works perfectly now, and, though I feel somewhat klutzy, the satisfaction derived from my program compensates for it. :] Thank you very much, Darryl.

    Colton
  • 5. Re: Writing an Image
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Some other points.

    -- All Swing components should be constructed and their methods called only on the EDT.
    -- Use meaningful variable names. Extremely short one- and two-letter variable names make code difficult to read.
    -- For forum postings, it's better to indent code with 2 to 4 spaces, not tabs
    -- (repeated for emphasis) Never, never swallow exceptions. In the very rare case that you really don't care if an exception is thrown, place a comment in the catch block.
    } catch (...) {
        // ignore
    }
    db
  • 6. Re: Writing an Image
    875749 Newbie
    Currently Being Moderated
    Okay, thanks, Darryl. For code I write personally and for non-testing purposes, I always use meaningful names, but for this example I was more focused on brevity. And I went ahead and changed the settings in my editor to replace tabs with spaces so now that'll work better for forum posts. :] I still have to read more on the EDT to get a full understanding of what that actually is, because I read about it a month or so back when you first commented on one of my posts and it didn't completely click, so I'll try retouching that topic. Thank you, sir!

    Colton

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points