Link to home
Start Free TrialLog in
Avatar of ninh
ninh

asked on

Save graphic image

Hi,

This is a file that I am trying to save the graphic into a permanent file.  As I have attempt to copy the Vector lines, and Vector colors into an an array of object.  And I intend to save them into a random acccess file, but I don't think I can.

I also have tried to copy Pixel but again, I am not confident that it will work.  So can you please help me?

Neil


import java.io.*;
import java.awt.*;
import java.util.*;
import java.lang.*;



public class DrawTest extends Frame  {

    // constructor
    public DrawTest()  {
    super(" Draw apllication");
    setUp();
}
//-------------------------------------------------------------------------//

    public void setUp (){
    DrawPanel drawnPanel = new DrawPanel();  // calling DrawPanel function
    setLayout(new BorderLayout());
      add("Center", drawnPanel);                    // add drawPanel to the center
      add("South",new DrawControls(drawnPanel));    // add drawControl south
    resize(300, 300);
    show();
   }
//--------------------------------------------------------------------//

   public boolean handleEvent(Event e) {
      switch (e.id) {
        case Event.WINDOW_DESTROY:
          System.exit(0);
          return true;
        default:
          return false;
      }
 }

//-----------------------------------------------------------------------//

    public static void main(String args[])  {
    DrawTest drawTest = new DrawTest();
    //add("Center", drawTest);

    }
}

//--------------------------------------------------------------------------//
   // Create a different class that use Panel method

 class DrawPanel extends Panel  {
    public static final int LINES = 0;
    public static final int POINTS = 1;
    int mode = LINES;   // setting mode to 0
    Vector lines = new Vector();
    Vector colors = new Vector();
    int x1, y1;
    int x2, y2;
    int xL, yL;
    int np = 0;
    RandomAccessFile output;
    Object linesArray[];// = new String[100] ;
    Object colorsArray[]; // = new String[100];

     Image myImage;


   // constructor for creating the panel
 public DrawPanel()  {
    setBackground(Color.white);

   }

   //-----------------------------------------------------------------------//
   // set modes to draw
 public void setDrawMode(int mode) {
    switch (mode)
    {
        case LINES:
        case POINTS:
        this.mode = mode;
        break;
        default:
        throw new IllegalArgumentException();
    }
   }

   //-----------------------------------------------------------------------//
public void locations(){
   System.out.println(" x1 "+ x1);
   System.out.println(" y1 "+ y1);
   System.out.println(" x2 "+ x2);
   System.out.println(" y2 "+ y2);
   System.out.println(" xL"+ xL);
   System.out.println(" yl "+ yL);
}


//public void PixelGrabber(Image img, int x, int y, int w, int h, int[] pix, int off, int set){

//}


public void Pixel(){
  System.out.println("grapPixel ");

 lines.copyInto(linesArray);
 colors.copyInto(colorsArray);
 try {
    output = new RandomAccessFile(np + ".dat", "rw");
 }
 catch (IOException e){
  System.err.println(e.toString());
  System.exit(1);
 }
 writeVector();

/*
int width = myImage.getWidth(this);
int height = myImage.getHeight(this);
int x = 0;
int y = 0;

int ary[] = new int [width * height];
PixelGrabber grabber = new PixelGrabber(myImage,x,y,width,
                          height,ary,0,width);

try{
 grabber.grabPixels();
}catch(InterruptedException e){}

int status  = grabber.status();

if((status & ImageObserver.ABORT) || (status & ImageObserver.ERROR)){
System.out.println(" Problem in fetching Image pixels");
}
else{
 //for(int i = 0; i < ary.length;i++){
 System.out.println("bnbbbbbbbb");
 //}
}


*/

}


public void writeVector(){
try {
 writeObject();
 }
 catch(IOException e){
  System.err.println(e.toString());
 }
}

public void writeObject() throws IOException
   {
    output.writeInt(np); //write the password number
    output.writeBytes(linesArray);
    output.writeBytes(colorsArray);
   }



public void readObject() throws IOException,
                        ClassNotFoundException{
     System.out.println("mmm");
}



public boolean handleEvent(Event e)  {
    switch(e.id)
    {
      case Event.MOUSE_DOWN:  // if the mouse button is pressed down

         switch (mode) {
                  case LINES:
                  x1 = e.x;
                  y1 = e.y;
                  x2 = -1;

                  locations();
                  System.out.println(" Mouse down, Lines");
                  break;

                  case POINTS:

                  default:
                  colors.addElement(getForeground());
                  lines.addElement(new Rectangle(e.x, e.y, -1, -1));
                  x1 = e.x;
                  y1 = e.y;
                  repaint();    // might not work
                 System.out.println(" Mouse down, default line");
                  locations();
                  break;
                 }
               return true;

         case Event.MOUSE_UP:
            switch(mode) {

                   case LINES:
                   colors.addElement(getForeground());
                   lines.addElement(new Rectangle(x1, y1, e.x, e.y));
                   x2 = xL = -1;
                   System.out.println(" Mouse up ");

                   locations();

                   case POINTS:
                   default:
                   break;
                }
                repaint();
                return true;

          case Event.MOUSE_DRAG:
            switch (mode)  {

                 case LINES:
                 xL = x2;
                 yL = y2;
                 x2 = e.x;
                 y2 = e.y;
                 System.out.println(" Mouse drag ");
                 locations();
                 break;

                 case POINTS:

                 default:
                 colors.addElement(getForeground());
                 lines.addElement(new Rectangle(x1, y1, e.x, e.y));
                 x1 = e.x;
                 y1 = e.y;
                  System.out.println(" Mouse drag: default ");
                 locations();
                 break;
            }
             repaint();
             return true;

           case Event.WINDOW_DESTROY:
           System.exit(0);
           return true;
           default:
           return false;
     }
   }

//-------------------------------------------------------------------//

public void paint(Graphics g) {
       np = lines.size();

    System.out.println(" np: " + np);

      /* draw the current lines */
      g.setColor(getForeground());
      g.setPaintMode();


      for (int i=0; i < np; i++) {
          Rectangle p = (Rectangle)lines.elementAt(i);
          g.setColor((Color)colors.elementAt(i));

          if (p.width != -1) {
            g.drawLine(p.x, p.y, p.width, p.height);
          }
          else {
            g.drawLine(p.x, p.y, p.x, p.y);
          }
      }


      if (mode == LINES) {
          g.setXORMode(getBackground());
          if (xL != -1) {
            g.drawLine(x1, y1, xL, yL);       /* erase the last line. */
         }
          g.setColor(getForeground());
          g.setPaintMode();
          if (x2 != -1) {
            g.drawLine(x1, y1, x2, y2);
          }
        }
    }
}

//----------------------------------------------------------------//

class DrawControls extends Panel {
    DrawPanel target;
    Button Save = new Button("Save");


    public DrawControls(DrawPanel target) {
      this.target = target;

      setLayout(new FlowLayout());
      setBackground(Color.lightGray);
      target.setForeground(Color.red);
      CheckboxGroup group = new CheckboxGroup();
      Checkbox b;


    add(Save);

      add(b = new Checkbox(null, group, false));
      b.setBackground(Color.red);
      add(b = new Checkbox(null, group, false));
      b.setBackground(Color.green);
      add(b = new Checkbox(null, group, false));
      b.setBackground(Color.blue);
      add(b = new Checkbox(null, group, false));
      b.setBackground(Color.pink);
      add(b = new Checkbox(null, group, false));
      b.setBackground(Color.orange);
      add(b = new Checkbox(null, group, true));
      b.setBackground(Color.black);
      target.setForeground(b.getForeground());

      Choice shapes = new Choice();
      shapes.addItem("Lines");
      shapes.addItem("Points");
      shapes.setBackground(Color.lightGray);
      add(shapes);

    }

//---------------------------------------------------------//

    public void paint(Graphics g) {
      Rectangle r = bounds();

      g.setColor(Color.lightGray);
      g.draw3DRect(0, 0, r.width, r.height, false);
    }

//---------------------------------------------------------------------//

    public boolean handleEvent(Event e) {
      if (e.target instanceof Checkbox) {
          target.setForeground(((Component)e.target).getBackground());
      }
      else if (e.target instanceof Choice) {
          String choice = (String)e.arg;

          if (choice.equals("Lines")) {
            target.setDrawMode(DrawPanel.LINES);
          } else if (choice.equals("Points")) {
            target.setDrawMode(DrawPanel.POINTS);
          }

      }
      else if(e.target instanceof Button){
       if(e.target == Save){
       System.out.println("mmmmmmm");
      target.Pixel();
      return true;
      }
 }
 return true;
 }
 }













ASKER CERTIFIED SOLUTION
Avatar of jpk041897
jpk041897

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial