11 package com.google.scrollview.ui;
20 import org.piccolo2d.PCamera;
21 import org.piccolo2d.PCanvas;
22 import org.piccolo2d.PLayer;
23 import org.piccolo2d.extras.swing.PScrollPane;
24 import org.piccolo2d.nodes.PImage;
25 import org.piccolo2d.nodes.PPath;
26 import org.piccolo2d.nodes.PText;
27 import org.piccolo2d.util.PPaintContext;
29 import java.awt.BasicStroke;
30 import java.awt.BorderLayout;
31 import java.awt.Color;
33 import java.awt.GraphicsEnvironment;
34 import java.awt.Rectangle;
35 import java.awt.TextArea;
36 import java.awt.geom.IllegalPathStateException;
37 import java.util.regex.Matcher;
38 import java.util.regex.Pattern;
40 import javax.swing.JFrame;
41 import javax.swing.JOptionPane;
42 import javax.swing.SwingUtilities;
43 import javax.swing.WindowConstants;
56 private static final int MAX_WINDOW_X = 1000;
57 private static final int MAX_WINDOW_Y = 800;
60 private static final int DEF_MESSAGEBOX_HEIGHT = 200;
69 Color currentPenColor;
75 Color currentBrushColor;
91 BasicStroke stroke =
new BasicStroke(0.5f);
111 private TextArea ta = null;
114 private int winSizeX;
115 private int winSizeY;
118 public void brush(
int red,
int green,
int blue) {
119 brush(red, green, blue, 255);
123 public void brush(
int red,
int green,
int blue,
int alpha) {
126 currentBrushColor = null;
128 currentBrushColor =
new Color(red, green, blue, alpha);
137 final java.util.concurrent.CountDownLatch latch =
new java.util.concurrent.CountDownLatch(1);
138 SwingUtilities.invokeLater(
new Runnable() {
140 layer.removeAllChildren();
147 }
catch (InterruptedException e) {
178 for (
int p = 1; p < numCoords; ++p) {
183 pn.setStrokePaint(currentPenColor);
185 pn.setStroke(stroke);
203 int sizeY,
int canvasSizeX,
int canvasSizeY) {
207 if (sizeX == 0) sizeX = canvasSizeX;
208 if (sizeY == 0) sizeY = canvasSizeY;
209 if (canvasSizeX == 0) canvasSizeX = sizeX;
210 if (canvasSizeY == 0) canvasSizeY = sizeY;
216 this.currentPenColor = Color.BLACK;
217 this.currentBrushColor = Color.BLACK;
218 this.currentFont =
new Font(
"Times New Roman", Font.PLAIN, 12);
222 int shrinkfactor = 1;
224 if (sizeX > MAX_WINDOW_X) {
225 shrinkfactor = (sizeX + MAX_WINDOW_X - 1) / MAX_WINDOW_X;
227 if (sizeY / shrinkfactor > MAX_WINDOW_Y) {
228 shrinkfactor = (sizeY + MAX_WINDOW_Y - 1) / MAX_WINDOW_Y;
230 winSizeX = sizeX / shrinkfactor;
231 winSizeY = sizeY / shrinkfactor;
232 double initialScalingfactor = 1.0 / shrinkfactor;
233 if (winSizeX > canvasSizeX || winSizeY > canvasSizeY) {
234 initialScalingfactor = Math.min(1.0 * winSizeX / canvasSizeX,
235 1.0 * winSizeY / canvasSizeY);
239 if (canvas == null) {
240 canvas =
new PCanvas();
241 getContentPane().add(canvas, BorderLayout.CENTER);
244 layer = canvas.getLayer();
245 canvas.setBackground(Color.BLACK);
248 canvas.setDefaultRenderQuality(PPaintContext.LOW_QUALITY_RENDERING);
250 setLayout(
new BorderLayout());
252 setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
255 canvas.requestFocus();
261 SwingUtilities.invokeLater(
new Runnable() {
267 setSize(winSizeX, winSizeY);
268 setLocation(posX, posY);
272 PScrollPane scrollPane =
new PScrollPane(canvas);
273 getContentPane().add(scrollPane);
274 scrollPane.setWheelScrollingEnabled(
false);
275 PCamera lc = canvas.getCamera();
276 lc.scaleViewAboutPoint(initialScalingfactor, 0, 0);
279 addWindowListener(svEventHandler);
280 canvas.removeInputEventListener(canvas.getPanEventHandler());
281 canvas.removeInputEventListener(canvas.getZoomEventHandler());
282 canvas.addInputEventListener(svEventHandler);
283 canvas.addKeyListener(svEventHandler);
298 ta.setEditable(
false);
299 getContentPane().add(ta, BorderLayout.SOUTH);
302 winSizeY += DEF_MESSAGEBOX_HEIGHT;
303 setSize(winSizeX, winSizeY);
314 stroke =
new BasicStroke(width);
323 PPath pn = PPath.createEllipse(x, y, width, height);
324 pn.setStrokePaint(currentPenColor);
325 pn.setStroke(stroke);
326 pn.setPaint(currentBrushColor);
344 public void drawLine(
int x1,
int y1,
int x2,
int y2) {
345 PPath pn = PPath.createLine(x1, y1, x2, y2);
346 pn.setStrokePaint(currentPenColor);
348 pn.setStroke(stroke);
372 PPath pn = PPath.createRectangle(x1, y1, x2 - x1, y2 - y1);
373 pn.setStrokePaint(currentPenColor);
374 pn.setStroke(stroke);
375 pn.setPaint(currentBrushColor);
387 int unreadableCharAt = -1;
388 char[] chars = text.toCharArray();
389 PText pt =
new PText(text);
390 pt.setTextPaint(currentPenColor);
391 pt.setFont(currentFont);
394 for (
int i = 0; i < chars.length; i++) {
395 if (!currentFont.canDisplay(chars[i])) {
397 unreadableCharAt = i;
403 if (unreadableCharAt != -1) {
405 GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
406 for (
int j = 0; j < allfonts.length; j++) {
407 if (allfonts[j].canDisplay(chars[unreadableCharAt])) {
409 new Font(allfonts[j].getFontName(), currentFont.getStyle(),
410 currentFont.getSize());
411 pt.setFont(tempFont);
423 public void pen(
int red,
int green,
int blue) {
424 pen(red, green, blue, 255);
428 public void pen(
int red,
int green,
int blue,
int alpha) {
429 currentPenColor =
new Color(red, green, blue, alpha);
436 boolean italic,
boolean underlined) {
439 if (font.equals(
"Times")) {
440 font =
"Times New Roman";
443 int style = Font.PLAIN;
448 style += Font.ITALIC;
450 currentFont =
new Font(font, style, pixelSize);
458 if (x2 > x1 && y2 > y1) {
459 winSizeX = getWidth();
460 winSizeY = getHeight();
462 int height = y2 - y1;
465 int wmargin = width / 2;
466 int hmargin = height / 2;
467 double scalefactor = Math.min(winSizeX / (2.0 * wmargin + width),
468 winSizeY / (2.0 * hmargin + height));
469 PCamera lc = canvas.getCamera();
470 lc.scaleView(scalefactor / lc.getViewScale());
471 lc.animateViewToPanToBounds(
new Rectangle(x1 - hmargin, y1 - hmargin,
473 2 * hmargin + height), 0);
493 }
catch (NullPointerException e) {
495 }
catch (IllegalPathStateException e) {
503 svMenuBar.
add(parent, name,
id, checked);
513 if (svMenuBar == null) {
517 svMenuBar.
add(parent, name,
id);
523 ta.append(message +
"\n");
525 System.out.println(message +
"\n");
539 private static String convertIntegerStringToUnicodeString(String input) {
540 StringBuffer sb =
new StringBuffer(input);
541 Pattern numbers = Pattern.compile(
"0x[0-9a-fA-F]{4}");
542 Matcher matcher = numbers.matcher(sb);
544 while (matcher.find()) {
548 char a = (char) (Integer.decode(matcher.group()).intValue());
551 sb.replace(matcher.start(), matcher.end(), String.valueOf(a));
556 return sb.toString();
571 svEventHandler.
timer.stop();
573 (String) JOptionPane.showInputDialog(
this, msg,
"",
574 JOptionPane.QUESTION_MESSAGE, null, null, def);
577 tmp = convertIntegerStringToUnicodeString(tmp);
581 svEventHandler.
timer.restart();
604 JOptionPane.showOptionDialog(
this, msg,
"", JOptionPane.YES_NO_OPTION,
605 JOptionPane.QUESTION_MESSAGE, null, null, null);
610 }
else if (res == 1) {
618 if (svPuMenu == null) {
621 svPuMenu.
add(parent, name, -1);
626 String value, String desc) {
627 if (svPuMenu == null) {
630 svPuMenu.
add(parent, name, cmdEvent, value, desc);
void addMessage(String message)
void addMenuBarItem(String parent, String name, int id)
static int polylineScanned
void textAttributes(String font, int pixelSize, boolean bold, boolean italic, boolean underlined)
void zoomRectangle(int x1, int y1, int x2, int y2)
void addMenuBarItem(String parent, String name, int id, boolean checked)
void brush(int red, int green, int blue)
void drawRectangle(int x1, int y1, int x2, int y2)
void drawImage(PImage img, int xPos, int yPos)
void addMenuBarItem(String parent, String name)
void drawLine(int x1, int y1, int x2, int y2)
void showInputDialog(String msg)
void add(String parent, String name, int id)
void drawText(int x, int y, String text)
void pen(int red, int green, int blue, int alpha)
void showYesNoDialog(String msg)
void setStrokeWidth(float width)
static void addMessage(SVEvent e)
static float polylineYCoords[]
void createPolyline(int length)
void pen(int red, int green, int blue)
static final double SCALING_FACTOR
void addPopupMenuItem(String parent, String name)
SVWindow(String name, int hash, int posX, int posY, int sizeX, int sizeY, int canvasSizeX, int canvasSizeY)
void showInputDialog(String msg, String def, int id, SVEventType evtype)
void add(String parent, String name, int id)
void addPopupMenuItem(String parent, String name, int cmdEvent, String value, String desc)
void drawEllipse(int x, int y, int width, int height)
static float polylineXCoords[]
void brush(int red, int green, int blue, int alpha)