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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.addWindowListen er(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(),he ight());
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(),yT op(),width (),height( ),arcWidth ,arcHeight );
if(filled) {
g.setColor(fillColor);
g.fillRoundRect(xLeft()+1, yTop()+1,w idth()-1,h eight()-1, arcWidth,a rcHeight);
}
}
}
class Oval extends GraphicalObject {
void draw(Graphics g) {
g.setColor(color);
g.drawOval(xLeft(),yTop(), width(),he ight());
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("draw ing 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.curre ntShape();
g = currentView.getGraphics();
try {
currentObject = (GraphicalObject)Class.for Name(curre ntShape).n ewInstance ();
} catch (Exception e) {System.out.println(e.getM essage()); };
currentObject.init(x, y, currentView.controls.curre ntColor(), // line color
currentView.controls.curre ntFillColo r(), // fill color
currentView.controls.curre ntFillMode (), //fill mode
1 // penWidth
);
if(currentObject instanceof Text) {
Text t = (Text)currentObject;
t.setText(currentView.cont rols.text. getText()) ;
}
// setXORMode doesn't seem to work for text, but we persist ...
g.setXORMode(currentView.g etBackgrou nd());
currentObject.draw(g);
}
void nextPoint(int x, int y, boolean mod) {
g.setXORMode(currentView.g etBackgrou nd());
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.a ddElement( currentObj ect);
}
}
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("DrawPa nel paints");
/* draw all the current objects */
g.setPaintMode();
for (int i=0; i < n; i++) {
GraphicalObject go = (GraphicalObject)drawnObje cts.elemen tAt(i);
//System.out.println("draw ing 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.nextPoin t(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.lastPoin t(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.gree n);
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.oran ge);
add(b = new Checkbox(null, colors, true));
b.setBackground(Color.blac k);
add(b = new Checkbox(null, colors, false));
b.setBackground(Color.whit e);
}
}
class DrawControls extends Panel {
ColorSelector lineColorSelector;
ColorSelector fillColorSelector;
Choice shapes;
Checkbox filled;
TextField text;
DrawControls(DrawPanel theDrawing) {
theDrawing.setControls(thi s);
setLayout(new GridLayout(3,3,4,4));
setBackground(Color.lightG ray);
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.g etSelected Checkbox() .getBackgr ound();
}
Color currentFillColor(){
return fillColorSelector.colors.g etSelected Checkbox() .getBackgr ound();
}
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);
}
}
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.addWindowListen
}
}
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(),
if(filled) {
g.setColor(fillColor);
g.fillRect(xLeft()+1,yTop(
}
}
}
class RoundRect extends GraphicalObject {
int arcWidth = 20, arcHeight = 20;
void draw(Graphics g) {
g.setColor(color);
g.drawRoundRect(xLeft(),yT
if(filled) {
g.setColor(fillColor);
g.fillRoundRect(xLeft()+1,
}
}
}
class Oval extends GraphicalObject {
void draw(Graphics g) {
g.setColor(color);
g.drawOval(xLeft(),yTop(),
if(filled) {
g.setColor(fillColor);
g.fillOval(xLeft()+1,yTop(
}
}
}
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("draw
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.curre
g = currentView.getGraphics();
try {
currentObject = (GraphicalObject)Class.for
} catch (Exception e) {System.out.println(e.getM
currentObject.init(x, y, currentView.controls.curre
currentView.controls.curre
currentView.controls.curre
1 // penWidth
);
if(currentObject instanceof Text) {
Text t = (Text)currentObject;
t.setText(currentView.cont
}
// setXORMode doesn't seem to work for text, but we persist ...
g.setXORMode(currentView.g
currentObject.draw(g);
}
void nextPoint(int x, int y, boolean mod) {
g.setXORMode(currentView.g
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.a
}
}
class DrawPanel extends Panel {
Vector drawnObjects = new Vector();
gController currentController = null;
DrawControls controls;
DrawPanel me = this;
public DrawPanel() {
setBackground(Color.white)
addMouseMotionListener(new
addMouseListener(new MouseEventHandler());
}
void setControls(DrawControls controls) {
this.controls = controls;
}
public void paint(Graphics g) {
int n = drawnObjects.size();
// System.out.println("DrawPa
/* draw all the current objects */
g.setPaintMode();
for (int i=0; i < n; i++) {
GraphicalObject go = (GraphicalObject)drawnObje
//System.out.println("draw
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.nextPoin
}
}
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.lastPoin
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.gree
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.oran
add(b = new Checkbox(null, colors, true));
b.setBackground(Color.blac
add(b = new Checkbox(null, colors, false));
b.setBackground(Color.whit
}
}
class DrawControls extends Panel {
ColorSelector lineColorSelector;
ColorSelector fillColorSelector;
Choice shapes;
Checkbox filled;
TextField text;
DrawControls(DrawPanel theDrawing) {
theDrawing.setControls(thi
setLayout(new GridLayout(3,3,4,4));
setBackground(Color.lightG
shapes = new Choice();
shapes.add("Line");
shapes.add("Rect");
shapes.add("RoundRect");
shapes.add("Oval");
//shapes.add("Ink");
shapes.add("Text");
shapes.setBackground(Color
filled = new Checkbox("Filled");
filled.setBackground(Color
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.g
}
Color currentFillColor(){
return fillColorSelector.colors.g
}
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);
}
}
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.