Solved

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

Posted on 1998-03-10
3
315 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
3 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 30 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

763 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

11 Experts available now in Live!

Get 1:1 Help Now