[sword-svn] r146 - in trunk: . app/src/org/crosswire/flashcards

Apache apache at www.crosswire.org
Tue Jan 1 16:54:40 MST 2008


Author: 
Date: 2008-01-01 16:54:38 -0700 (Tue, 01 Jan 2008)
New Revision: 146

Modified:
   trunk/.project
   trunk/app/src/org/crosswire/flashcards/ComplexLesson.java
   trunk/app/src/org/crosswire/flashcards/QuizPane.java
Log:
Added font resizing proportional to window size


Modified: trunk/.project
===================================================================
--- trunk/.project	2007-12-30 20:31:14 UTC (rev 145)
+++ trunk/.project	2008-01-01 23:54:38 UTC (rev 146)
@@ -1,17 +1,19 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<projectDescription>
-	<name>flashcards</name>
-	<comment></comment>
-	<projects>
-	</projects>
-	<buildSpec>
-		<buildCommand>
-			<name>org.eclipse.jdt.core.javabuilder</name>
-			<arguments>
-			</arguments>
-		</buildCommand>
-	</buildSpec>
-	<natures>
-		<nature>org.eclipse.jdt.core.javanature</nature>
-	</natures>
-</projectDescription>
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>flashcards</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+		<nature>org.eclipse.jem.beaninfo.BeanInfoNature</nature>
+	</natures>
+</projectDescription>

Modified: trunk/app/src/org/crosswire/flashcards/ComplexLesson.java
===================================================================
--- trunk/app/src/org/crosswire/flashcards/ComplexLesson.java	2007-12-30 20:31:14 UTC (rev 145)
+++ trunk/app/src/org/crosswire/flashcards/ComplexLesson.java	2008-01-01 23:54:38 UTC (rev 146)
@@ -88,7 +88,7 @@
                         if (fontFile.exists()) {
                              String url = fontFile.toURL().toString();
                              setFont(url);
-System.out.println("found font in ./");
+System.out.println("found font in ./; URL: " + url);
                              break;
                         }
                     }
@@ -101,7 +101,7 @@
                         if (fontFile.exists()) {
                              String url = fontFile.toURL().toString();
                              setFont(url);
-System.out.println("found font in ~/.flashcards");
+System.out.println("found font in ~/.flashcards; URL: " + url);
                              break;
                         }
                     }

Modified: trunk/app/src/org/crosswire/flashcards/QuizPane.java
===================================================================
--- trunk/app/src/org/crosswire/flashcards/QuizPane.java	2007-12-30 20:31:14 UTC (rev 145)
+++ trunk/app/src/org/crosswire/flashcards/QuizPane.java	2008-01-01 23:54:38 UTC (rev 146)
@@ -25,12 +25,17 @@
 import java.awt.ComponentOrientation;
 import java.awt.Dimension;
 import java.awt.Font;
+import java.awt.Graphics2D;
 import java.awt.GridLayout;
+import java.awt.Rectangle;
 import java.awt.SystemColor;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.ItemEvent;
 import java.awt.event.ItemListener;
+import java.awt.font.FontRenderContext;
+import java.awt.font.TextLayout;
+import java.awt.geom.Rectangle2D;
 import java.io.IOException;
 import java.io.Serializable;
 import java.io.InputStream;
@@ -73,6 +78,8 @@
      private static final int NUM_ANSWERS = 10;
      private static Hashtable fontCache = new Hashtable();
 
+     float optimalFontSize = 30;
+     List picks = new ArrayList();
      /**
       * Serialization ID
       */
@@ -215,6 +222,11 @@
           jPanel2.add(jPanel6, java.awt.BorderLayout.SOUTH);
           jPanel2.add(wordText, java.awt.BorderLayout.CENTER);
           jPanel6.add(choicesPanel, java.awt.BorderLayout.CENTER);
+          jPanel2.addComponentListener(new java.awt.event.ComponentAdapter() {
+              public void componentResized(java.awt.event.ComponentEvent e) {
+                  windowResized();
+              }
+          });
      }
 
 
@@ -253,27 +265,27 @@
 
 
     public Font loadFont(String url) {
-        Font retVal = null;
-        try {
-            // see if our font is already loaded...
-            retVal = (Font)fontCache.get(url);
-            if (retVal == null) {
-                statusBar.setText("Loading font...");
-                statusBar.paintImmediately(statusBar.getVisibleRect());
-                URL fontURL = new URL(url);
-                URLConnection uc = fontURL.openConnection();
-                InputStream is = uc.getInputStream();
-                retVal = Font.createFont(Font.TRUETYPE_FONT, is);
-//                Font newFont = font.deriveFont((float) 18.0);
-//                wordText.setFont(newFont);
-                is.close();
-                fontCache.put(url, retVal);
-                statusBar.setText("New Font Loaded.");
+        Font retVal = new Font("Dialog", 0, 30);
+        if ((url != null) && (url.length() > 0)) {
+            try {
+                // see if our font is already loaded...
+                retVal = (Font)fontCache.get(url);
+                if (retVal == null) {
+                    statusBar.setText("Loading font...");
+                    statusBar.paintImmediately(statusBar.getVisibleRect());
+                    URL fontURL = new URL(url);
+                    URLConnection uc = fontURL.openConnection();
+                    InputStream is = uc.getInputStream();
+                    retVal = Font.createFont(Font.TRUETYPE_FONT, is);
+                    is.close();
+                    fontCache.put(url, retVal);
+                    statusBar.setText("New Font Loaded.");
+                }
             }
+            catch (Exception ex) {
+                ex.printStackTrace();
+            }
         }
-        catch (Exception ex) {
-            ex.printStackTrace();
-        }
         return retVal;
     }
 
@@ -363,15 +375,9 @@
 
           playSoundButton.setVisible(currentWord.getAudioURL() != null);
 
-          if (currentWord.getFontURL() != null) {
-               Font newFont = loadFont(currentWord.getFontURL());
-               if (newFont != null) {
-                    newFont = newFont.deriveFont((float) 30.0);
-                    wordText.setFont(newFont);
-               }
-          }
+          Font newFont = loadFont(currentWord.getFontURL()).deriveFont(optimalFontSize);
+          wordText.setFont(newFont);
 
-
           wordText.setText(w.getSide(!setupPane.isFlipped()));
           if (setupPane.isNoMultipleChoice()) {
                choicesPanel.invalidate();
@@ -384,7 +390,7 @@
 
               // randomly pick answers
               boolean flipped = setupPane.isFlipped();
-              List picks = new ArrayList();
+              picks = new ArrayList();
               picks.add(createAnswerEntry(w.getSide(flipped)));
               int size = words.size();
               while (picks.size() < Math.min(NUM_ANSWERS, size)) {
@@ -412,6 +418,7 @@
               }
               wrong = 0;
               shownAnswer = false;
+              setOptimalFontSizes();
               updateStats();
               choicesPanel.invalidate();
               choicesPanel.validate();
@@ -530,10 +537,77 @@
                showAnswer();
           }
      }
+     
+     
+     public Rectangle getMaxBounds(float fontSize) {
+         Graphics2D g2d = (Graphics2D)wordText.getGraphics();
+         Rectangle biggest = new Rectangle(0, 0, 0, 0);
+         FontRenderContext fc = g2d.getFontRenderContext();
+         Iterator it = notLearned.iterator();
+         while (it.hasNext()) {
+             WordEntry we = (WordEntry)it.next();
+             Font f = loadFont(we.getFontURL());
+             if (f != null) {
+                 f = f.deriveFont(fontSize);
+                 TextLayout tlo = new TextLayout(we.getSide(!setupPane.isFlipped()), f, fc);
+                 Rectangle2D rect = tlo.getBounds();
+                 if (rect.getWidth() > biggest.width) biggest.width = (int)rect.getWidth();
+                 if (rect.getHeight() > biggest.height) biggest.height = (int)rect.getHeight();
+             }
+         }
+         return biggest;
+     }
 
+     
+     public float getOptimalFontSize(Rectangle bounds) {
+         float fontSize = 30;
+         Rectangle referenceBounds = getMaxBounds(fontSize);
+System.out.println("30 pt Max Bound: " + referenceBounds.toString());
+
+         float xmult = (float)bounds.width / (float)referenceBounds.width;
+         float ymult = (float)bounds.height / (float)referenceBounds.height;
+         fontSize *= (xmult < ymult) ? xmult : ymult;
+         fontSize *= 0.75;
+         return fontSize;
+     }
+
+     public void setOptimalFontSizes() {
+         jPanel2.invalidate();
+         jPanel2.validate();
+         jPanel2.repaint();
+         // make a first pass at the font size to approximate the choices font
+         Rectangle bounds = jPanel2.getBounds();
+         bounds.height /= 2;
+         optimalFontSize = getOptimalFontSize(bounds);
+System.out.println("optimal Font Size: " + optimalFontSize);
+         Font newFont = loadFont(currentWord.getFontURL()).deriveFont(optimalFontSize);
+         Font choiceFont = newFont.deriveFont(optimalFontSize/(NUM_ANSWERS/NUM_COLUMNS));
+         Iterator iter = picks.iterator();
+         while (iter.hasNext()) {
+             ((Component) iter.next()).setFont(choiceFont);
+         }
+         // Now that bottom layout is adjusted for new font size, computer real
+         // font size for top
+         optimalFontSize = getOptimalFontSize(wordText.getBounds());
+         newFont = loadFont(currentWord.getFontURL()).deriveFont(optimalFontSize);
+System.out.println("optimal Font Size: " + optimalFontSize);
+         wordText.setFont(newFont);
+     }
+     
+     
+     protected void windowResized() {
+         if (wordText != null) {
+             if (wordText.getText() != null) {
+                 if (wordText.getText().length() > 0) {
+                     setOptimalFontSizes();
+                 }
+             }
+         }
+     }
 }
 
 
+
 class QuizPane_startLessonButton_actionAdapter
           implements ActionListener {
      QuizPane adaptee;




More information about the sword-cvs mailing list