Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #2270 > unrolled thread

Change decimal color code on the fly

Started byBob <bherbst65@hotmail.com>
First post2012-11-16 08:05 -0800
Last post2012-11-16 17:11 -0500
Articles 10 — 6 participants

Back to article view | Back to comp.lang.java.help


Contents

  Change decimal color code on the fly Bob <bherbst65@hotmail.com> - 2012-11-16 08:05 -0800
    Re: Change decimal color code on the fly Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-11-16 11:49 -0500
    Re: Change decimal color code on the fly markspace <-@.> - 2012-11-16 08:50 -0800
    Re: Change decimal color code on the fly Nigel Wade <nmw@ion.le.ac.uk> - 2012-11-16 16:52 +0000
    Re: Change decimal color code on the fly Roedy Green <see_website@mindprod.com.invalid> - 2012-11-16 10:14 -0800
    Re: Change decimal color code on the fly Bob <bherbst65@hotmail.com> - 2012-11-16 10:39 -0800
      Re: Change decimal color code on the fly Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-11-16 13:56 -0500
    Re: Change decimal color code on the fly Bob <bherbst65@hotmail.com> - 2012-11-16 12:23 -0800
      Re: Change decimal color code on the fly Lew <lewbloch@gmail.com> - 2012-11-16 12:38 -0800
      Re: Change decimal color code on the fly Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-11-16 17:11 -0500

#2270 — Change decimal color code on the fly

FromBob <bherbst65@hotmail.com>
Date2012-11-16 08:05 -0800
SubjectChange decimal color code on the fly
Message-ID<5509b9ea-041a-46e6-b788-2abe90fcb8e7@googlegroups.com>
Hi All,
While making a datafile of decimal color pixel
codes for a small image, I want to change 
a particular String number that will go into the datafile 
to a different String number. 
I am doing this before it is added to the 
ArrayList. It does not seem to work and I need 
to know why. Below is my test to identify 
the String number and looking for the "if" 
statement to recognize it"
 
Here is a snip of my code:
....
      mSet =   redStr +  greenStr +  blueStr;
      System.out.println(mSet);//this is successful
      mSetx= "255255255";

      if (mSet ==  mSetx) {
         System.out.println("yes, recognized"); 
         //mSet = ("113000000");
      }
      colorpxl.add(mSet);// ArrayList
      mSet = ""; 

Thanks in advance for your response. 

Bob

[toc] | [next] | [standalone]


#2271

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2012-11-16 11:49 -0500
Message-ID<k85qq1$18c$1@dont-email.me>
In reply to#2270
On 11/16/2012 11:05 AM, Bob wrote:
> Hi All,
> While making a datafile of decimal color pixel
> codes for a small image, I want to change
> a particular String number that will go into the datafile
> to a different String number.
> I am doing this before it is added to the
> ArrayList. It does not seem to work and I need
> to know why. Below is my test to identify
> the String number and looking for the "if"
> statement to recognize it"

     I'm not sure what your problem is, so my comments
may be off the mark ...

> Here is a snip of my code:
> ....
>        mSet =   redStr +  greenStr +  blueStr;
>        System.out.println(mSet);//this is successful
>        mSetx= "255255255";
>
>        if (mSet ==  mSetx) {

     It looks like `mSet' and `mSetx' are String references.
If so, the `==' operator is probably the wrong thing to use;
most likely `mSet.equals(mSetx)' or `mSetx.equals(mSet)' is
what you intend.

     `==' asks "Do these two references denote the exact same
          String instance?"

     equals() asks "Do these Strings have the same value?"

     Analogy: Imagine you've found a dollar bill on the street,
and that you later fed it into a vending machine in exchange
for a candy bar.  Imagine also that you have another dollar
bill in your wallet.  You can now refer to "the dollar I found"
and to "the dollar I spent" and to "the dollar in my wallet."
A hypothetical equals() method for dollar bills would declare
that all three of these notions refer to a dollar bill with the
same value as the others (and as themselves), but the `=='
operator would say the first two differ from the third (the
"found" bill and the "spent" bill refer to the same physical
scrap of green paper, while the "wallet" bill refers to a
different one).  If your problem is that the `if' doesn't seem
to work, this is probably why.

>           System.out.println("yes, recognized");
>           //mSet = ("113000000");
>        }
>        colorpxl.add(mSet);// ArrayList
>        mSet = "";

     Or perhaps this is what confuses you.  `mSet' is not a
String object, but a reference to a String object.  Somewhere
"out there" is a String that `mSet' once referred to; the
ArrayList also has a reference to that same String.  When you
change `mSet' to refer to a different String (an empty String),
you do not change the ArrayList's reference to the original.
If you expected the final assignment to change what's in the
ArrayList -- well, it won't.

-- 
Eric Sosman
esosman@comcast-dot-net.invalid

[toc] | [prev] | [next] | [standalone]


#2272

Frommarkspace <-@.>
Date2012-11-16 08:50 -0800
Message-ID<k85qrs$1o8$1@dont-email.me>
In reply to#2270
On 11/16/2012 8:05 AM, Bob wrote:

>        mSet =   redStr +  greenStr +  blueStr;
>        System.out.println(mSet);//this is successful
>        mSetx= "255255255";
>
>        if (mSet ==  mSetx) {


Well, this will test that mSet and mSetx are the same object.  If you 
want to test that their contents are equal, use "equals." Also, try 
learning the language, this is really fundamental.


[toc] | [prev] | [next] | [standalone]


#2273

FromNigel Wade <nmw@ion.le.ac.uk>
Date2012-11-16 16:52 +0000
Message-ID<agn9a9FkggU1@mid.individual.net>
In reply to#2270
On 16/11/12 16:05, Bob wrote:
> Hi All,
> While making a datafile of decimal color pixel
> codes for a small image, I want to change
> a particular String number that will go into the datafile
> to a different String number.
> I am doing this before it is added to the
> ArrayList. It does not seem to work and I need
> to know why. Below is my test to identify
> the String number and looking for the "if"
> statement to recognize it"
>
> Here is a snip of my code:
> ....
>        mSet =   redStr +  greenStr +  blueStr;
>        System.out.println(mSet);//this is successful
>        mSetx= "255255255";
>
>        if (mSet ==  mSetx) {
>           System.out.println("yes, recognized");
>           //mSet = ("113000000");
>        }
>        colorpxl.add(mSet);// ArrayList
>        mSet = "";
>
> Thanks in advance for your response.
>
> Bob

You are using the wrong test. == tests for equality of the reference, 
not equality of what is referenced. It has no knowledge of String and 
cannot compare the actual contents of a String. All it does is compare 
the references mSet and mSetx. These refer to two different Strings, 
even though the Strings may contain the same sequence of characters, and 
therefore the test fails.

What you need is String.equals().

-- 
Nigel Wade

[toc] | [prev] | [next] | [standalone]


#2274

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-11-16 10:14 -0800
Message-ID<8h0da8hp5garspokp88pji6v8d4mli2idr@4ax.com>
In reply to#2270
On Fri, 16 Nov 2012 08:05:12 -0800 (PST), Bob <bherbst65@hotmail.com>
wrote, quoted or indirectly quoted someone who said :

>   if (mSet ==  mSetx) {

that only works if both strings are interned, otherwise you must use
if (mSet.equals( mSetx))

They are not necessarily the precise same object.


 See http://mindprod.com/jgloss/gotchas.html
-- 
Roedy Green Canadian Mind Products http://mindprod.com
Types of Garbage Collection:
()In Canada, the government sends men to your house every every week
  to take away your garbage. Hoarders are free to hang onto things 
  they don't really need.
()In third world countries, it is up to you to take your own garbage away.
()Java’s garbage collection system is analogous to a garbage removal
  system where every hour, workers scan your house for junk mail, the
  contents of waste baskets and anything else they are absolutely sure you
  don't want to keep.
()C++'s system for disposing of unreferenced objects is similar to India's,
  with the strange feature that undiscarded garbage becomes invisible but
  still stinks.

[toc] | [prev] | [next] | [standalone]


#2275

FromBob <bherbst65@hotmail.com>
Date2012-11-16 10:39 -0800
Message-ID<7a706635-c2f5-4e32-8b5b-26b4b05c558b@googlegroups.com>
In reply to#2270
On Friday, November 16, 2012 11:05:12 AM UTC-5, Bob wrote:
> Hi All,
> 
> While making a datafile of decimal color pixel
> 
> codes for a small image, I want to change 
> 
> a particular String number that will go into the datafile 
> 
> to a different String number. 
> 
> I am doing this before it is added to the 
> 
> ArrayList. It does not seem to work and I need 
> 
> to know why. Below is my test to identify 
> 
> the String number and looking for the "if" 
> 
> statement to recognize it"
> 
>  
> 
> Here is a snip of my code:
> 
> ....
> 
>       mSet =   redStr +  greenStr +  blueStr;
> 
>       System.out.println(mSet);//this is successful
> 
>       mSetx= "255255255";
> 
> 
> 
>       if (mSet ==  mSetx) {
> 
>          System.out.println("yes, recognized"); 
> 
>          //mSet = ("113000000");
> 
>       }
> 
>       colorpxl.add(mSet);// ArrayList
> 
>       mSet = ""; 
> 
> 
> 
> Thanks in advance for your response. 
> 
> 
> 
> Bob

Hi All,
Thanks for your help. This sequence 
is what was needed.   
.....
 mSet =   redStr +  greenStr +  blueStr;
      mSetx= "121000000";
      if (mSet.equals( mSetx)) {
         mSet = "";
         mSet = Integer.toString (255255255);
         System.out.println("revision "+ mSet);    
      }
      colorpxl.add(mSet);
      mSet = "";

......

With this code above, the 
datafile of decimal color pixels
recorded the intended 
changes and produces the correct image. 
Thanks again. I appreciate it.
Bob  

[toc] | [prev] | [next] | [standalone]


#2276

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2012-11-16 13:56 -0500
Message-ID<k86295$igh$1@dont-email.me>
In reply to#2275
On 11/16/2012 1:39 PM, Bob wrote:
>[...]
> Thanks for your help. This sequence
> is what was needed.
> .....
>   mSet =   redStr +  greenStr +  blueStr;
>        mSetx= "121000000";
>        if (mSet.equals( mSetx)) {
>           mSet = "";

     What's this for?  It's harmless, but it's useless because you
never use the assigned value for anything: You wipe it out in the
very next line.  You have written yourself a note, then immediately
erased and overwritten it.

     ... all of which makes me wonder whether there's something
you want accomplished, something you imagine this assignment
will accomplish for you.  If so, I've got bad news ...

>           mSet = Integer.toString (255255255);
>           System.out.println("revision "+ mSet);
>        }
>        colorpxl.add(mSet);
>        mSet = "";

     Can't tell whether this assignment is or isn't worthwhile.
If the value of `mSet' will be used again, maybe the assignment
is needed -- but then again, maybe it's just a repetition of the
same ineffectual pattern as the first one.  I still harbor a
suspicion that you're missing something.

-- 
Eric Sosman
esosman@comcast-dot-net.invalid

[toc] | [prev] | [next] | [standalone]


#2277

FromBob <bherbst65@hotmail.com>
Date2012-11-16 12:23 -0800
Message-ID<45427dcf-14ec-4f54-bb6f-862a8263c52b@googlegroups.com>
In reply to#2270
On Friday, November 16, 2012 11:05:12 AM UTC-5, Bob wrote:
> Hi All,
> 
> While making a datafile of decimal color pixel
> 
> codes for a small image, I want to change 
> 
> a particular String number that will go into the datafile 
> 
> to a different String number. 
> 
> I am doing this before it is added to the 
> 
> ArrayList. It does not seem to work and I need 
> 
> to know why. Below is my test to identify 
> 
> the String number and looking for the "if" 
> 
> statement to recognize it"
> 
>  
> 
> Here is a snip of my code:
> 
> ....
> 
>       mSet =   redStr +  greenStr +  blueStr;
> 
>       System.out.println(mSet);//this is successful
> 
>       mSetx= "255255255";
> 
> 
> 
>       if (mSet ==  mSetx) {
> 
>          System.out.println("yes, recognized"); 
> 
>          //mSet = ("113000000");
> 
>       }
> 
>       colorpxl.add(mSet);// ArrayList
> 
>       mSet = ""; 
> 
> 
> 
> Thanks in advance for your response. 
> 
> 
> 
> Bob


Hi Eric,
There might quite a few "oops"
as this is a work in progress.

Bob

import java.awt.Component;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.util.ArrayList;
import java.awt.event.*;
import java.awt.image.*;
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.io.FileOutputStream;


public class BufferedImageSto extends Component {
   ArrayList<String> colorpxl = new ArrayList<String>();
   ArrayList<String> dimensions = new ArrayList<String>();
   
   String alphaStr, redStr, greenStr, blueStr, wStr, hStr, mSet,mSetx;
   
   public static void main(String[] foo) {
      new BufferedImageSto();
   }
   
   public void printPixelARGB(int pixel) {
      
      int alpha = (pixel >> 24) & 0xff;
      alphaStr =  Integer.toString(alpha);
      
      int red = (pixel >> 16) & 0xff;
      redStr= Integer.toString(red);
      if (redStr.length() < 2) {
         redStr = "0" + redStr;}
      if (redStr.length() < 3) {
         redStr = "0" + redStr;}
      
      int green = (pixel >> 8) & 0xff;
      greenStr= Integer.toString(green);
      if (greenStr.length() < 2) {
         greenStr = "0" + greenStr;}
      if (greenStr.length() < 3) {
         greenStr = "0" + greenStr;}
      
      
      int blue = (pixel) & 0xff;
      blueStr= Integer.toString(blue);
      if (blueStr.length() < 2) {
         blueStr = "0" + blueStr;}
      if (blueStr.length() < 3) {
         blueStr = "0" + blueStr;}
      
      mSet =   redStr +  greenStr +  blueStr;
      mSetx= "121000000";
      if (mSet.equals( mSetx)) {
         mSet = "";
         mSet= Integer.toString (255255255);
         System.out.println("revision "+ mSet);    
      }
      
      //System.out.println(mSet);
      colorpxl.add(mSet);
      mSet = "";
   }
   
   private void marchThroughImage(BufferedImage image) {
      int w = image.getWidth();
      String wStr= Integer.toString(w);
      int h = image.getHeight();
      String hStr = Integer.toString(h);
      dimensions.add(wStr);
      dimensions.add(hStr);
      
      System.out.println("width, height: " + w + ", " + h);
      
      for (int i = 0; i < w; i++) {
         for (int j = 0; j < h; j++) {
            // System.out.println("x,y: " + j + ", " + i);
            int pixel = image.getRGB(i, j);
            printPixelARGB(pixel);
            System.out.println("");
         }
      }

      String respName = "ColorPixlData.txt";
      System.out.println("File Saved as "+ respName);
      System.out.println(respName);
      try {
         FileOutputStream fout = new FileOutputStream(respName);
         ObjectOutputStream out = new ObjectOutputStream(fout);
         out.writeObject(dimensions);
         out.writeObject(colorpxl);
         out.flush();
         out.close();
         repaint();
      }
      catch (IOException e) {e.printStackTrace();
      } 
   }  
   
   public BufferedImageSto() {
      try {
         // get the BufferedImage, using the ImageIO class
         BufferedImage image = ImageIO.read(this.getClass().getResource("MH0x.png"));
         marchThroughImage(image);
         
      } catch (IOException e) {
         System.err.println(e.getMessage());
      }
   }
}

[toc] | [prev] | [next] | [standalone]


#2278

FromLew <lewbloch@gmail.com>
Date2012-11-16 12:38 -0800
Message-ID<b568ec93-17d5-4b05-ac77-0447c2239e27@googlegroups.com>
In reply to#2277
Bob wrote:
> There might quite a few "oops"
> as this is a work in progress.
> 
> import java.awt.Component;
> import java.awt.image.BufferedImage;
> import javax.imageio.ImageIO;
> import java.util.ArrayList;
> import java.awt.event.*;
> import java.awt.image.*;
> import java.io.ObjectOutputStream;
> import java.io.IOException;
> import java.io.FileOutputStream;
> 
> public class BufferedImageSto extends Component {
>    ArrayList<String> colorpxl = new ArrayList<String>();

Usually the declared type of the variable should be the interface, and always it should 
be the widest type that supports the logic of the code. And variable names are supposed 
to be spelled in camel case. You really should track best practices and coding conventions.

It's recommended that variable names not be overly abbreviated.

Thus, 'List<String> colorPixel ...'

>    ArrayList<String> dimensions = new ArrayList<String>();
>    String alphaStr, redStr, greenStr, blueStr, wStr, hStr, mSet,mSetx;

Are you certain all of these should be instance attributes? Why don't you 
declare your members 'private'? (There are good reasons; I'm just wondering 
whether yours is one of them.)

>    public static void main(String[] foo) {
>       new BufferedImageSto();
>    }

Is this supposed to be a GUI program?

Don't do logic in a constructor. Constructors are for construction. Period.

What you're doing is bad because you're operating on an incomplete object.

>    public void printPixelARGB(int pixel) {
>       int alpha = (pixel >> 24) & 0xff;
>       alphaStr =  Integer.toString(alpha);
> 
>       int red = (pixel >> 16) & 0xff;
>       redStr= Integer.toString(red);
> 
>       if (redStr.length() < 2) {
>          redStr = "0" + redStr;}

You should follow the coding conventions for braces.

>       if (redStr.length() < 3) {
>          redStr = "0" + redStr;}
> 
>       int green = (pixel >> 8) & 0xff;
>       greenStr= Integer.toString(green);
>       if (greenStr.length() < 2) {
>          greenStr = "0" + greenStr;}
>       if (greenStr.length() < 3) {
>          greenStr = "0" + greenStr;}
> 
>       int blue = (pixel) & 0xff;
>       blueStr= Integer.toString(blue);
>       if (blueStr.length() < 2) {
>          blueStr = "0" + blueStr;}
> 
>       if (blueStr.length() < 3) {
>          blueStr = "0" + blueStr;}
> 
>       mSet =   redStr +  greenStr +  blueStr;

Are you doing int-to-hex conversion? Why don't the standard APIs work for you?

>       mSetx= "121000000";

Why do you reset this variable to the same value over and over and over and over and over and over?

>       if (mSet.equals( mSetx)) {
>          mSet = "";
>          mSet= Integer.toString (255255255);

Seriously?

>          System.out.println("revision "+ mSet);   

Google "Java logging".
 
>       }

>       //System.out.println(mSet);

And this comment is here because ...?

>       colorpxl.add(mSet);
>       mSet = "";
>    }
> 
>    private void marchThroughImage(BufferedImage image) {
>       int w = image.getWidth();
>       String wStr= Integer.toString(w);
>       int h = image.getHeight();
>       String hStr = Integer.toString(h);
>       dimensions.add(wStr);
>       dimensions.add(hStr);
> 
>       System.out.println("width, height: " + w + ", " + h);
> 
>       for (int i = 0; i < w; i++) {
>          for (int j = 0; j < h; j++) {
>             // System.out.println("x,y: " + j + ", " + i);

And this comment is here because ...?

>             int pixel = image.getRGB(i, j);
>             printPixelARGB(pixel);
>             System.out.println("");
>          }
>       }
> 
>       String respName = "ColorPixlData.txt";
>       System.out.println("File Saved as "+ respName);
>       System.out.println(respName);

Tell me once, tell me twice, tell me once again.

>       try {
>          FileOutputStream fout = new FileOutputStream(respName);
>          ObjectOutputStream out = new ObjectOutputStream(fout);
>          out.writeObject(dimensions);
>          out.writeObject(colorpxl);
>          out.flush();
>          out.close();
>          repaint();
> 
>       }
> 
>       catch (IOException e) {e.printStackTrace();

Follow coding conventions.

Don't ignore exceptions.

>       } 
>    }  
> 
>    public BufferedImageSto() {
>       try {
>          // get the BufferedImage, using the ImageIO class
>          BufferedImage image = ImageIO.read(this.getClass().getResource("MH0x.png"));

This might be work to do in a constructor, but in this case I don't think so.

>          marchThroughImage(image);

This is NOT work to do in a constructor.

>       } catch (IOException e) {
>          System.err.println(e.getMessage());

Don't ignore exceptions.

>       }
>    }
> }

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#2279

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2012-11-16 17:11 -0500
Message-ID<k86dln$ss4$1@dont-email.me>
In reply to#2277
On 11/16/2012 3:23 PM, Bob wrote:
> [...]
> There might quite a few "oops"
> as this is a work in progress.

     Okay.  Just a few comments; take 'em or leave 'em.

> import java.awt.Component;
> import java.awt.image.BufferedImage;
> import javax.imageio.ImageIO;
> import java.util.ArrayList;
> import java.awt.event.*;
> import java.awt.image.*;
> import java.io.ObjectOutputStream;
> import java.io.IOException;
> import java.io.FileOutputStream;
>
>
> public class BufferedImageSto extends Component {
>     ArrayList<String> colorpxl = new ArrayList<String>();
>     ArrayList<String> dimensions = new ArrayList<String>();
>
>     String alphaStr, redStr, greenStr, blueStr, wStr, hStr, mSet,mSetx;

     These Strings would probably be better off as local variables
in the methods that use them.  There are two main reasons for making
something an instance variable: Because it is part of the "state"
of the instance that persists even when no method of the class
happens to be running, or because it's a convenient way to share
something between two or more methods.

>     public static void main(String[] foo) {
>        new BufferedImageSto();
>     }
>
>     public void printPixelARGB(int pixel) {
>
>        int alpha = (pixel >> 24) & 0xff;
>        alphaStr =  Integer.toString(alpha);

     Since you never use `alphaStr', and since you use `alpha'
only to produce the never-used `alphaStr', why compute either
of them at all?

>        int red = (pixel >> 16) & 0xff;
>        redStr= Integer.toString(red);
>        if (redStr.length() < 2) {
>           redStr = "0" + redStr;}
>        if (redStr.length() < 3) {
>           redStr = "0" + redStr;}
>
>        int green = (pixel >> 8) & 0xff;
>        greenStr= Integer.toString(green);
>        if (greenStr.length() < 2) {
>           greenStr = "0" + greenStr;}
>        if (greenStr.length() < 3) {
>           greenStr = "0" + greenStr;}
>
>
>        int blue = (pixel) & 0xff;
>        blueStr= Integer.toString(blue);
>        if (blueStr.length() < 2) {
>           blueStr = "0" + blueStr;}
>        if (blueStr.length() < 3) {
>           blueStr = "0" + blueStr;}
>
>        mSet =   redStr +  greenStr +  blueStr;

     You are spending too much time doing things the hard way,
and not enough time reading the Javadoc!  *All* of the code so
far in this method could be replaced by *one* statement:

	mSet = String.format("%03d%03d%03d",
	    (pixel >> 16) & 0xff,
	    (pixel >> 8) & 0xff,
	    pixel & 0xff);

>        mSetx= "121000000";
>        if (mSet.equals( mSetx)) {
>           mSet = "";

     Purposeless assignment.

>           mSet= Integer.toString (255255255);
>           System.out.println("revision "+ mSet);
>        }
>
>        //System.out.println(mSet);
>        colorpxl.add(mSet);
>        mSet = "";

     Purposeless assignment.

>     }

     Even with the suggested shortcuts you're still working too
hard.  Let's restate the purpose: You want to produce a nine-
digit string representing the pixel's R,G,B values (ignoring A),
except that if R is 121 and B,G are both zero you want to set all
three to 255.  Let's start with ARGB and get rid of the unwanted A:

	pixel &= 0xffffff;  // A is now zeroed out

Does this match the color you want to victimize?

	if (pixel == 121 << 16) {

If so, change it to the replacement color:

	    pixel = (255 << 16) + (255 << 8) + 255;
	    // Print it if you want; doesn't seem informative.
	}

Finally, develop your nine-digit string and add it to the list:

	colorpxl.add(String.format("%03d%03d%03d",
	    pixel >> 16, (pixel >> 8) & 0xff, pixel & 0xff));

(You can do without R's `&0xff' because we know the A bits are zero.)

>
>     private void marchThroughImage(BufferedImage image) {
>        int w = image.getWidth();
>        String wStr= Integer.toString(w);
>        int h = image.getHeight();
>        String hStr = Integer.toString(h);
>        dimensions.add(wStr);
>        dimensions.add(hStr);
>
>        System.out.println("width, height: " + w + ", " + h);
>
>        for (int i = 0; i < w; i++) {
>           for (int j = 0; j < h; j++) {
>              // System.out.println("x,y: " + j + ", " + i);
>              int pixel = image.getRGB(i, j);

     Perhaps this is what you want, but it looks a bit strange.
Most raster formats work their way across the top row, then move
down and work across the second row, then the third, and so on to
the bottom of the image.  ("Interlaced" formats may jumble the row
order, but they still usually work across each row as a unit.)  Your
code, instead, marches down the leftmost column, then moves one
place rightwards and marches down the next column, and so on until
it reaches the right-hand edge.  It's possible that's your intent,
but it seems very strange.

>              printPixelARGB(pixel);
>              System.out.println("");
>           }
>        }
>
>        String respName = "ColorPixlData.txt";

     Seems a strange name for a file that will be filled with
binary garbage.  It's not really "garbage," of course: It's
a binary representation from which an ObjectInputStream could
reconstruct the `dimensions' and `colorpx1' lists and their
content.  But ".txt" ordinarily means "Something that would
make sense to a program like `NotePad' or `more'," which this
file won't.

>        System.out.println("File Saved as "+ respName);
>        System.out.println(respName);
>        try {
>           FileOutputStream fout = new FileOutputStream(respName);
>           ObjectOutputStream out = new ObjectOutputStream(fout);
>           out.writeObject(dimensions);
>           out.writeObject(colorpxl);
>           out.flush();
>           out.close();
>           repaint();

     Why?  Nothing has changed; in fact, as far as I can tell
nothing has been painted to begin with.  It's (arguably) an
actual error to do this because this method is called from
the constructor, hence the constructor has not yet finished,
hence the object being repainted hasn't been fully initialized
yet.  (See below.)

>        }
>        catch (IOException e) {e.printStackTrace();
>        }
>     }
>
>     public BufferedImageSto() {
>        try {
>           // get the BufferedImage, using the ImageIO class
>           BufferedImage image = ImageIO.read(this.getClass().getResource("MH0x.png"));
>           marchThroughImage(image);
>
>        } catch (IOException e) {
>           System.err.println(e.getMessage());
>        }
>     }

     It's not exactly "wrong," but you probably don't want to be
doing so much heavy lifting in a constructor.  The purpose of a
constructor should be to construct, but the purpose of this one
seems to load an image and spit out a file.  As things stand, I
don't see any need for a BufferedImageSto object at all: A `static'
method or two would do all the work with fewer needless trappings.

> }
>


-- 
Eric Sosman
esosman@comcast-dot-net.invalid

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.help


csiph-web