• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Text file to map colours to a PNG Image

 
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there, I'm new to Java and have been stuck on this program for several days now. I have been assigned the task of creating a program that takes in a text file of 50 lines of 50 characters, which is a combination of the 3 symbols * / and # .

These 3 symbols are then to be mapped to a colour and to save the resulting picture to a PNG file. My code does not have any syntax errors but when it runs the image file is not written to.

I would really appreciate if someone could review my code and point out where I'm going wrong:

I have tried to attached the input text file containing the characters to the forum post but it says I'm not allowed.

 
Marshal
Posts: 27365
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Charles!

Now, when you say the output file is "not written to", does that mean that it isn't created? Or that it is created but zero bytes are written to it? Or perhaps that some bytes are written to it but you don't see anything when you try to display it as a PNG file?
 
Charles Ormond
Ranch Hand
Posts: 52
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Welcome to the Ranch, Charles!

Now, when you say the output file is "not written to", does that mean that it isn't created? Or that it is created but zero bytes are written to it? Or perhaps that some bytes are written to it but you don't see anything when you try to display it as a PNG file?



Hi Paul, much appreciated.

I had created the image file previously, simply by adding it in on file explorer, and when I ran the program with the PNG file created it had zero bytes written to it.

I have now deleted it to test whether the program even creates the file, and can confirm that it doesn't.
 
Saloon Keeper
Posts: 9538
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
write() returns false if the write fails, check for that. Also enclose write() in a try/catch block and print a stack trace if an exception is thrown.
 
Carey Brown
Saloon Keeper
Posts: 9538
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Charles Ormond
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm writing a program to take an input of a text file containing a 50 by 50 block of three different characters: # , * and / , and colour each pixel of an output PNG file in accordance with each symbol so that there is a colour associated with each of the characters.
There is no syntax error or any message showing up in the console, and the file is being created. However, all that is being written to it is a diagonal green line, but I can tell from the text file that there should be more to the image than that. I can add in the content of the text file if this would be helpful.

Here is my code:

 
Charles Ormond
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Contents of superimage.txt:

 
Carey Brown
Saloon Keeper
Posts: 9538
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your loops are terribly messed up. You have 4 loops, you only need 2, one for X and one for Y. Your Y loop must come first with the X loop being an inner loop. You were incrementing X and Y, both in the for() and in the body of the loop.

 
Rancher
Posts: 4874
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Other post may be from different OP.
 
Charles Ormond
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for helping with the code Carey, I had been stuck on this for at least a week! This has resolved the issues and the file seems to be writing properly now.

Much appreciated
 
reply
    Bookmark Topic Watch Topic
  • New Topic