public Image colImage(int color) { Image img = Image.createImage(Font.getDefaultFont().getHeight(), Font.getDefaultFont().getHeight()); Graphics g = img.getGraphics(); g.setColor(color); g.fillRect(0, 0, img.getWidth(), img.getHeight()); return img; }
public ExtendedTextField() { setFullScreenMode(true); setCommandListener(this); exit = new Command("Exit", Command.EXIT, 1); open = new Command("Open", Command.ITEM, 1); newFile = new Command("New", Command.ITEM, 2); save = new Command("Save", Command.ITEM, 3); saveAs = new Command("Save As", Command.ITEM, 4); eval = new Command("Eval", Command.ITEM, 5); output = new Command("Output", Command.ITEM, 6); cls = new Command("Clear", Command.ITEM, 7); addCommand(exit); addCommand(newFile); addCommand(open); addCommand(save); addCommand(saveAs); addCommand(eval); addCommand(output); addCommand(cls); new Thread(this).start(); // TODO: change font size? inputFont = Font.getDefaultFont(); inputWidth = getWidth(); inputHeight = inputFont.getHeight(); linesOnScreen = getHeight() / inputHeight; addNewLine(0); }
/** Initialize the graphic elements needed for this item. */ protected void initContent(int firstLineWidth, int availWidth, int availHeight) { if (this.font == null) { this.font = Font.getDefaultFont(); this.fontColor = 0x000000; } contentHeight = font.getHeight() + this.linePadding; contentWidth = availWidth; }
/** * Returns the font of the style. If the font of the style is null return the default font. * * @return the font */ public Font getFont() { if (this.font == null) { if (this.style != null) { this.font = this.style.getFont(); } if (this.font == null) { this.font = Font.getDefaultFont(); } } return this.font; }
/** * Konstruktor obiektu klasy MainCanvas * * @throws IOException */ public MainCanvas() throws IOException { super(true); soundPlayer = new SoundPlayer(); // uruchomienie watku odpowiedzialnego za odtwarzanie dzwiekow new Thread(soundPlayer).start(); screenWidth = getWidth(); screenHeight = getHeight(); fontHeight = Font.getDefaultFont().getHeight(); mediaLibrarySelectedItemOnScreen = 0; mediaLibraryItemsNumberOnScreen = (screenHeight - 16) / (fontHeight + 5); buttonsLocation = 70; loadResources(); // utworzenie obiektow warstwy tla i tekstur bluetoothPlayer = new BluetoothPlayer(); Thread appThread = new Thread(this); // uruchomienie watku odpowiedzialnego za wyswietlanie grafiki appThread.start(); }
/** * Metoda wyswietlajaca biblioteke muzyczna na wyswietlaczu * * @param g Referencja do obiektu klasy Graphics, ktory pozwala na wyswietlenie tekstu * @param player Referencja do obiektu klasy odtwarzacza muzycznego * @param screenSelectedItemIndex Indeks wybranego elementu na wyswietlaczu * @param screenNumberOfItems Liczba elementow wyswietlonych na wyswietlaczu */ public void showMediaLibrary( Graphics g, BluetoothPlayer player, int screenSelectedItemIndex, int screenNumberOfItems) { int color = g.getColor(); // przechowanie uzywanego koloru int textPos = 0; String text; g.setColor(255, 255, 255); // biala czcionka for (int i = 0; i < screenNumberOfItems; i++) { text = bluetoothPlayer .getMediaLibrary() .getItem( bluetoothPlayer.getMediaLibrary().getMediaLibrarySelectedItem() - screenSelectedItemIndex + i, g); if (text != null) { // jesli zmienil sie wybrany tekst if ((screenSelectedItemIndex == i) && (text.equals(mediaLibraryLastText) == false)) { mediaLibraryLastText = text; mediaLibraryTextPos = 0; mediaLibraryWaitTimeText = SCROLL_TIME_WAIT; } textPos = mediaLibraryTextPos; // if whole text can be displayed at once if (Font.getDefaultFont().stringWidth(text) <= screenWidth - 32) { g.setColor(255, 255, 255); // biala czcionka g.drawString(text, 10, i * (fontHeight + 5) + 8, Graphics.TOP | Graphics.LEFT); } // if text doesn't fit into the screen else { String leftTextTmp = ""; int j = 0; // cut text from the left as long as it doesn't fit to the screen (only for selected item) if (screenSelectedItemIndex == i) while (Font.getDefaultFont().stringWidth(leftTextTmp) < textPos) { leftTextTmp = text.substring(0, ++j); } String textTmp = text.substring(j, text.length()); boolean textRightCut = false; // cut text from the right as long as it doesn't fit to the screen while (Font.getDefaultFont().stringWidth(textTmp) > screenWidth - 24) { textTmp = textTmp.substring(0, textTmp.length() - 1); textRightCut = true; } g.setColor(255, 255, 255); // biala czcionka g.drawString(textTmp, 10, i * (fontHeight + 5) + 8, Graphics.TOP | Graphics.LEFT); g.setColor(BACKGROUND_COLOR); // zamazanie tekstu po bokach g.fillRect(screenWidth - 10, i * (fontHeight + 5) + 8, 5, fontHeight); if (screenSelectedItemIndex == i) { if ((textRightCut == false) && (mediaLibraryWaitTimeText == 0)) // jesli tekst przewinal sie do konca mediaLibraryWaitTimeText = SCROLL_TIME_WAIT; if (mediaLibraryWaitTimeText == 0) mediaLibraryTextPos += 2; // przewiniecie tekstu // if text is going to be displayed statically (without scrolling it) else { mediaLibraryWaitTimeText--; // if wait time has ended, start scrolling from the beginning if ((mediaLibraryWaitTimeText == 0) && (mediaLibraryTextPos != 0)) { mediaLibraryWaitTimeText = SCROLL_TIME_WAIT; mediaLibraryTextPos = 0; } } } } } } g.setColor(255, 255, 255); // obwodka dla podswietlonego elementu if (screenNumberOfItems > 0) g.drawRect( 7, screenSelectedItemIndex * (fontHeight + 5) + 6, screenWidth - 16, fontHeight + 1); g.setColor(color); }
/** * Metoda sluzaca do wypisania tekstu na ekranie w podanej linii * * @param g Referencja do obiektu klasy Graphics, ktory pozwala na wyswietlenie tekstu * @param text Tekst, ktory ma zostac wypisany * @param line Numer linii, w ktorej ma zostac wypisany tekst * @return <code>true</code> jesli wypisanie tekstu powiodlo sie, <code>false</code> w przeciwnym * razie */ public boolean drawText(Graphics g, String text, int line) { int color = g.getColor(); // przechowanie uzywanego koloru int textPos = 0; g.setColor(BACKGROUND_COLOR); // zamazanie starego tekstu g.fillRect(16, 16 + line * (fontHeight + 3), screenWidth - 32, fontHeight); if (line == 0) { // if displayed text had changed if (text.equals(lastText0) == false) { lastText0 = text; textPos0 = 0; waitTimeText0 = SCROLL_TIME_WAIT; } textPos = textPos0; } else if (line == 1) { // if displayed text had changed if (text.equals(lastText1) == false) { lastText1 = text; textPos1 = 0; waitTimeText1 = SCROLL_TIME_WAIT; } textPos = textPos1; } // if whole text can be displayed at once if (Font.getDefaultFont().stringWidth(text) <= screenWidth - 32) { g.setColor(255, 255, 255); // biala czcionka g.drawString(text, 16, 16 + line * (fontHeight + 3), Graphics.TOP | Graphics.LEFT); } // if text doesn't fit into the screen else { String leftTextTmp = ""; int i = 0; // cut text from the left as long as it doesn't fit to the screen while (Font.getDefaultFont().stringWidth(leftTextTmp) < textPos) { leftTextTmp = text.substring(0, ++i); } String textTmp = text.substring(i, text.length()); boolean textRightCut = false; // cut text from the right as long as it doesn't fit to the screen while (Font.getDefaultFont().stringWidth(textTmp) > screenWidth - 32) { textTmp = textTmp.substring(0, textTmp.length() - 1); textRightCut = true; } g.setColor(255, 255, 255); // biala czcionka g.drawString(textTmp, 16, 16 + line * (fontHeight + 3), Graphics.TOP | Graphics.LEFT); g.setColor(BACKGROUND_COLOR); // zamazanie tekstu po bokach g.fillRect(screenWidth - 16, 16 + line * (fontHeight + 3), 11, fontHeight); // g.fillRect(5, 16 + line*(fontHeight + 3), 11, fontHeight); if (line == 0) { if ((textRightCut == false) && (waitTimeText0 == 0)) // jesli tekst przewinal sie do konca waitTimeText0 = SCROLL_TIME_WAIT; if (waitTimeText0 == 0) textPos0 += 2; // przewiniecie tekstu // if text is going to be displayed statically (without scrolling it) else { waitTimeText0--; // if wait time has ended, start scrolling from the beginning if ((waitTimeText0 == 0) && (textPos0 != 0)) { waitTimeText0 = SCROLL_TIME_WAIT; textPos0 = 0; } } } else if (line == 1) { if ((textRightCut == false) && (waitTimeText1 == 0)) // jesli tekst przewinal sie do konca waitTimeText1 = SCROLL_TIME_WAIT; if (waitTimeText1 == 0) textPos1 += 2; // przewiniecie tekstu // if text is going to be displayed statically (without scrolling it) else { waitTimeText1--; // if wait time has ended, start scrolling from the beginning if ((waitTimeText1 == 0) && (textPos1 != 0)) { waitTimeText1 = SCROLL_TIME_WAIT; textPos1 = 0; } } } } g.setColor(color); return true; }
/** * The AboutScreen is a Canvas which shows the MTE Logo and some information about the MTE * * <p>The AboutScreen has 3 sections, the Mobile Trail Explorer (MTE) Logo/Image at the top, * followed by an "About" section, and finally a "Useage" section.<br> * * <p>Both the About and Usage sections consist of an underlined 'Title' followed by multiple lines * of text. * * @author Barry Redmond */ public final class AboutScreen extends Canvas { /** The size margin to draw when margins are needed. */ private static final int MARGIN = 5; /** The default font to ~use for drawing the messages. */ private static final Font DEFAULT_SIMPLE_FONT = Font.getDefaultFont(); private static final String[] aboutText = {"BTIS,", "", "Version: Alpha release", ""}; private static final String[] trailScreenHelpText = { "use following keys ", "2 for UP", "4 for LEFT", "5 for CENTRE", "6 for RIGHT", "8 for DOWN", "", "1 for Zoom out", "3 for Zoom in" }; private static final String[] elevationScreenHelpText = {"", ""}; private static final String[] skyViewScreenHelpText = {"", ""}; /* private static final String[] elevationScreenHelpText = { "Scroll the Elevation Diagram LEFT or RIGHT using the 4 and 6 keys respectivly.", "", "The Elevation scale can be \"Zoomed\" using the 1 and 3 keys for ZOOM-OUT and ZOOM-IN respectivly.", "", "The Elevation scale can be re-centered by pressing the 2 key.", "", "The Time scale can be \"Zoomed\" using the 7 and 9 keys for ZOOM-OUT and ZOOM-IN respectivly.", "", "N.B. Altitude accuracy on GPS is similar to location (i.e. ~10metres. However since most trails do not" + " rise or fall very fast the altitude track displayed may seem very jumpy, and inaccurate." }; private static final String[] skyViewScreenHelpText = { "The Sky View shows the visible GPS sattelite locations and signal strength", "", "Because it is a sky view the E and W appear back the front unless pointed at the sky", "", "The number shown next to Trk # is the number of sattelites currently being used for the GPS fix", "", "Increase or decrease the Signal SNR range for the display by pressing the 2 and 8 keys"}; */ private static final String[] helpText = { "For more information visit", "www.btis.in", "", "", "", "" }; /** * The titles of the different sections {@link AboutScreen#TITLES}[n] is the title corrosponding * to {@link AboutScreen#MESSAGES}[n]<br> * Both {@link AboutScreen#TITLES} and {@link AboutScreen#MESSAGES} must NOT be null, and must * have the same number of elements. */ private static final String[] TITLES = {"Map browsing", "Help"}; /** * The text for the different sections {@link AboutScreen#MESSAGES}[n] is the text corrosponding * to {@link AboutScreen#TITLES}[n]<br> * Both {@link AboutScreen#TITLES} and {@link AboutScreen#MESSAGES} must NOT be null, and must * have the same number of elements. */ private static final String[][] MESSAGES = {trailScreenHelpText, helpText}; static { if (TITLES == null || MESSAGES == null || TITLES.length != MESSAGES.length) { try { throw new java.lang.IllegalStateException( "There was a problem with the coding of the " + AboutScreen.class.getName() + " class. neither titles, nor messages may be null, and they both must have the same number of elements"); } catch (IllegalStateException e) { e.printStackTrace(); throw e; } } } private Controller controller; private final Image logo; private int yPos; // this is calculated the first time the paint(Graphics) method is // called. private int totalHeight; // Used to decide if we should set the 'totalHeight' property. private boolean firstTime = true; private String[][] formattedMessages; /** Creates a new instance of AboutScreen */ public AboutScreen() { this.controller = Controller.getController(); this.setFullScreenMode(true); this.logo = ImageUtil.loadImage("/images/logo.png"); this.yPos = 0; this.formattedMessages = new String[MESSAGES.length][]; for (int i = 0; i < MESSAGES.length; i++) { formattedMessages[i] = formatMessage(MESSAGES[i], this.getWidth()); } } /** * Takes an array of messages and a 'Screen-width' and returns the same messages, but any string * in that that is wider than 'width' will be split up into 2 or more Strings that will fit on the * screen 'Spliting' up a String is done on the basis of Words, so if a single WORD is longer than * 'width' it will be on a Line on it's own, but that line WILL be WIDER than 'width' * * @param message * @param width the maximum width a string may be before being split up. * @return */ private String[] formatMessage(String[] message, int width) { Vector result = new Vector(message.length); for (int i = 0; i < message.length; i++) { if (DEFAULT_SIMPLE_FONT.stringWidth(message[i]) <= width) { result.addElement(message[i]); } else { String[] splitUp = StringUtil.chopStrings(message[i], " ", AboutScreen.DEFAULT_SIMPLE_FONT, width); for (int j = 0; j < splitUp.length; j++) { result.addElement(splitUp[j]); } } } String[] finalResult = new String[result.size()]; for (int i = 0; i < finalResult.length; i++) { finalResult[i] = (String) result.elementAt(i); } return finalResult; } protected void paint(Graphics g) { int width = this.getWidth(); // Fill in the background White g.setColor(0xFFFFFF); // White g.fillRect(0, 0, width, this.getHeight()); int y = yPos; y += MARGIN; if (this.logo != null) { g.drawImage(this.logo, width / 2, y, Graphics.TOP | Graphics.HCENTER); y += logo.getHeight() + MARGIN; } g.setColor(0x0); // Black // Write the title "About" final Font titleFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD | Font.STYLE_UNDERLINED, Font.SIZE_LARGE); /* * TODO: currently paints ALL the text, even if there is (a lot) more * than can fit on the screen. (must do this first time to assess * 'height', but shouldn't need to do it for any other repaint */ for (int i = 0; i < TITLES.length; i++) { final int basicFontHeight = DEFAULT_SIMPLE_FONT.getHeight(); final int titleFontHeight = titleFont.getHeight(); // Write out the 'message' heading g.setFont(titleFont); final String title = TITLES[i]; int stringWidth = g.getFont().stringWidth(title); g.drawString(title, (width - stringWidth) / 2, y, 0); y += titleFontHeight; // Write out the 'message' text. g.setFont(DEFAULT_SIMPLE_FONT); for (int j = 0; j < this.formattedMessages[i].length; j++) { final String message = this.formattedMessages[i][j]; stringWidth = g.getFont().stringWidth(message); g.drawString(message, (width - stringWidth) / 2, y, 0); y += basicFontHeight; } y += basicFontHeight; } // Set the total-Height the first time if (firstTime) { totalHeight = y + MARGIN; firstTime = false; } } /** Handle all keyPressed-events */ public void keyPressed(int keycode) { if (this.getGameAction(keycode) == UP) { upPressed(); } else if (this.getGameAction(keycode) == DOWN) { downPressed(); } else { // Exit on ANY other key press. controller.MainMenu(); } } /** Handle all keyRepeaded-events */ public void keyRepeated(int keycode) { if (this.getGameAction(keycode) == UP) { upPressed(); } else if (this.getGameAction(keycode) == DOWN) { downPressed(); } } /** Pan the screen up if an Up-Key is pressed */ private void upPressed() { yPos += 20; if (yPos > 0) { yPos = 0; } this.repaint(); } /** Pan the screen down if a Down-Key is pressed */ private void downPressed() { yPos -= 20; if (yPos < this.getHeight() - this.totalHeight) { yPos = this.getHeight() - this.totalHeight; } this.repaint(); } }
protected void paint(Graphics g) { int w = getWidth(); int h = getHeight(); if (_phase == 0) { long t = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) g.drawLine(0, i % 150, 149, 149 - i % 150); _times[_phase++] = System.currentTimeMillis() - t; } else if (_phase == 1) { long t = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) g.drawLine(0, i % h, w - 1, h - 1 - i % h); _times[_phase++] = System.currentTimeMillis() - t; } else if (_phase == 2) { long t = System.currentTimeMillis(); for (int i = 0; i < 1000; i++) g.fillRect(0, 0, 150, 150); _times[_phase++] = System.currentTimeMillis() - t; } else if (_phase == 3) { long t = System.currentTimeMillis(); for (int i = 0; i < 1000; i++) g.fillRect(0, 0, w, h); _times[_phase++] = System.currentTimeMillis() - t; } else if (_phase == 4) { Image img = Image.createImage(150, 150); img.getGraphics().setColor(0x456789); img.getGraphics().setFont(Font.getDefaultFont()); img.getGraphics().drawString("ahoj", 0, 0, Graphics.LEFT | Graphics.TOP); long t = System.currentTimeMillis(); for (int i = 0; i < 1000; i++) g.drawImage(img, 0, 0, Graphics.LEFT | Graphics.TOP); _times[_phase++] = System.currentTimeMillis() - t; } else if (_phase == 5) { long t = System.currentTimeMillis(); GeneralFont f = GeneralFont.SystemFontsBold[0]; for (int i = 0; i < 1000; i++) f.drawString(g, "0123", 0, 0, Graphics.TOP | Graphics.LEFT); _times[_phase++] = System.currentTimeMillis() - t; } else if (_phase == 6) { long t = System.currentTimeMillis(); GeneralFont f = GeneralFont.NumberFonts[0]; for (int i = 0; i < 1000; i++) f.drawString(g, "0123", 0, 0, Graphics.TOP | Graphics.LEFT); _times[_phase++] = System.currentTimeMillis() - t; } else if (_phase == 7) { long t = System.currentTimeMillis(); double a, b, c; for (int i = 0; i < 10000; i++) { a = Math.cos(Math.sin(Math.floor(1.1 + i))); b = Math.sqrt(a); c = (a * b + a * b + a * b) % (1 + b * b); _times[_phase] = (long) c; } _times[_phase++] = System.currentTimeMillis() - t; } else if (_phase == 8) { long t = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { // Earth.atan2(i*0.001, (i+1)*0.001); Earth.gg2lat(Earth.lat2gg(50)); } _times[_phase++] = System.currentTimeMillis() - t; } Font f = Font.getDefaultFont(); int y = 0; g.setColor(0); g.fillRect(0, 0, w, h); g.setColor(0xffffff); g.setFont(f); for (int i = 0; i < _phase; i++) { g.drawString(_tests[i] + ": " + _times[i] + "ms", 0, y, Graphics.LEFT | Graphics.TOP); y += f.getHeight(); } if (_phase < _tests.length) { g.drawString("<testing...>", 0, y, Graphics.LEFT | Graphics.TOP); new Thread() { public void run() { try { Thread.sleep(100); } catch (InterruptedException e) { } repaint(); } }.start(); } else g.drawString("<done>", 0, y, Graphics.LEFT | Graphics.TOP); }