Solved

Save graphic image

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
File conversion question 5 57
changePi Challenge 15 77
AutoCad licenses 9 40
Java passing arguments (type error) 2 35
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Learn how to set up basic frames and paths in Prezi and understand the open space that Prezi allows you to create presentations in.

747 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

14 Experts available now in Live!

Get 1:1 Help Now