Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Save graphic image

Posted on 1998-03-23
1
Medium Priority
?
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 6

Accepted Solution

by:
jpk041897 earned 100 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Learn how to create interesting presentations by including videos to keep your audience engaged using Prezi. Select "Insert" from the top menu in your Prezi editor: Select "YouTube Video": Paste the video URL into the prompt: "Select "Insert":…
Suggested Courses

609 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