FileDocCategorySizeDatePackage
ColorPane.javaAPI DocExample2196Thu Oct 24 20:14:26 BST 2002None

ColorPane

public class ColorPane extends JTextPane

Fields Summary
Constructors Summary
Methods Summary
public voidappend(java.awt.Color c, java.lang.String s)

 // better implementation--uses StyleContext
    StyleContext sc = StyleContext.getDefaultStyleContext();
    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY,
                                        StyleConstants.Foreground, c);
  
    int len = getDocument().getLength(); // same value as getText().length();
    setCaretPosition(len);  // place caret at the end (with no selection)
    setCharacterAttributes(aset, false);
    replaceSelection(s); // there is no selection, so inserts at caret
  
public voidappendNaive(java.awt.Color c, java.lang.String s)

 // naive implementation
    // bad: instiantiates a new AttributeSet object on each call
    SimpleAttributeSet aset = new SimpleAttributeSet();
    StyleConstants.setForeground(aset, c);
  
    int len = getText().length();
    setCaretPosition(len); // place caret at the end (with no selection)
    setCharacterAttributes(aset, false);
    replaceSelection(s); // there is no selection, so inserts at caret
  
public static booleanisPerfectSquare(int n)

    int j = 1;
    while (j*j < n  &&  j*j > 0)
      j += 1;
    return (j*j == n);
  
public static booleanisPrime(int n)

    if (n < 2) return false;
    double max = Math.sqrt(n);
    for (int j=2; j <= max; j+=1)
      if (n % j == 0) return false; // j is a factor
    return true;
  
public static voidmain(java.lang.String[] argv)


    ColorPane pane = new ColorPane();
    for (int n=1; n <= 400; n+=1) {
      if (isPrime(n)) {
        pane.append(Color.red, String.valueOf(n)+' ");
      } else if (isPerfectSquare(n)) {
        pane.append(Color.blue, String.valueOf(n)+' ");
      } else {
        pane.append(Color.black, String.valueOf(n)+' ");
      }
    }

    JFrame f = new JFrame("ColorPane example");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(new JScrollPane(pane));
    f.setSize(600, 400);
    f.setVisible(true);