?
Solved

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

Posted on 1998-03-10
3
Medium Priority
?
336 Views
Last Modified: 2010-04-16
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
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
3 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 90 total points
ID: 1233488
Two possibilities.  First, if you are using JDK 1.1, you have the object serialization capabilities at your disposal.  This makes adding the ability of objects to save/restore themselves quite straightforward.  If not, you need to write some type of routine which will walk your instanciated classes and dump them to an output file in some format of your choosing.  

If you have just drawn object on the window, however, you won't be able to do either of these things, you need to have two things for an application that can save/restore a view.  You need a data structure that is the collection of drawing objects, and you need a rendering of them on the screen for the user to see.  For SAVE/RESTORE functionality, you always generate the view from the data.
0
 
LVL 6

Expert Comment

by:gadio
ID: 1233489
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.

0
 

Author Comment

by:ninh
ID: 1233490
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);
      }
}


0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

765 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