Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Changing how a Window is drawn.

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Smile Changing how a Window is drawn.

    I've gotten the code for JFrame. How can I alter it so that it draws a Window like an alarm clock shape?
    It'll hold my java clock background. I'm just curious and how it would be done. How do you make a default look, i.e., say drawing a Window from a Polygon?

    1 /*
    2  * @(#)JFrame.java 1.104 03/12/19
    3  *
    4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
    5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
    6  */
    7 package javax.swing;
    8
    9 import java.awt.*;
    10 import java.awt.event.*;
    11 import java.beans.PropertyChangeListener JavaDoc;
    12 import java.util.Locale JavaDoc;
    13 import java.util.Vector JavaDoc;
    14 import java.io.Serializable JavaDoc;
    15
    16 import javax.accessibility.*;
    17
    18
    19 /**
    20  * An extended version of <code>java.awt.Frame</code> that adds support for
    21  * the JFC/Swing component architecture.
    22  * You can find task-oriented documentation about using <code>JFrame</code>
    23  * in <em>The Java Tutorial</em>, in the section
    24  * <a
    25  href="http://java.sun.com/docs/books/tutorial/uiswing/components/frame.html">How to Make Frames</a>.
    26  *
    27  * <p>
    28  * The <code>JFrame</code> class is slightly incompatible with <code>Frame</code>.
    29  * Like all other JFC/Swing top-level containers,
    30  * a <code>JFrame</code> contains a <code>JRootPane</code> as its only child.
    31  * The <b>content pane</b> provided by the root pane should,
    32  * as a rule, contain
    33  * all the non-menu components displayed by the <code>JFrame</code>.
    34  * This is different from the AWT <code>Frame</code> case.
    35  * As a conveniance <code>add</code> and its variants, <code>remove</code> and
    36  * <code>setLayout</code> have been overridden to forward to the
    37  * <code>contentPane</code> as necessary. This means you can write:
    38  * <pre>
    39  * frame.add(child);
    40  * </pre>
    41  * And the child will be added to the contentPane.
    42  * The content pane will
    43  * always be non-null. Attempting to set it to null will cause the JFrame
    44  * to throw an exception. The default content pane will have a BorderLayout
    45  * manager set on it.
    46  * Refer to {@link javax.swing.RootPaneContainer}
    47  * for details on adding, removing and setting the <code>LayoutManager</code>
    48  * of a <code>JFrame</code>.
    49  * <p>
    50  * Unlike a <code>Frame</code>, a <code>JFrame</code> has some notion of how to
    51  * respond when the user attempts to close the window. The default behavior
    52  * is to simply hide the JFrame when the user closes the window. To change the
    53  * default behavior, you invoke the method
    54  * {@link #setDefaultCloseOperation}.
    55  * To make the <code>JFrame</code> behave the same as a <code>Frame</code>
    56  * instance, use
    57  * <code>setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)</code>.
    58  * <p>
    59  * For more information on content panes
    60  * and other features that root panes provide,
    61  * see <a
    62  href="http://java.sun.com/docs/books/tutorial/uiswing/components/toplevel.html">Using Top-Level Containers</a> in <em>The Java Tutorial</em>.
    63  * <p>
    64  * In a multi-screen environment, you can create a <code>JFrame</code>
    65  * on a different screen device. See {@link java.awt.Frame} for more
    66  * information.
    67  * <p>
    68  * <strong>Warning:</strong>
    69  * Serialized objects of this class will not be compatible with
    70  * future Swing releases. The current serialization support is
    71  * appropriate for short term storage or RMI between applications running
    72  * the same version of Swing. As of 1.4, support for long term storage
    73  * of all JavaBeans<sup><font size="-2">TM</font></sup>
    74  * has been added to the <code>java.beans</code> package.
    75  * Please see {@link java.beans.XMLEncoder}.
    76  *
    77  * @see JRootPane
    78  * @see #setDefaultCloseOperation
    79  * @see java.awt.event.WindowListener#windowClosing
    80  * @see javax.swing.RootPaneContainer
    81  *
    82  * @beaninfo
    83  * attribute: isContainer true
    84  * attribute: containerDelegate getContentPane
    85  * description: A toplevel window which can be minimized to an icon.
    86  *
    87  * @version 1.104 12/19/03
    88  * @author Jeff Dinkins
    89  * @author Georges Saab
    90  * @author David Kloba
    91  */
    92 public class JFrame extends Frame implements WindowConstants JavaDoc, Accessible, RootPaneContainer JavaDoc
    93 {
    94     /**
    95      * The exit application default window close operation. If a window
    96      * has this set as the close operation and is closed in an applet,
    97      * a <code>SecurityException</code> may be thrown.
    98      * It is recommended you only use this in an application.
    99      * <p>
    100      * @since 1.3
    101      */
    102     public static final int EXIT_ON_CLOSE = 3;
    103
    104     /**
    105      * Key into the AppContext, used to check if should provide decorations
    106      * by default.
    107      */
    108     private static final Object JavaDoc defaultLookAndFeelDecoratedKey =
    109             new StringBuffer JavaDoc("JFrame.defaultLookAndFeelDecorated");
    110
    111     private int defaultCloseOperation = HIDE_ON_CLOSE;
    112
    113     /**
    114      * The <code>JRootPane</code> instance that manages the
    115      * <code>contentPane</code>
    116      * and optional <code>menuBar</code> for this frame, as well as the
    117      * <code>glassPane</code>.
    118      *
    119      * @see JRootPane
    120      * @see RootPaneContainer
    121      */
    122     protected JRootPane JavaDoc rootPane;
    123
    124     /**
    125      * If true then calls to <code>add</code> and <code>setLayout</code>
    126      * will be forwarded to the <code>contentPane</code>. This is initially
    127      * false, but is set to true when the <code>JFrame</code> is constructed.
    128      *
    129      * @see #isRootPaneCheckingEnabled
    130      * @see #setRootPaneCheckingEnabled
    131      * @see javax.swing.RootPaneContainer
    132      */
    133     protected boolean rootPaneCheckingEnabled = false;
    134
    135
    136     /**
    137      * Constructs a new frame that is initially invisible.
    138      * <p>
    139      * This constructor sets the component's locale property to the value
    140      * returned by <code>JComponent.getDefaultLocale</code>.
    141      *
    142      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
    143      * returns true.
    144      * @see java.awt.GraphicsEnvironment#isHeadless
    145      * @see Component#setSize
    146      * @see Component#setVisible
    147      * @see JComponent#getDefaultLocale
    148      */
    149     public JFrame() throws HeadlessException {
    150         super();
    151         frameInit();
    152     }
    153
    154     /**
    155      * Creates a <code>Frame</code> in the specified
    156      * <code>GraphicsConfiguration</code> of
    157      * a screen device and a blank title.
    158      * <p>
    159      * This constructor sets the component's locale property to the value
    160      * returned by <code>JComponent.getDefaultLocale</code>.
    161      *
    162      * @param gc the <code>GraphicsConfiguration</code> that is used
    163      * to construct the new <code>Frame</code>;
    164      * if <code>gc</code> is <code>null</code>, the system
    165      * default <code>GraphicsConfiguration</code> is assumed
    166      * @exception IllegalArgumentException if <code>gc</code> is not from
    167      * a screen device. This exception is always thrown when
    168      * GraphicsEnvironment.isHeadless() returns true.
    169      * @see java.awt.GraphicsEnvironment#isHeadless
    170      * @see JComponent#getDefaultLocale
    171      * @since 1.3
    172      */
    173     public JFrame(GraphicsConfiguration gc) {
    174         super(gc);
    175         frameInit();
    176     }
    177
    178     /**
    179      * Creates a new, initially invisible <code>Frame</code> with the
    180      * specified title.
    181      * <p>
    182      * This constructor sets the component's locale property to the value
    183      * returned by <code>JComponent.getDefaultLocale</code>.
    184      *
    185      * @param title the title for the frame
    186      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
    187      * returns true.
    188      * @see java.awt.GraphicsEnvironment#isHeadless
    189      * @see Component#setSize
    190      * @see Component#setVisible
    191      * @see JComponent#getDefaultLocale
    192      */
    193     public JFrame(String JavaDoc title) throws HeadlessException {
    194         super(title);
    195         frameInit();
    196     }
    197     
    198     /**
    199      * Creates a <code>JFrame</code> with the specified title and the
    200      * specified <code>GraphicsConfiguration</code> of a screen device.
    201      * <p>
    202      * This constructor sets the component's locale property to the value
    203      * returned by <code>JComponent.getDefaultLocale</code>.
    204      *
    205      * @param title the title to be displayed in the
    206      * frame's border. A <code>null</code> value is treated as
    207      * an empty string, "".
    208      * @param gc the <code>GraphicsConfiguration</code> that is used
    209      * to construct the new <code>JFrame</code> with;
    210      * if <code>gc</code> is <code>null</code>, the system
    211      * default <code>GraphicsConfiguration</code> is assumed
    212      * @exception IllegalArgumentException if <code>gc</code> is not from
    213      * a screen device. This exception is always thrown when
    214      * GraphicsEnvironment.isHeadless() returns true.
    215      * @see java.awt.GraphicsEnvironment#isHeadless
    216      * @see JComponent#getDefaultLocale
    217      * @since 1.3
    218      */
    219     public JFrame(String JavaDoc title, GraphicsConfiguration gc) {
    220         super(title, gc);
    221         frameInit();
    222     }
    223
    224     /** Called by the constructors to init the <code>JFrame</code> properly. */
    225     protected void frameInit() {
    226         enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.WINDOW_EVENT_MASK);
    227         setLocale( JComponent.getDefaultLocale() );
    228         setRootPane(createRootPane());
    229         setBackground(UIManager.getColor("control"));
    230         setRootPaneCheckingEnabled(true);
    231         if (JFrame.isDefaultLookAndFeelDecorated()) {
    232             boolean supportsWindowDecorations =
    233             UIManager.getLookAndFeel().getSupportsWindowDecorations();
    234             if (supportsWindowDecorations) {
    235                 setUndecorated(true);
    236                 getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
    237             }
    238         }
    239         sun.awt.SunToolkit.checkAndSetPolicy(this, true);
    240     }
    241
    242     /**
    243      * Called by the constructor methods to create the default
    244      * <code>rootPane</code>.
    245      */
    246     protected JRootPane JavaDoc createRootPane() {
    247         JRootPane JavaDoc rp = new JRootPane JavaDoc();
    248         // NOTE: this uses setOpaque vs LookAndFeel.installProperty as there
    249 // is NO reason for the RootPane not to be opaque. For painting to
    250 // work the contentPane must be opaque, therefor the RootPane can
    251 // also be opaque.
    252 rp.setOpaque(true);
    253         return rp;
    254     }
    255  
    256     /**
    257      * Processes window events occurring on this component.
    258      * Hides the window or disposes of it, as specified by the setting
    259      * of the <code>defaultCloseOperation</code> property.
    260      *
    261      * @param e the window event
    262      * @see #setDefaultCloseOperation
    263      * @see java.awt.Window#processWindowEvent
    264      */
    265     protected void processWindowEvent(WindowEvent e) {
    266         super.processWindowEvent(e);
    267
    268         if (e.getID() == WindowEvent.WINDOW_CLOSING) {
    269             switch(defaultCloseOperation) {
    270               case HIDE_ON_CLOSE:
    271                  setVisible(false);
    272                  break;
    273               case DISPOSE_ON_CLOSE:
    274                  setVisible(false);
    275                  dispose();
    276                  break;
    277               case DO_NOTHING_ON_CLOSE:
    278                  default:
    279                  break;
    280           case EXIT_ON_CLOSE:
    281                   // This needs to match the checkExit call in
    282 // setDefaultCloseOperation
    283 System.exit(0);
    284         break;
    285             }
    286         }
    287     }
    288
    289 // public void setMenuBar(MenuBar menu) {
    290 // throw new IllegalComponentStateException("Please use setJMenuBar() with JFrame.");
    291 // }
    292
    293     /**
    294      * Sets the operation that will happen by default when
    295      * the user initiates a "close" on this frame.
    296      * You must specify one of the following choices:
    297      * <p>
    298      * <ul>
    299      * <li><code>DO_NOTHING_ON_CLOSE</code>
    300      * (defined in <code>WindowConstants</code>):
    301      * Don't do anything; require the
    302      * program to handle the operation in the <code>windowClosing</code>
    303      * method of a registered <code>WindowListener</code> object.
    304      *
    305      * <li><code>HIDE_ON_CLOSE</code>
    306      * (defined in <code>WindowConstants</code>):
    307      * Automatically hide the frame after
    308      * invoking any registered <code>WindowListener</code>
    309      * objects.
    310      *
    311      * <li><code>DISPOSE_ON_CLOSE</code>
    312      * (defined in <code>WindowConstants</code>):
    313      * Automatically hide and dispose the
    314      * frame after invoking any registered <code>WindowListener</code>
    315      * objects.
    316      *
    317      * <li><code>EXIT_ON_CLOSE</code>
    318      * (defined in <code>JFrame</code>):
    319      * Exit the application using the <code>System</code>
    320      * <code>exit</code> method. Use this only in applications.
    321      * </ul>
    322      * <p>
    323      * The value is set to <code>HIDE_ON_CLOSE</code> by default.
    324      * <p>
    325      * <b>Note</b>: When the last displayable window within the
    326      * Java virtual machine (VM) is disposed of, the VM may
    327      * terminate. See <a HREF="../../java/awt/doc-files/AWTThreadIssues.html">
    328      * AWT Threading Issues</a> for more information.
    329      *
    330      * @param operation the operation which should be performed when the
    331      * user closes the frame
    332      * @exception IllegalArgumentException if defaultCloseOperation value
    333      * isn't one of the above valid values
    334      * @see #addWindowListener
    335      * @see #getDefaultCloseOperation
    336      * @see WindowConstants
    337      * @throws SecurityException
    338      * if <code>EXIT_ON_CLOSE</code> has been specified and the
    339      * <code>SecurityManager</code> will
    340      * not allow the caller to invoke <code>System.exit</code>
    341      * @see java.lang.Runtime#exit(int)
    342      *
    343      * @beaninfo
    344      * preferred: true
    345      * bound: true
    346      * enum: DO_NOTHING_ON_CLOSE WindowConstants.DO_NOTHING_ON_CLOSE
    347      * HIDE_ON_CLOSE WindowConstants.HIDE_ON_CLOSE
    348      * DISPOSE_ON_CLOSE WindowConstants.DISPOSE_ON_CLOSE
    349      * EXIT_ON_CLOSE WindowConstants.EXIT_ON_CLOSE
    350      * description: The frame's default close operation.
    351      */
    352     public void setDefaultCloseOperation(int operation) {
    353     if (operation != DO_NOTHING_ON_CLOSE &&
    354         operation != HIDE_ON_CLOSE &&
    355         operation != DISPOSE_ON_CLOSE &&
    356         operation != EXIT_ON_CLOSE) {
    357             throw new IllegalArgumentException JavaDoc("defaultCloseOperation must be one of: DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, DISPOSE_ON_CLOSE, or EXIT_ON_CLOSE");
    358     }
    359         if (this.defaultCloseOperation != operation) {
    360             if (operation == EXIT_ON_CLOSE) {
    361                 SecurityManager JavaDoc security = System.getSecurityManager();
    362                 if (security != null) {
    363                     security.checkExit(0);
    364                 }
    365             }
    366             int oldValue = this.defaultCloseOperation;
    367             this.defaultCloseOperation = operation;
    368             firePropertyChange("defaultCloseOperation", oldValue, operation);
    369     }
    370     }
    371
    372
    373    /**
    374     * Returns the operation that occurs when the user
    375     * initiates a "close" on this frame.
    376     *
    377     * @return an integer indicating the window-close operation
    378     * @see #setDefaultCloseOperation
    379     */
    380     public int getDefaultCloseOperation() {
    381         return defaultCloseOperation;
    382     }
    383
    384
    385     /**
    386      * Just calls <code>paint(g)</code>. This method was overridden to
    387      * prevent an unnecessary call to clear the background.
    388      *
    389      * @param g the Graphics context in which to paint
    390      */
    391     public void update(Graphics g) {
    392         paint(g);
    393     }
    394
    395    /**
    396     * Sets the menubar for this frame.
    397     * @param menubar the menubar being placed in the frame
    398     *
    399     * @see #getJMenuBar
    400     *
    401     * @beaninfo
    402     * hidden: true
    403     * description: The menubar for accessing pulldown menus from this frame.
    404     */
    405     public void setJMenuBar(JMenuBar JavaDoc menubar) {
    406         getRootPane().setMenuBar(menubar);
    407     }
    408
    409    /**
    410     * Returns the menubar set on this frame.
    411     * @return the menubar for this frame
    412     *
    413     * @see #setJMenuBar
    414     */
    415     public JMenuBar JavaDoc getJMenuBar() {
    416         return getRootPane().getMenuBar();
    417     }
    418
    419     /**
    420      * Returns whether calls to <code>add</code> and
    421      * <code>setLayout</code> are forwarded to the <code>contentPane</code>.
    422      *
    423      * @return true if <code>add</code> and <code>setLayout</code>
    424      * are fowarded; false otherwise
    425      *
    426      * @see #addImpl
    427      * @see #setLayout
    428      * @see #setRootPaneCheckingEnabled
    429      * @see javax.swing.RootPaneContainer
    430      */
    431     protected boolean isRootPaneCheckingEnabled() {
    432         return rootPaneCheckingEnabled;
    433     }
    434
    435
    436     /**
    437      * Sets whether calls to <code>add</code> and
    438      * <code>setLayout</code> are forwarded to the <code>contentPane</code>.
    439      *
    440      * @param enabled true if <code>add</code> and <code>setLayout</code>
    441      * are forwarded, false if they should operate directly on the
    442      * <code>JFrame</code>.
    443      *
    444      * @see #addImpl
    445      * @see #setLayout
    446      * @see #isRootPaneCheckingEnabled
    447      * @see javax.swing.RootPaneContainer
    448      * @beaninfo
    449      * hidden: true
    450      * description: Whether the add and setLayout methods are forwarded
    451      */
    452     protected void setRootPaneCheckingEnabled(boolean enabled) {
    453         rootPaneCheckingEnabled = enabled;
    454     }
    455
    456
    457     /**
    458      * Adds the specified child <code>Component</code>.
    459      * This method is overridden to conditionally forwad calls to the
    460      * <code>contentPane</code>.
    461      * By default, children are added to the <code>contentPane</code> instead
    462      * of the frame, refer to {@link javax.swing.RootPaneContainer} for
    463      * details.
    464      *
    465      * @param comp the component to be enhanced
    466      * @param constraints the constraints to be respected
    467      * @param index the index
    468      * @exception IllegalArgumentException if <code>index</code> is invalid
    469      * @exception IllegalArgumentException if adding the container's parent
    470      * to itself
    471      * @exception IllegalArgumentException if adding a window to a container
    472      *
    473      * @see #setRootPaneCheckingEnabled
    474      * @see javax.swing.RootPaneContainer
    475      */
    476     protected void addImpl(Component comp, Object JavaDoc constraints, int index)
    477     {
    478         if(isRootPaneCheckingEnabled()) {
    479             getContentPane().add(comp, constraints, index);
    480         }
    481         else {
    482             super.addImpl(comp, constraints, index);
    483         }
    484     }
    485
    486     /**
    487      * Removes the specified component from the container. If
    488      * <code>comp</code> is not the <code>rootPane</code>, this will forward
    489      * the call to the <code>contentPane</code>. This will do nothing if
    490      * <code>comp</code> is not a child of the <code>JFrame</code> or
    491      * <code>contentPane</code>.
    492      *
    493      * @param comp the component to be removed
    494      * @throws NullPointerException if <code>comp</code> is null
    495      * @see #add
    496      * @see javax.swing.RootPaneContainer
    497      */
    498     public void remove(Component comp) {
    499     if (comp == rootPane) {
    500         super.remove(comp);
    501     } else {
    502         getContentPane().remove(comp);
    503     }
    504     }
    505
    506
    507     /**
    508      * Sets the <code>LayoutManager</code>.
    509      * Overridden to conditionally forward the call to the
    510      * <code>contentPane</code>.
    511      * Refer to {@link javax.swing.RootPaneContainer} for
    512      * more information.
    513      *
    514      * @param manager the <code>LayoutManager</code>
    515      * @see #setRootPaneCheckingEnabled
    516      * @see javax.swing.RootPaneContainer
    517      */
    518     public void setLayout(LayoutManager manager) {
    519         if(isRootPaneCheckingEnabled()) {
    520             getContentPane().setLayout(manager);
    521         }
    522         else {
    523             super.setLayout(manager);
    524         }
    525     }
    526
    527
    528     /**
    529      * Returns the <code>rootPane</code> object for this frame.
    530      * @return the <code>rootPane</code> property
    531      *
    532      * @see #setRootPane
    533      * @see RootPaneContainer#getRootPane
    534      */
    535     public JRootPane JavaDoc getRootPane() {
    536         return rootPane;
    537     }
    538
    539
    540     /**
    541      * Sets the <code>rootPane</code> property.
    542      * This method is called by the constructor.
    543      * @param root the <code>rootPane</code> object for this frame
    544      *
    545      * @see #getRootPane
    546      *
    547      * @beaninfo
    548      * hidden: true
    549      * description: the RootPane object for this frame.
    550      */
    551     protected void setRootPane(JRootPane JavaDoc root)
    552     {
    553         if(rootPane != null) {
    554             remove(rootPane);
    555         }
    556         rootPane = root;
    557         if(rootPane != null) {
    558             boolean checkingEnabled = isRootPaneCheckingEnabled();
    559             try {
    560                 setRootPaneCheckingEnabled(false);
    561                 add(rootPane, BorderLayout.CENTER);
    562             }
    563             finally {
    564                 setRootPaneCheckingEnabled(checkingEnabled);
    565             }
    566         }
    567     }
    568
    569     public void setIconImage(Image image) {
    570         Image oldImage = getIconImage();
    571         super.setIconImage(image);
    572         firePropertyChange("iconImage", oldImage, image);
    573     }
    574
    575     /**
    576      * Returns the <code>contentPane</code> object for this frame.
    577      * @return the <code>contentPane</code> property
    578      *
    579      * @see #setContentPane
    580      * @see RootPaneContainer#getContentPane
    581      */
    582     public Container getContentPane() {
    583         return getRootPane().getContentPane();
    584     }
    585
    586     /**
    587      * Sets the <code>contentPane</code> property.
    588      * This method is called by the constructor.
    589      * <p>
    590      * Swing's painting architecture requires an opaque <code>JComponent</code>
    591      * in the containment hiearchy. This is typically provided by the
    592      * content pane. If you replace the content pane it is recommended you
    593      * replace it with an opaque <code>JComponent</code>.
    594      *
    595      * @param contentPane the <code>contentPane</code> object for this frame
    596      *
    597      * @exception java.awt.IllegalComponentStateException (a runtime
    598      * exception) if the content pane parameter is <code>null</code>
    599      * @see #getContentPane
    600      * @see RootPaneContainer#setContentPane
    601      * @see JRootPane
    602      *
    603      * @beaninfo
    604      * hidden: true
    605      * description: The client area of the frame where child
    606      * components are normally inserted.
    607      */
    608     public void setContentPane(Container contentPane) {
    609         getRootPane().setContentPane(contentPane);
    610     }
    611
    612     /**
    613      * Returns the <code>layeredPane</code> object for this frame.
    614      * @return the <code>layeredPane</code> property
    615      *
    616      * @see #setLayeredPane
    617      * @see RootPaneContainer#getLayeredPane
    618      */
    619     public JLayeredPane JavaDoc getLayeredPane() {
    620         return getRootPane().getLayeredPane();
    621     }
    622
    623     /**
    624      * Sets the <code>layeredPane</code> property.
    625      * This method is called by the constructor.
    626      * @param layeredPane the <code>layeredPane</code> object for this frame
    627      *
    628      * @exception java.awt.IllegalComponentStateException (a runtime
    629      * exception) if the layered pane parameter is <code>null</code>
    630      * @see #getLayeredPane
    631      * @see RootPaneContainer#setLayeredPane
    632      *
    633      * @beaninfo
    634      * hidden: true
    635      * description: The pane that holds the various frame layers.
    636      */
    637     public void setLayeredPane(JLayeredPane JavaDoc layeredPane) {
    638         getRootPane().setLayeredPane(layeredPane);
    639     }
    640
    641     /**
    642      * Returns the <code>glassPane</code> object for this frame.
    643      * @return the <code>glassPane</code> property
    644      *
    645      * @see #setGlassPane
    646      * @see RootPaneContainer#getGlassPane
    647      */
    648     public Component getGlassPane() {
    649         return getRootPane().getGlassPane();
    650     }
    651
    652     /**
    653      * Sets the <code>glassPane</code> property.
    654      * This method is called by the constructor.
    655      * @param glassPane the <code>glassPane</code> object for this frame
    656      *
    657      * @see #getGlassPane
    658      * @see RootPaneContainer#setGlassPane
    659      *
    660      * @beaninfo
    661      * hidden: true
    662      * description: A transparent pane used for menu rendering.
    663      */
    664     public void setGlassPane(Component glassPane) {
    665         getRootPane().setGlassPane(glassPane);
    666     }
    667
    668     /**
    669      * Provides a hint as to whether or not newly created <code>JFrame</code>s
    670      * should have their Window decorations (such as borders, widgets to
    671      * close the window, title...) provided by the current look
    672      * and feel. If <code>defaultLookAndFeelDecorated</code> is true,
    673      * the current <code>LookAndFeel</code> supports providing window
    674      * decorations, and the current window manager supports undecorated
    675      * windows, then newly created <code>JFrame</code>s will have their
    676      * Window decorations provided by the current <code>LookAndFeel</code>.
    677      * Otherwise, newly created <code>JFrame</code>s will have their
    678      * Window decorations provided by the current window manager.
    679      * <p>
    680      * You can get the same effect on a single JFrame by doing the following:
    681      * <pre>
    682      * JFrame frame = new JFrame();
    683      * frame.setUndecorated(true);
    684      * frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
    685      * </pre>
    686      *
    687      * @param defaultLookAndFeelDecorated A hint as to whether or not current
    688      * look and feel should provide window decorations
    689      * @see javax.swing.LookAndFeel#getSupportsWindowDecorations
    690      * @since 1.4
    691      */
    692     public static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated) {
    693         if (defaultLookAndFeelDecorated) {
    694             SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.TRUE);
    695         } else {
    696             SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.FALSE);
    697         }
    698     }
    699
    700
    701     /**
    702      * Returns true if newly created <code>JFrame</code>s should have their
    703      * Window decorations provided by the current look and feel. This is only
    704      * a hint, as certain look and feels may not support this feature.
    705      *
    706      * @return true if look and feel should provide Window decorations.
    707      * @since 1.4
    708      */
    709     public static boolean isDefaultLookAndFeelDecorated() {
    710         Boolean JavaDoc defaultLookAndFeelDecorated =
    711             (Boolean JavaDoc) SwingUtilities.appContextGet(defaultLookAndFeelDecoratedKey);
    712         if (defaultLookAndFeelDecorated == null) {
    713             defaultLookAndFeelDecorated = Boolean.FALSE;
    714         }
    715         return defaultLookAndFeelDecorated.booleanValue();
    716     }
    717
    718     /**
    719      * Returns a string representation of this <code>JFrame</code>.
    720      * This method
    721      * is intended to be used only for debugging purposes, and the
    722      * content and format of the returned string may vary between
    723      * implementations. The returned string may be empty but may not
    724      * be <code>null</code>.
    725      *
    726      * @return a string representation of this <code>JFrame</code>
    727      */
    728     protected String JavaDoc paramString() {
    729         String JavaDoc defaultCloseOperationString;
    730         if (defaultCloseOperation == HIDE_ON_CLOSE) {
    731             defaultCloseOperationString = "HIDE_ON_CLOSE";
    732         } else if (defaultCloseOperation == DISPOSE_ON_CLOSE) {
    733             defaultCloseOperationString = "DISPOSE_ON_CLOSE";
    734         } else if (defaultCloseOperation == DO_NOTHING_ON_CLOSE) {
    735             defaultCloseOperationString = "DO_NOTHING_ON_CLOSE";
    736         } else if (defaultCloseOperation == 3) {
    737             defaultCloseOperationString = "EXIT_ON_CLOSE";
    738         } else defaultCloseOperationString = "";
    739     String JavaDoc rootPaneString = (rootPane != null ?
    740                  rootPane.toString() : "");
    741     String JavaDoc rootPaneCheckingEnabledString = (rootPaneCheckingEnabled ?
    742                         "true" : "false");
    743
    744     return super.paramString() +
    745     ",defaultCloseOperation=" + defaultCloseOperationString +
    746     ",rootPane=" + rootPaneString +
    747     ",rootPaneCheckingEnabled=" + rootPaneCheckingEnabledString;
    748     }
    749
    750
    751
    752 /////////////////
    753 // Accessibility support
    754 ////////////////
    755
    756     /** The accessible context property. */
    757     protected AccessibleContext accessibleContext = null;
    758
    759     /**
    760      * Gets the AccessibleContext associated with this JFrame.
    761      * For JFrames, the AccessibleContext takes the form of an
    762      * AccessibleJFrame.
    763      * A new AccessibleJFrame instance is created if necessary.
    764      *
    765      * @return an AccessibleJFrame that serves as the
    766      * AccessibleContext of this JFrame
    767      */
    768     public AccessibleContext getAccessibleContext() {
    769         if (accessibleContext == null) {
    770             accessibleContext = new AccessibleJFrame();
    771         }
    772         return accessibleContext;
    773     }
    774
    775     /**
    776      * This class implements accessibility support for the
    777      * <code>JFrame</code> class. It provides an implementation of the
    778      * Java Accessibility API appropriate to frame user-interface
    779      * elements.
    780      */
    781     protected class AccessibleJFrame extends AccessibleAWTFrame {
    782
    783         // AccessibleContext methods
    784 /**
    785          * Get the accessible name of this object.
    786          *
    787          * @return the localized name of the object -- can be null if this
    788          * object does not have a name
    789          */
    790         public String JavaDoc getAccessibleName() {
    791             if (accessibleName != null) {
    792                 return accessibleName;
    793             } else {
    794                 if (getTitle() == null) {
    795                     return super.getAccessibleName();
    796                 } else {
    797                     return getTitle();
    798                 }
    799             }
    800         }
    801
    802         /**
    803          * Get the state of this object.
    804          *
    805          * @return an instance of AccessibleStateSet containing the current
    806          * state set of the object
    807          * @see AccessibleState
    808          */
    809         public AccessibleStateSet getAccessibleStateSet() {
    810             AccessibleStateSet states = super.getAccessibleStateSet();
    811
    812             if (isResizable()) {
    813                 states.add(AccessibleState.RESIZABLE);
    814             }
    815             if (getFocusOwner() != null) {
    816                 states.add(AccessibleState.ACTIVE);
    817             }
    818             // FIXME: [[[WDW - should also return ICONIFIED and ICONIFIABLE
    819 // if we can ever figure these out]]]
    820 return states;
    821         }
    822     } // inner class AccessibleJFrame
    823 }

    import java.awt.*;
    import javax.swing.*;       
    import java.util.*;
     
    class Clock extends JFrame implements Runnable
    {
      Thread runner; //declare global objects
      Font clockFont;
     
         public Clock()
         {
           super("Java clock");
           setSize( 350, 100);
           setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           setVisible(true);
           setResizable(false);                             //create window
     
           clockFont = new Font("Serif", Font.BOLD, 40);    //create font instance
     
           Container contentArea = getContentPane();
           ClockPanel timeDisplay = new ClockPanel();
     
     
           contentArea.add(timeDisplay);                    //add components
           setContentPane(contentArea);
           start();                                         //start thread running
     
         }
     
     
         class ClockPanel extends JPanel
         {
          public void paintComponent(Graphics painter )
            {
            Image pic =
              Toolkit.getDefaultToolkit().getImage("background.jpg");
     
             if(pic != null)
     
                painter.drawImage(pic, 0, 0, this);     //create image
     
     
    //if I didn't use a background image I would have used the setColor and fillRect methods to set background
     
              painter.setFont(clockFont);                   //create clock components
              painter.setColor(Color.black);
              painter.drawString( timeNow(), 60, 40);
     
     
            }
         }
     
     
         //get current time
         public String timeNow()
         {
           Calendar now = Calendar.getInstance();
           int hrs = now.get(Calendar.HOUR);
           int min = now.get(Calendar.MINUTE);
           int sec = now.get(Calendar.SECOND);
           int meridian = now.get(Calendar.HOUR_OF_DAY);
     
           String meridian2 = "";
           if (meridian >=0 && meridian <12)
        	   meridian2 = "A.M.";
           else
        	   meridian2 = "P.M.";
           String time = zero(hrs)+":"+zero(min)+":"+zero(sec) + " " + meridian2;
     
           return time;
         }
     
     
     
         public String zero(int num)
         {
           String number=( num < 10) ? ("0"+num) : (""+num);
           return number;                                    //Add leading zero if needed
     
         }
     
     
         public void start()
         {
           if(runner == null) runner = new Thread(this);
           runner.start();
                                                                 //method to start thread
         }
     
     
         public void run()
         {
           while (runner == Thread.currentThread() )
           {
            repaint();
                                                             //define thread task
               try
                 {
                   Thread.sleep(1000);
                 }
                  catch(InterruptedException e)
                      {
                        System.out.println("Thread failed");
                      }
     
           }
         }
     
         //create main method
         public static void main(String [] args)
         {
           Clock eg = new Clock();
           eg.setBackground(Color.WHITE);
         }
    }

    Id like to put the two together and make it look like an actual digital clock. Is that possible, and if so, is it very hard and advanced to do?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Changing how a Window is drawn.


  3. The Following User Says Thank You to copeg For This Useful Post:

    javapenguin (January 8th, 2011)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Changing how a Window is drawn.

    Access restriction: The method setOpacity(Window, float) from the type AWTAccessor.WindowAccessor is not accessible due to restriction on required library C:\Program Files\Java\jre6\lib\rt.jar
    How do I fix it so that these imports will work?

Similar Threads

  1. Boolean Value Not Changing
    By bosox960 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 21st, 2010, 02:11 PM
  2. changing the splitpane's cursor
    By chronoz13 in forum AWT / Java Swing
    Replies: 2
    Last Post: February 8th, 2010, 12:11 PM
  3. Help With Java - Changing a File
    By RonenIL in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 5th, 2010, 02:34 AM
  4. Parameter's value is never changing
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 27th, 2009, 05:29 AM