1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.devaki.nextobjects.util;
22
23 import java.io.File;
24 import java.awt.Point;
25 import java.awt.Rectangle;
26 import java.awt.image.BufferedImage;
27 import java.awt.Graphics2D;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.devaki.nextobjects.workspace.models.BaseModel;
31 import org.devaki.nextobjects.workspace.models.objects.BaseObject;
32 import org.devaki.nextobjects.workspace.models.graphics.ClassView;
33 import org.devaki.nextobjects.workspace.models.graphics.ObjectView;
34
35 /***
36 * This class is responsible for drawing models to files.
37 *
38 * @author <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
39 *
40 */
41
42 public class NOImageTransform
43 {
44 /***
45 * The Log4J logger
46 */
47 private static Log logger =
48 LogFactory.getLog(NOImageTransform.class.getName());
49
50 /***
51 * Dummy constructor
52 */
53 public NOImageTransform()
54 {
55 }
56
57 /***
58 * Move the model to the upper left corner to get the samllest image.
59 *
60 * @param pPDM the context model
61 * @param xOffset the X offset
62 * @param yOffset the Y offset
63 */
64 private static void translateModel(
65 BaseModel pModel,
66 int xOffset,
67 int yOffset)
68 {
69 BaseObject[] tmp = pModel.getModelObjects();
70
71 for (int j = 0; j < tmp.length; j++)
72 {
73 if (tmp[j].getObjectView() instanceof ClassView)
74 {
75 ((ClassView) tmp[j].getObjectView()).setLocation(
76 new Point(
77 tmp[j].getObjectView().getLocation().x - xOffset,
78 tmp[j].getObjectView().getLocation().y - yOffset));
79 tmp[j].getObjectView().resetSelectionPoint();
80 }
81 }
82 for (int i = 0; i < pModel.getLabels().size(); i++)
83 {
84 ObjectView label =
85 (ObjectView) pModel.getLabels().elementAt(i);
86 label.setLocation(
87 new Point(
88 label.getLocation().x - xOffset,
89 label.getLocation().y - yOffset));
90 label.resetSelectionPoint();
91 }
92 }
93
94 /***
95 * Write the base model to a PNG file
96 * @param pModel the context model
97 * @param file the file to write
98 * @return success
99 */
100 public static boolean writeImage(BaseModel pModel, File file)
101 {
102 boolean success = false;
103
104
105
106
107
108
109
110
111 Rectangle modelRect=pModel.getModelView().calculateRectangle();
112
113
114
115 BufferedImage bi = new BufferedImage(modelRect.width ,
116 modelRect.height ,
117 BufferedImage.TYPE_4BYTE_ABGR_PRE);
118 Graphics2D g = (Graphics2D) bi.createGraphics();
119
120
121
122
123 g.clearRect(0,0,modelRect.width,modelRect.height);
124
125
126 translateModel(pModel, modelRect.x , modelRect.y );
127
128
129 ModelMan.resetCurrentObjects();
130
131 pModel.getModelView().drawingArea.paint(g);
132
133
134 translateModel(pModel, (modelRect.x ) * -1, (modelRect.y) * -1);
135
136
137 try
138 {
139 success = javax.imageio.ImageIO.write(bi, "PNG", file);
140 }
141 catch (Exception e)
142 {
143 logger.error(e.getMessage());
144
145 e.printStackTrace();
146 }
147
148 return success;
149 }
150 }