001: /*
002: Copyright (C) 2004 David Bucciarelli (davibu@interfree.it)
003:
004: This program is free software; you can redistribute it and/or
005: modify it under the terms of the GNU General Public License
006: as published by the Free Software Foundation; either version 2
007: of the License, or (at your option) any later version.
008:
009: This program is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: GNU General Public License for more details.
013:
014: You should have received a copy of the GNU General Public License
015: along with this program; if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: */
018:
019: package org.homedns.dade.jcgrid.gui;
020:
021: import java.io.*;
022: import java.awt.*;
023: import java.awt.image.*;
024: import javax.swing.*;
025: import javax.imageio.*;
026:
027: import org.apache.log4j.*;
028:
029: public class guiRenderingPannel extends javax.swing.JPanel {
030: private final static String className = guiRenderingPannel.class
031: .getName();
032: private static Logger log = Logger.getLogger(className);
033: private static Logger logDetail = Logger.getLogger("DETAIL."
034: + className);
035:
036: private static final long serialVersionUID = 1L;
037:
038: private BufferedImage bImage;
039: private Graphics2D graph;
040:
041: private boolean enableCursor;
042: private int cursorX;
043: private int cursorY;
044:
045: public guiRenderingPannel() {
046: initComponents();
047:
048: enableCursor = false;
049: cursorX = 0;
050: cursorY = 0;
051: }
052:
053: public synchronized void setCursorEnable(boolean v) {
054: enableCursor = v;
055: }
056:
057: public double getCursorX() {
058: return cursorX / (double) this .getWidth();
059: }
060:
061: public double getCursorY() {
062: return 1.0 - cursorY / (double) this .getHeight();
063: }
064:
065: public synchronized void initImage() {
066: int w = this .getSize().width;
067: int h = this .getSize().height;
068:
069: if ((w <= 0) || (h <= 0)) {
070: bImage = null;
071: graph = null;
072: return;
073: }
074:
075: bImage = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
076: graph = bImage.createGraphics();
077:
078: this .clearImage();
079: }
080:
081: public Graphics2D getGraphics2D() {
082: return graph;
083: }
084:
085: public synchronized void saveImage(String imgName) {
086: int idx = imgName.lastIndexOf('.');
087: if (idx == -1)
088: JOptionPane.showMessageDialog(this ,
089: "Missing image file extension", "IO error",
090: JOptionPane.ERROR_MESSAGE);
091: else {
092: String outputFormat = imgName.substring(idx + 1);
093:
094: String[] formats = ImageIO.getWriterFormatNames();
095: boolean found = false;
096: for (int i = 0; i < formats.length; i++) {
097: if (formats[i].equals(outputFormat)) {
098: found = true;
099: break;
100: }
101: }
102:
103: if (!found)
104: JOptionPane.showMessageDialog(this , "The image format "
105: + outputFormat + " is not supported",
106: "IO error", JOptionPane.ERROR_MESSAGE);
107: else {
108: File f = new File(imgName);
109: try {
110: ImageIO.write(bImage, outputFormat, f);
111: } catch (Exception ex) {
112: log.warn("Error saving the image", ex);
113:
114: JOptionPane.showMessageDialog(this ,
115: "Error saving the image", "IO error",
116: JOptionPane.ERROR_MESSAGE);
117: }
118: }
119: }
120: }
121:
122: public synchronized void resizeImage(int width, int height) {
123: if ((width == bImage.getWidth())
124: && (height == bImage.getHeight()))
125: return;
126:
127: bImage = new BufferedImage(width, height,
128: BufferedImage.TYPE_3BYTE_BGR);
129: graph = bImage.createGraphics();
130:
131: this .clearImage();
132: }
133:
134: public synchronized void clearImage() {
135: int w = this .getSize().width;
136: int h = this .getSize().height;
137:
138: if ((w <= 0) || (h <= 0)) {
139: bImage = null;
140: graph = null;
141: return;
142: }
143:
144: for (int y = 0; y < h; y += 10) {
145: for (int x = 0; x < w; x += 10) {
146: if (((x / 10 + y / 10) % 2) == 0)
147: graph.setColor(Color.BLACK);
148: else
149: graph.setColor(Color.WHITE);
150: graph.fillRect(x, y, 10, 10);
151: }
152: }
153:
154: this .repaint();
155: }
156:
157: public synchronized void paintComponent(Graphics g) {
158: super .paintComponent(g);
159:
160: // Retry to inizilize image
161:
162: if (bImage == null)
163: this .initImage();
164:
165: // Repaint image
166:
167: if (bImage != null)
168: g.drawImage(bImage, 0, 0, Color.BLACK, null);
169:
170: if (enableCursor) {
171: g.setColor(Color.YELLOW);
172: g.setXORMode(Color.BLUE);
173: g.drawLine(cursorX - 5, cursorY, cursorX + 5, cursorY);
174: g.drawLine(cursorX, cursorY - 5, cursorX, cursorY + 5);
175: }
176: }
177:
178: /** This method is called from within the constructor to
179: * initialize the form.
180: * WARNING: Do NOT modify this code. The content of this method is
181: * always regenerated by the Form Editor.
182: */
183: private void initComponents() {//GEN-BEGIN:initComponents
184:
185: setLayout(new java.awt.BorderLayout());
186:
187: addMouseListener(new java.awt.event.MouseAdapter() {
188: public void mouseClicked(java.awt.event.MouseEvent evt) {
189: formMouseClicked(evt);
190: }
191: });
192:
193: }//GEN-END:initComponents
194:
195: private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
196: cursorX = evt.getX();
197: cursorY = evt.getY();
198:
199: this .repaint();
200: }//GEN-LAST:event_formMouseClicked
201:
202: // Variables declaration - do not modify//GEN-BEGIN:variables
203: // End of variables declaration//GEN-END:variables
204:
205: }
|