Solved

Save graphic image

Posted on 1998-03-23
1
206 Views
Last Modified: 2013-11-19
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;
 }
 }













0
Comment
Question by:ninh
1 Comment
 
LVL 6

Accepted Solution

by:
jpk041897 earned 50 total points
ID: 1233726
Your encoder is all wrong. You need to write a GIG or JPEG encoder.

If you are attempting this under JDK 1.02 then there is no good solution. Any solution requires the use of PixelGrabber which was buggy in this version of the SDK. So for 1.02 you will ghave to use native methods.

For 1.1.x+ you can use the following GIF encoder class at:

http://www.acme.com/java/software/Acme.JPM.Encoders.GifEncoder.html

or  the one at:

http://developer.javasoft.com/developer/techDocs/SearchData/dd/awt/197.html

Note: you will need to register (free) at :

http://developer.javasoft.com/developer/index.html

to acces the 197.html URL.


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now