• R/O
  • SSH
  • HTTPS

zenjiro: コミット


コミットメタ情報

リビジョン136 (tree)
日時2010-11-28 18:32:35
作者zenjiro

ログメッセージ

OSMap 0.2.0をリリース

変更サマリ

差分

--- osmap/tags/0.2.0/Main.java (nonexistent)
+++ osmap/tags/0.2.0/Main.java (revision 136)
@@ -0,0 +1,267 @@
1+import java.awt.BorderLayout;
2+import java.awt.Dimension;
3+import java.awt.Graphics;
4+import java.awt.Graphics2D;
5+import java.awt.Image;
6+import java.awt.RenderingHints;
7+import java.awt.event.MouseAdapter;
8+import java.awt.event.MouseEvent;
9+import java.awt.event.MouseMotionListener;
10+import java.awt.event.MouseWheelEvent;
11+import java.awt.event.MouseWheelListener;
12+import java.awt.event.WindowAdapter;
13+import java.awt.event.WindowEvent;
14+import java.io.File;
15+import java.io.FileInputStream;
16+import java.io.FileNotFoundException;
17+import java.io.FileOutputStream;
18+import java.io.FileWriter;
19+import java.io.IOException;
20+import java.net.MalformedURLException;
21+import java.net.URL;
22+import java.util.Formatter;
23+import java.util.HashMap;
24+import java.util.Map;
25+import java.util.Properties;
26+import java.util.Timer;
27+import java.util.TimerTask;
28+
29+import javax.imageio.IIOException;
30+import javax.imageio.ImageIO;
31+import javax.swing.JFrame;
32+import javax.swing.JLabel;
33+import javax.swing.JPanel;
34+import javax.swing.WindowConstants;
35+
36+/**
37+ * OpenStreetMapのビューア
38+ */
39+public class Main {
40+ /**
41+ * タイルの大きさ[px]
42+ */
43+ public static int TILE_SIZE = 256;
44+ /**
45+ * キャッシュファイルを置くディレクトリ
46+ */
47+ public static String CACHE_DIRECTORY = "cache";
48+ /**
49+ * 設定ファイルの名前
50+ */
51+ public static String CONFIGURATION_FILE = "osmap.ini";
52+ /**
53+ * 縮尺
54+ */
55+ static int zoom = 5;
56+ /**
57+ * 列
58+ */
59+ static int col = 27;
60+ /**
61+ * 行
62+ */
63+ static int row = 12;
64+ /**
65+ * x座標
66+ */
67+ static int offsetX = 20;
68+ /**
69+ * y座標
70+ */
71+ static int offsetY = 150;
72+ /**
73+ * マウスドラッグが開始されたx座標
74+ */
75+ static int lastX = 0;
76+ /**
77+ * マウスドラッグが開始されたy座標
78+ */
79+ static int lastY = 0;
80+
81+ /**
82+ * メインメソッド
83+ * @param args コマンドライン引数
84+ * @throws IOException 入出力例外
85+ */
86+ public static void main(final String[] args) throws IOException {
87+ final Properties properties = new Properties();
88+ try {
89+ properties.load(new FileInputStream(CONFIGURATION_FILE));
90+ zoom = properties.containsKey("zoom") ? Integer.parseInt(properties.getProperty("zoom")) : zoom;
91+ col = properties.containsKey("col") ? Integer.parseInt(properties.getProperty("col")) : col;
92+ row = properties.containsKey("row") ? Integer.parseInt(properties.getProperty("row")) : row;
93+ offsetX = properties.containsKey("offsetX") ? Integer.parseInt(properties.getProperty("offsetX")) : offsetX;
94+ offsetY = properties.containsKey("offsetY") ? Integer.parseInt(properties.getProperty("offsetY")) : offsetY;
95+ } catch (final FileNotFoundException exception) {
96+ // do nothing
97+ }
98+ final Map<String, Image> images = new HashMap<String, Image>();
99+ final JFrame frame = new JFrame("OSMap");
100+ final Timer timer = new Timer();
101+ frame.setLayout(new BorderLayout());
102+ final JLabel label = new JLabel(" ");
103+ final JPanel panel = new JPanel() {
104+ @Override
105+ protected void paintComponent(final Graphics graphics) {
106+ super.paintComponent(graphics);
107+ final Graphics2D g = (Graphics2D) graphics;
108+ g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
109+ g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
110+ for (int i = -1; TILE_SIZE * i + offsetY < this.getHeight(); i++) {
111+ if (row + i < 0 || row + i >= 1 << zoom) {
112+ continue;
113+ }
114+ for (int j = -1; TILE_SIZE * j + offsetX < this.getWidth(); j++) {
115+ if (col + j < 0 || col + j >= 1 << zoom) {
116+ continue;
117+ }
118+ g.drawImage(images.get(zoom + "_" + (col + j) + "_" + (row + i)), TILE_SIZE * j + offsetX,
119+ TILE_SIZE * i + offsetY, this);
120+ g.drawRect(TILE_SIZE * j + offsetX, TILE_SIZE * i + offsetY, TILE_SIZE, TILE_SIZE);
121+ g.drawString(zoom + "_" + (col + j) + "_" + (row + i), TILE_SIZE * j + offsetX, TILE_SIZE * i
122+ + offsetY + TILE_SIZE - 2);
123+ }
124+ }
125+ label.setText(new Formatter().format("zoom = %d, col = %d, row = %d, offsetX = %d, offsetY = %d\n",
126+ zoom, col, row, offsetX, offsetY).toString());
127+ }
128+ };
129+ panel.setPreferredSize(new Dimension(640, 480));
130+ frame.add(panel, BorderLayout.CENTER);
131+ frame.add(label, BorderLayout.SOUTH);
132+ panel.addMouseWheelListener(new MouseWheelListener() {
133+ @Override
134+ public void mouseWheelMoved(final MouseWheelEvent event) {
135+ final int clickedCol = (event.getX() - offsetX + TILE_SIZE) / TILE_SIZE - 1;
136+ final int clickedRow = (event.getY() - offsetY + TILE_SIZE) / TILE_SIZE - 1;
137+ final int diffX = (event.getX() - offsetX + TILE_SIZE) % TILE_SIZE;
138+ final int diffY = (event.getY() - offsetY + TILE_SIZE) % TILE_SIZE;
139+ if (event.getWheelRotation() < 0) {
140+ if (zoom < 18) {
141+ zoom++;
142+ col = col * 2 + clickedCol + (diffX < TILE_SIZE / 2 ? 0 : 1);
143+ row = row * 2 + clickedRow + (diffY < TILE_SIZE / 2 ? 0 : 1);
144+ offsetX = offsetX - diffX % TILE_SIZE + (diffX < TILE_SIZE / 2 ? 0 : TILE_SIZE);
145+ offsetY = offsetY - diffY % TILE_SIZE + (diffY < TILE_SIZE / 2 ? 0 : TILE_SIZE);
146+ }
147+ } else {
148+ if (zoom > 0) {
149+ zoom--;
150+ offsetX = offsetX + diffX / 2 - (col % 2 == 0 ? 0 : TILE_SIZE / 2) + clickedCol * TILE_SIZE / 2;
151+ offsetY = offsetY + diffY / 2 - (row % 2 == 0 ? 0 : TILE_SIZE / 2) + clickedRow * TILE_SIZE / 2;
152+ col /= 2;
153+ row /= 2;
154+ }
155+ }
156+ panel.repaint();
157+ }
158+ });
159+ panel.addMouseListener(new MouseAdapter() {
160+ @Override
161+ public void mouseClicked(final MouseEvent event) {
162+ if (event.getButton() == MouseEvent.BUTTON1) {
163+ } else if (event.getButton() == MouseEvent.BUTTON3) {
164+ }
165+ panel.repaint();
166+ }
167+
168+ @Override
169+ public void mousePressed(final MouseEvent event) {
170+ lastX = event.getX();
171+ lastY = event.getY();
172+ }
173+ });
174+ panel.addMouseMotionListener(new MouseMotionListener() {
175+ @Override
176+ public void mouseMoved(final MouseEvent event) {
177+ }
178+
179+ @Override
180+ public void mouseDragged(final MouseEvent event) {
181+ offsetX += event.getX() - lastX;
182+ offsetY += event.getY() - lastY;
183+ lastX = event.getX();
184+ lastY = event.getY();
185+ panel.repaint();
186+ }
187+ });
188+ frame.pack();
189+ frame.addWindowListener(new WindowAdapter() {
190+ @Override
191+ public void windowClosed(final WindowEvent event) {
192+ timer.cancel();
193+ try {
194+ properties.put("zoom", Integer.toString(zoom));
195+ properties.put("col", Integer.toString(col));
196+ properties.put("row", Integer.toString(row));
197+ properties.put("offsetX", Integer.toString(offsetX));
198+ properties.put("offsetY", Integer.toString(offsetY));
199+ properties.store(new FileWriter(CONFIGURATION_FILE), "OSMap configuration");
200+ } catch (final IOException exception) {
201+ exception.printStackTrace();
202+ }
203+ }
204+ });
205+ frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
206+ JFrame.setDefaultLookAndFeelDecorated(true);
207+ frame.setLocationByPlatform(true);
208+ frame.setVisible(true);
209+ timer.scheduleAtFixedRate(new TimerTask() {
210+ @Override
211+ public void run() {
212+ if (offsetX < 0) {
213+ offsetX += TILE_SIZE;
214+ col++;
215+ } else if (offsetX >= TILE_SIZE) {
216+ offsetX -= TILE_SIZE;
217+ col--;
218+ }
219+ if (offsetY < 0) {
220+ offsetY += TILE_SIZE;
221+ row++;
222+ } else if (offsetY >= TILE_SIZE) {
223+ offsetY -= TILE_SIZE;
224+ row--;
225+ }
226+ final int zoom = Main.zoom;
227+ final int col = Main.col;
228+ final int row = Main.row;
229+ final File cacheDirectory = new File(CACHE_DIRECTORY);
230+ if (!cacheDirectory.isDirectory()) {
231+ cacheDirectory.mkdir();
232+ }
233+ for (int i = -1; TILE_SIZE * i + offsetY < panel.getHeight(); i++) {
234+ if (row + i < 0 || row + i >= 1 << zoom) {
235+ continue;
236+ }
237+ for (int j = -1; TILE_SIZE * j + offsetX < panel.getWidth(); j++) {
238+ if (col + j < 0 || col + j >= 1 << zoom) {
239+ continue;
240+ }
241+ if (!images.containsKey(zoom + "_" + (col + j) + "_" + (row + i))) {
242+ final File file = new File(new Formatter().format("%s%s%d_%d_%d.png", CACHE_DIRECTORY,
243+ File.separator, zoom, col + j, row + i).toString());
244+ try {
245+ if (!file.isFile()) {
246+ WebUtilities.copy(
247+ new URL(new Formatter().format(
248+ "http://tile.openstreetmap.org/%d/%d/%d.png", zoom, col + j,
249+ row + i).toString()).openStream(), new FileOutputStream(file));
250+ }
251+ images.put(zoom + "_" + (col + j) + "_" + (row + i), ImageIO.read(file));
252+ panel.repaint();
253+ } catch (final IIOException exception) {
254+ file.delete();
255+ } catch (final MalformedURLException exception) {
256+ exception.printStackTrace();
257+ } catch (final IOException exception) {
258+ exception.printStackTrace();
259+ }
260+ }
261+ }
262+ }
263+ panel.repaint();
264+ }
265+ }, 200, 200);
266+ }
267+}
--- osmap/tags/0.2.0/WebUtilities.java (nonexistent)
+++ osmap/tags/0.2.0/WebUtilities.java (revision 136)
@@ -0,0 +1,23 @@
1+import java.io.IOException;
2+import java.io.InputStream;
3+import java.io.OutputStream;
4+
5+/**
6+ * ネットワーク関係のユーティリティクラス
7+ */
8+public class WebUtilities {
9+ /**
10+ * ストリームを使ってファイルをコピーします。
11+ * @param in 入力ストリーム
12+ * @param out 出力ストリーム
13+ * @throws IOException 入出力例外
14+ */
15+ public static void copy(final InputStream in, final OutputStream out) throws IOException {
16+ final byte buf[] = new byte[1024];
17+ int size;
18+ while ((size = in.read(buf)) != -1) {
19+ out.write(buf, 0, size);
20+ }
21+ out.close();
22+ }
23+}
旧リポジトリブラウザで表示