Picture imperfect

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
Shrewtality
Posts: 41
Joined: Wed Oct 17, 2007 4:15 am UTC
Location: Somewhere between 4 and 5

Picture imperfect

Postby Shrewtality » Sat Oct 20, 2007 10:21 pm UTC

Using this code i'm using the values for the pixel(x,y) to determine it's color. This creates an image and paints it into an applet. My question is how can I save this image as a .bmp,.jpg,etc. The applet doesn't have a right click -> save picture as feature.

edit: Also assume I don't want to take the print screen-paste route.

Code: Select all

import java.applet.Applet;
import java.awt.*;

public class Driver extends Applet
{
   public void paint (Graphics g)
    {
      int x,y,xres,yres;
      double number;

      xres=1000;
       yres=xres*3/4;
       
       for(x=0;x<xres;x++)
          for(y=0;y<yres;y++)
             {
                number = x*y;
                try
                {
                   Color mycolor = new Color(0,0,(int)(number)%255);
                    g.setColor(mycolor);
                     g.drawLine(x,y,x,y);
                }
                catch(IllegalArgumentException e)
                {
                   g.setColor(Color.black);
                   g.drawLine(x,y,x,y);                   
                }
                
             }
   
    }
}


edit:
well for the last 4 hours I was able to piece together bits and pieces of information to make which works sufficiently well

Code: Select all

import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

public class Driver2 extends Applet
{
   private boolean bSaved = false;
   
   public void paint (Graphics g)
    {
      int x,y,xres,yres;
      double number;

      xres=1000;
       yres=xres*3/4;
       
       BufferedImage bi = null;
       Graphics g2 = null;
       if (!bSaved)
       {
          bi = new BufferedImage(xres, yres, BufferedImage.TYPE_INT_RGB);
          g2 = bi.getGraphics();
          bSaved = true;
       }
       
       for(x=0;x<xres;x++)
          for(y=0;y<yres;y++)
             {
                number = x^2*y;
                try
                {
                   Color mycolor = new Color(0,(int)(number)%255,0);
                    g.setColor(mycolor);
                     g.drawLine(x,y,x,y);
                     
                     if (g2 != null)
                     {
                        g2.setColor(mycolor);
                        g2.drawLine(x, y, x, y);
                     }
                }
                catch(IllegalArgumentException e)
                {
                   g.setColor(Color.black);
                   g.drawLine(x,y,x,y);                   
                        if (g2 != null)
                     {
                        g2.setColor(Color.black);
                        g2.drawLine(x, y, x, y);
                     }
                }
                
             }
       
       if (bi != null)
       {
          try
          {
             File f = new File("output.png");
             ImageIO.write(bi, "png", f);
          }
          catch (Exception e) {}
       }
    }
}


Pretty cool picture :D

Image
Image
I approach infinity around 5, that way i can get through with it sooner.
me: I wouldnt know where to begin to respond to that... perhaps after the last digit of pi...

User avatar
Pathway
Leon Sumbitches...?
Posts: 647
Joined: Sun Oct 15, 2006 5:59 pm UTC

Re: Picture imperfect

Postby Pathway » Tue Oct 23, 2007 5:25 am UTC

Code: Select all

catch (Exception e) {}


:?
SargeZT wrote:Oh dear no, I love penguins. They're my favorite animal ever besides cows.

The reason I would kill penguins would be, no one ever, ever fucking kills penguins.

User avatar
bavardage
Posts: 253
Joined: Sun Apr 15, 2007 11:38 pm UTC
Contact:

Re: Picture imperfect

Postby bavardage » Tue Oct 23, 2007 3:40 pm UTC

Pathway wrote:

Code: Select all

catch (Exception e) {}


:?


At least I have the decency to put a comment within the curly braces.

Code: Select all

catch (Exception e) {
            //who cares ?
         }
'It can't be software incompatibility - the Trodden Spiral was designed for concentric rings, idiot ...'

User avatar
Shrewtality
Posts: 41
Joined: Wed Oct 17, 2007 4:15 am UTC
Location: Somewhere between 4 and 5

Re: Picture imperfect

Postby Shrewtality » Tue Oct 23, 2007 6:39 pm UTC

Ahh I'm a freshman CS student - I haven't really gotten into the habit of commenting my code. At least I don't put comments like

Code: Select all

//Instantiates an integer variable named x


:?
Image
I approach infinity around 5, that way i can get through with it sooner.
me: I wouldnt know where to begin to respond to that... perhaps after the last digit of pi...


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 11 guests