Link to home
Start Free TrialLog in
Avatar of ninh
ninh

asked on

How to save drawing (i.e: line, rectangles, circles) into a permanent file

Hi,

I really want to find a way to save a drawing pictures(i.e:  a circle, lines, polygon, etc..) into a permanent file so that I can open that file with that drawing that I have drawn.  
It is a stand still picture, no need to move use the mouse or anything.

The language I used is Java.  

Can you give me some hints, or information (i.e: web address, examples, source codes, specific Java command)

Many thanks
ASKER CERTIFIED SOLUTION
Avatar of jhance
jhance

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
Avatar of gadio
gadio

Let me add to jhance's answer that there are generally two approaches possible when saving graphics. One is to treat the
graphics as raster image and the second (jhance approach) is to treat it as a display list, and save the objects that compose the image. Each of these approaches has their upsides and downsides. However, while the proposed answer is very good for graphic editors (you can edit the previously drawn objects - e.g. application: AutoCad) it is not good for raw graphics editing (raster graphics editing - e.g. application: photoshop). You should first decide which direction is better for your application and then chose jhance's answer or go to the other direction which involves saving raw graphic areas.

Avatar of ninh

ASKER

Can I send the program as an specific example that I like to save the picture that I have drawn.  This program will run but it can not save the drawing into the permanent file.  I really hope that  you could give some further advices.

Thanks


/*
* @(#)Whiteboard.java   1/11/96 George C.
*/

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Whiteboard extends Frame{
      
      Whiteboard(String title){
            super(title);
            setSize(500, 300);
            setLocation(100,100);
            DrawPanel dp = new DrawPanel();
            dp.setLayout(new BorderLayout());
            setLayout(new BorderLayout());
            this.add("Center", dp);
            this.add("South",new DrawControls(dp));
            setVisible(true);
      }
      
      static class WindowCloser extends WindowAdapter {
            public void windowClosing(WindowEvent w) {
                  System.exit(0);
            }
      }

      public static void main(String args[]) {
            Whiteboard whiteboard = new Whiteboard("WhiteBoard");
            whiteboard.addWindowListener(new WindowCloser());
      }
}


abstract class GraphicalObject {
      Color color;
      Color fillColor;
      int penWidth;
      boolean filled;
      
      int x,y,xd,yd;  //bounding box or other relevant coordinates
      
      // Note: There is no constructor because we create instances using the newInstance() method of Class Class
      // we use the following method to initialize them after they're created.
      void init(int x, int y, Color color, Color fillColor, boolean filled, int penWidth) {
            this.color = color;
            this.fillColor = fillColor;
            this.filled = filled;
            this.penWidth = penWidth;
            
            this.x = x;
            this.y = y;
            this.xd = 0;
            this.yd = 0;
      }
      
      void newPoint(int xx, int yy) {
            xd = xx-x;
            yd = yy-y;
      }
      
      int xLeft() {
            if (xd >= 0) return x; else return x+xd;
      }

      int yTop() {
            if (yd >= 0) return y; else return y+yd;
      }
      
      int width() {
            return Math.abs(xd);
      }

      int height() {
            return Math.abs(yd);
      }

      abstract void draw(Graphics g); // to satisfy syntax checks
}

class Line extends GraphicalObject {

      void draw(Graphics g) {
            g.setColor(color);
            g.drawLine(x,y,x+xd,y+yd);
      }
}

class Rect extends GraphicalObject {
      
      void draw(Graphics g) {
            g.setColor(color);
            g.drawRect(xLeft(),yTop(),width(),height());
            if(filled) {
                  g.setColor(fillColor);
                  g.fillRect(xLeft()+1,yTop()+1,width()-1,height()-1);
            }
      }
}

class RoundRect extends GraphicalObject {
      int arcWidth = 20, arcHeight = 20;
      
      void draw(Graphics g) {
            g.setColor(color);
            g.drawRoundRect(xLeft(),yTop(),width(),height(),arcWidth,arcHeight);
            if(filled) {
                  g.setColor(fillColor);
                  g.fillRoundRect(xLeft()+1,yTop()+1,width()-1,height()-1,arcWidth,arcHeight);
            }
      }
}

class Oval extends GraphicalObject {
      
      void draw(Graphics g) {
            g.setColor(color);
            g.drawOval(xLeft(),yTop(),width(),height());
            if(filled) {
                  g.setColor(fillColor);
                  g.fillOval(xLeft()+1,yTop()+1,width()-1,height()-1);
            }
      }
}

class Ink extends GraphicalObject {
      Vector lines = new Vector();            // we maintain two representations
      Polygon polyLines = new Polygon();      // so that we can use Java's built-in polygon drawing
      
      void init(int x, int y, Color color, Color fillColor, boolean filled, int penWidth) {
            super.init(x, y, color, fillColor, filled, penWidth);
            lines.addElement(new Point(x,y));
            polyLines.addPoint(x,y);
      }
      
      void newPoint(int xx, int yy) {
            lines.addElement(new Point(xx-x,yy-y));
            polyLines.addPoint(xx,yy);
      }
            
      void draw(Graphics g) {
            g.setColor(color);
            g.drawPolygon(polyLines);
            /* no filled ink objects for the present - their feedback is too confusing!
            if(filled) {
                  g.setColor(fillColor);
                  g.fillPolygon(polyLines);
            }
            */
      }
}

class Text extends GraphicalObject {
      String text = "";
      Font font;
      
      void setText(String text) {
            this.text = text;
      }
                  
      void newPoint(int xx, int yy) {
            x = xx;
            y = yy;
      }
      
      void adjust(Color color, Color fillColor, boolean filled, int penWidth) {
            this.color = color;
            this.fillColor = fillColor;
            this.filled = filled;
            this.penWidth = penWidth;
      }

      void draw(Graphics g) {
            //System.out.println("drawing text "+text+" at "+x+","+y);
            g.setColor(color);
            g.drawString(text,x,y);
      }
}

class gController extends Object {
      // an instance of this class is created when the DrawPanel gets a MOUSE_DOWN event.
      // subsequent MOUSE_DRAG cause invocations of the nextPoint method
      // MOUSE_UP invokes lastPoint
      // to produce a new graphical object and appends it to the drawnObjects vector.
      
      GraphicalObject currentObject;
      Graphics g;
      String currentShape;
      DrawPanel currentView;
      
      gController(int x, int y, boolean mod, DrawPanel view) {
            currentView = view;
            currentShape = currentView.controls.currentShape();
            g = currentView.getGraphics();
            try {
                  currentObject = (GraphicalObject)Class.forName(currentShape).newInstance();
            } catch (Exception e) {System.out.println(e.getMessage());};
            currentObject.init(x, y, currentView.controls.currentColor(), // line color
            currentView.controls.currentFillColor(), // fill color
            currentView.controls.currentFillMode(), //fill mode
            1 // penWidth
            );

            if(currentObject instanceof Text) {
                  Text t = (Text)currentObject;
                  t.setText(currentView.controls.text.getText());
            }

            // setXORMode doesn't seem to work for text, but we persist ...
            g.setXORMode(currentView.getBackground());
            currentObject.draw(g);
      }
      
      void nextPoint(int x, int y, boolean mod) {
            g.setXORMode(currentView.getBackground());
            currentObject.draw(g);
            currentObject.newPoint(x, y);
            currentObject.draw(g);
      }
      
      void lastPoint(int x, int y, boolean mod) {
            nextPoint(x,y,mod);
            g.setPaintMode();
            currentObject.draw(g);
            currentView.drawnObjects.addElement(currentObject);
      }
}

class DrawPanel extends Panel {
      Vector drawnObjects = new Vector();
      gController currentController = null;
      DrawControls controls;
      DrawPanel me = this;
      
      public DrawPanel() {
            setBackground(Color.white);
            addMouseMotionListener(new MouseMotionEventHandler());
            addMouseListener(new MouseEventHandler());
      }
      
      void setControls(DrawControls controls) {
            this.controls = controls;
      }
      
      public void paint(Graphics g) {
            int n = drawnObjects.size();
      
            // System.out.println("DrawPanel paints");
            /* draw all the current objects */
            g.setPaintMode();
            for (int i=0; i < n; i++) {
                  GraphicalObject go = (GraphicalObject)drawnObjects.elementAt(i);
                  //System.out.println("drawing in object "+go);
                  if (go != null) go.draw(g); // could have been deleted
            }
      }      

      class MouseMotionEventHandler extends MouseMotionAdapter {
            public void mouseDragged(MouseEvent m) {
                  // System.out.println("mouse drag in DrawPanel");
                  currentController.nextPoint(m.getX(), m.getY(), m.isShiftDown());
            }
      }
      
      class MouseEventHandler extends MouseAdapter {
            public void mousePressed(MouseEvent m) {
                  // System.out.println("mouse down in DrawPanel");
                  currentController = new gController(m.getX(), m.getY(), m.isShiftDown(), me);
            }
                              
            public void mouseReleased(MouseEvent m) {
                  currentController.lastPoint(m.getX(), m.getY(), m.isShiftDown());
                  repaint();
            }
      }

}

class ColorSelector extends Panel {
      CheckboxGroup colors;
      
      ColorSelector() {
            super();
            setLayout(new GridLayout(1,7));
            colors = new CheckboxGroup();
            Checkbox b;
            add(b = new Checkbox(null, colors, false));
            b.setBackground(Color.red);
            add(b = new Checkbox(null, colors, false));
            b.setBackground(Color.green);
            add(b = new Checkbox(null, colors, false));
            b.setBackground(Color.blue);
            add(b = new Checkbox(null, colors, false));
            b.setBackground(Color.pink);
            add(b = new Checkbox(null, colors, false));
            b.setBackground(Color.orange);
            add(b = new Checkbox(null, colors, true));
            b.setBackground(Color.black);
            add(b = new Checkbox(null, colors, false));
            b.setBackground(Color.white);
      }
}

class DrawControls extends Panel {
      ColorSelector lineColorSelector;
      ColorSelector fillColorSelector;
      Choice shapes;
      Checkbox filled;
      TextField text;
      
      DrawControls(DrawPanel theDrawing) {
            theDrawing.setControls(this);
            setLayout(new GridLayout(3,3,4,4));
            setBackground(Color.lightGray);
      
            shapes = new Choice();
            shapes.add("Line");
            shapes.add("Rect");
            shapes.add("RoundRect");
            shapes.add("Oval");
             //shapes.add("Ink");
            shapes.add("Text");
            shapes.setBackground(Color.lightGray);
            filled = new Checkbox("Filled");
            filled.setBackground(Color.lightGray);
            text = new TextField("", 10);
      
             add(shapes); add(new Label("Lines:", Label.RIGHT)); add(lineColorSelector = new ColorSelector());
            add(text); add(new Label("Fill:", Label.RIGHT));
            add(fillColorSelector = new ColorSelector());
            add(filled);
      }
      
      Color currentColor(){
            return lineColorSelector.colors.getSelectedCheckbox().getBackground();
      }

      Color currentFillColor(){
            return fillColorSelector.colors.getSelectedCheckbox().getBackground();
      }
      
      String currentShape(){
            return shapes.getSelectedItem();
      }
      
      boolean currentFillMode(){
            return filled.getState();              
      }

      public void paint(Graphics g) {
            Rectangle r = getBounds();
      
            g.setColor(Color.lightGray);
            g.draw3DRect(0, 0, r.width, r.height, false);
      }
}