0

I wrote a program that reads an image from command line and want to read each pixel to draw a rectangle of the respective colour to "recreate" the image from rectangles.

However, although the rectangles have the correct size, every pixel seems to be black. At least, what I see in the output panel is a black picture that has the same size as the input picture.

    class AppDrawPanel extends JPanel {  

        private BufferedImage bi;

        /* ... */

        public void loadAPPImage( String s ) throws IOException{
             bi = ImageIO.read(new File(s));
        }

        @Override
        public void paint(Graphics g){
           Graphics2D g2 = (Graphics2D) g;
           int w = bi.getWidth();
           int h = bi.getHeight();

           for( int x = 0; x < w; x++){
                for ( int z = 0; z < h; z++ ){
                        Color c = new Color(bi.getRGB(x, z));
                        super.setForeground(c);
                        g2.fillRect(x, z, 3, 3);  
                }    
           }
        }
    }

And the main function:

    public static void main( String[] args ) throws IOException{         
        /* ... */

        AppDrawPanel draw = new AppDrawPanel();
        draw.loadAPPImage(args[0]);
        frame.add(draw);        
        /* ... */
    } 

where /* ... */ represents code that has nothing to do with drawing the rectangles or reading the image.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
TheWaveLad
  • 966
  • 2
  • 14
  • 39
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 2) One way to get image(s) for an example is to hot-link to the images seen in [this answer](http://stackoverflow.com/a/19209651/418556). 3) Given this is a Q&A site, it's best to from an explicit question like 'How to draw the boxes so they aren't black?' or 'What is the error?'. What is your question? – Andrew Thompson Jun 29 '14 at 09:37
  • 1
    The `super.setForeground(c);` should probably simply be `g2.setColor(c)`. – Marco13 Jun 29 '14 at 12:28
  • @Marco13 thank you, it works :) @ Andrew Thompson Thank you for your advices :) – TheWaveLad Jun 29 '14 at 13:16
  • Nevertheless, have a look at the answer from trashgod. Simply painting the image *larger* is **much** more efficient than painting thousands of rectangles. (Although, based on the question, one has to consider the possibility that you chose your approach intentionally...) – Marco13 Jun 29 '14 at 13:48
  • @Marco13 yes, it's a homework and I had to do it with rectangles, because we had to implement different black/white-functions ;) – TheWaveLad Jun 29 '14 at 14:41
  • The scale functions in `mouseMoved()` will also work for hit testing in `mouseClicked()`. – trashgod Jun 29 '14 at 16:18

1 Answers1

3

In this related example, the width and height of each pixel is scaled by an arbitrary factor of 10. The method drawImage() then scales the image to the component's preferred size. As an exercise, override getPreferredSize() to return an appropriate dimension:

new Dimension(imgW * 10, imgH * 10);

Also consider making the arbitrary factor a class-level property.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045