Solved

Save graphic image

Posted on 1998-03-23
1
209 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 96
JavaScript/Java - Changing an image background color 4 66
Change size 15 41
Java: anonymous class 4 17
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

808 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