⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 practice.html

📁 jsf、swing的官方指南
💻 HTML
📖 第 1 页 / 共 2 页
字号:
    <div id=MainFlow class=MainFlow_indented>            <span id=BreadCrumbs>                <a href=../../index.html target=_top>Home Page</a>                &gt;                <a href=../index.html target=_top>Creating a GUI with JFC/Swing</a>                &gt;                <a href=index.html target=_top>Performing Custom Painting</a>            </span>            <div class=NavBit>                <a target=_top href=concepts2.html>&laquo;&nbsp;Previous</a>&nbsp;&bull;&nbsp;<a target=_top href=../TOC.html>Trail</a>&nbsp;&bull;&nbsp;<a target=_top href=summary.html>Next&nbsp;&raquo;</a>            </div>            <div id=PageTitle>Implementing a Custom Component</div>            <blockquote>Before you implement a component that performs custom painting,first make sure that you really need to do so.You might be able to use the text and imagecapabilities of<a class="TutorialLink" target="_top" href="../components/label.html">labels</a>,<a class="TutorialLink" target="_top" href="../components/button.html">buttons</a>, or<a class="TutorialLink" target="_top" href="../components/text.html">text components</a> instead.And remember, you can sometimes use <a class="TutorialLink" target="_top" href="../components/border.html">borders</a> to customize the outside edgesof a component and <a class="TutorialLink" target="_top" href="../components/icon.html">icons</a> to paint an areathat perhaps varies by component state.If you need to make changes to many standard components,you should consider doing it by customizing a <a class="TutorialLink" target="_top" href="../lookandfeel/plaf.html">look and feel</a> such as the GTK+ look and feel.<p>If you really need to perform custom painting,then you need to decide which superclass to use.Your component can extend <code>JComponent</code>,<code>JPanel</code>,or a more specialized Swing component class.<p>For example, if you're creating a custom button class,you should probably implement it by extending a button classsuch as <code>JButton</code> or <code>JToggleButton</code>.That way you'll inherit the state management provided by those classes.If you're creating a component that paints on top of an image,you might want to create a <code>JLabel</code> subclass.A component that's a specialized containershould probably extend <code>JPanel</code>.On the other hand, if you're implementing a componentthat generates and displays a graph, for example &#151;with or without providing user interaction &#151;then you might want to use a <code>JComponent</code> subclass.<p>When implementing custom painting codefor a component,keep these rules in mind:<ul><li> Your custom painting code should be in a method with the     signature <code>protected void paintComponent(Graphics)</code>.<li> You can &#151; and probably should &#151; use a border     to paint the outside edges     of your component.<li> Except when painting the background of the component,     you should avoid painting over the border area of the     component.     You can determine this area      using the <code>getInsets</code> method.<li> Your component must honor the <em>opaque</em> property.     If your component is opaque,     it must paint its complete area     using an opaque color or colors.     If its <em>opaque</em> property is false,     then you have the option of not painting over the entire component.<li> Make sure that when the <code>paintComponent</code> method exits,     the <code>Graphics</code> object that was passed into it     has the same state that it had     at the start of the method.<li> To gain access to the power of the <a class="TutorialLink" target="_top" href="../../2d/index.html">2D graphics API</a>,     you can cast the <code>Graphics</code> parameter     into a <code>Graphics2D</code> object.</ul><p>Besides those painting-related considerations,here are a few more rules to keep in mind:<ul><li> Your component should return reasonable size information.     Specifically, you should either override the     <code>getMinimumSize</code>, <code>getPreferredSize</code>,     and <code>getMaximumSize</code> methods     or make sure that your component's superclass     supplies values that are appropriate.<li> Your component should be as accessible as possible.     For details, see<a class="TutorialLink" target="_top" href="../misc/access.html#accessiblecomponents">Making Custom Components Accessible</a>.<li> You should separate out strings and resources     such as images     so that your component can be easily localized.     More information is in<a class="TutorialLink" target="_top" href="../../i18n/index.html">Internationalization</a>.</blockquote><h3>An Example of Custom Painting</h3><blockquote>The following application gives an example of custom painting.It features a custom component called<code>IconDisplayer</code>that paints an icon multiple times,with all but the rightmost icon transparent.<p><center><IMG SRC="../../figures/uiswing/painting/IconDisplayer.gif" WIDTH="465" HEIGHT="105" ALIGN="BOTTOM" ALT="4 faded rocket ships follow Duke's rocket ship"></center></p>You can<b><a href=http://java.sun.com/docs/books/tutorialJWS/uiswing/painting/examples/IconDisplayer.jnlp>run the IconDisplayerapplication</a></b>using <a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java<sup><font size=-2>TM</font></sup> Web Start</a>.    Or, to compile and run the example yourself,     consult the     <a href="examples/index.html#IconDisplayer">example index</a>.<p>The source code is in <a class="SourceLink" target="_blank" href="examples/IconDisplayer.java"><code>IconDisplayer.java</code></a>.Here are the main painting-related parts:<blockquote><pre>public class IconDisplayer extends JComponent {    ...    protected void paintComponent(Graphics g) {        if (isOpaque()) { //paint background            g.setColor(getBackground());            g.fillRect(0, 0, getWidth(), getHeight());        }        if (icon != null) {            ...            Graphics2D g2d = (Graphics2D)g.create();            while (<em>/* we're not done */</em>) {                <em>/* Paint an icon. */</em>            }            g2d.dispose(); //clean up        }    }    ...}</pre></blockquote><p>The first thing the <code>paintComponent</code> method does is checkwhether the <code>IconDisplayer</code> needs to be opaque and,if so, paint its background.If <code>IconDisplayer</code> werea subclass of something other than <code>JComponent</code>,we might omit this codeand just call <code>super.paintComponent</code>.<p>The second part of the <code>paintComponent</code> methodpaints the main part of the component.It creates a copy of the <code>Graphics</code> objectit was handedand casts it into a <code>Graphics2D</code> object.The typecast lets <code>paintComponent</code> use 2D features such as alpha compositing;the copy lets it avoid making changes to the passed-in <code>Graphics</code> object,which would have to be undone before returning.<p>Here's a complete listing of the code that paints the icon repeatedly:<blockquote><pre>if (icon != null) {    Insets insets = getInsets();    int iconWidth = icon.getIconWidth();    int iconX = getWidth() - insets.right                           - iconWidth;    int iconY = insets.top;    boolean faded = false;        Graphics2D g2d = (Graphics2D)g.create();    g.getClipBounds(clipRect);    while (iconX >= insets.left) {	iconRect.setBounds(iconX, iconY, iconWidth,			   icon.getIconHeight());	if (iconRect.intersects(clipRect)) {            icon.paintIcon(this, g2d, iconX, iconY);	}        iconX -= (iconWidth + pad);        if (!faded) {             g2d.setComposite(AlphaComposite.getInstance(                AlphaComposite.SRC_OVER, 0.1f));            faded = true;        }    }    g2d.dispose(); //clean up}</pre></blockquote><p>The first thing the code does is prepare for painting icons.It gets its border size, using the <code>getInsets</code> method.It then uses the insets,along with the component and icon sizes,when calculating where the first icon &#151;the rightmost one &#151;should be painted.The icon is an <code>ImageIcon</code>,so it's easy to get its size using methodssuch as <code>getIconWidth</code>,and it can paint itself using the <code>Icon</code>-definedmethod <code>paintIcon</code>.The code next gets a <code>Graphics2D</code> objectso that it can use alpha compositing when painting some of the icons.<p>Next comes a bit of performance tuning.The code gets the <em>clipping area</em>, using the <code>Graphics</code> method<code>getClipBounds</code>.The clipping area is the part of the componentthat needs to be repainted.For example, if a window covering the right half of the component goes away,then only the right half of the component needs to be repainted.After getting the clipping area,the code checks whether this clipping area intersects with the icon's current painting area.If so, it paints the icon.If not, it saves a little time by not painting the icon.<p>After dealing with the first (rightmost) occurrence of the icon, the code invokes <code>setComposite</code>on the <code>Graphics2D</code> object,specifying parameters that make the subsequently painted iconsappear to be only 10% opaque.You can find information on using alpha compositing in<a class="TutorialLink" target="_top" href="../../2d/advanced/compositing.html">Compositing Graphics</a>,a section in the<a class="TutorialLink" target="_top" href="../../2d/index.html">2D Graphics</a> trail.<p>After the <code>paintComponent</code> methodpaints all the icons, there's nothing for it to do but clean up.In this case,the cleanup is as simple asdisposing of the <code>Graphics2D</code> objectthe method created.        </blockquote>        <div class=NavBit>            <a target=_top href=concepts2.html>&laquo; Previous</a>            &bull;            <a target=_top href=../TOC.html>Trail</a>            &bull;            <a target=_top href=summary.html>Next &raquo;</a>        </div>    </div>    <div id=Footer><div id=TagNotes>    Problems with the examples? Try <a target="_blank"        href=../../information/run-examples.html>Compiling and Running        the Examples: FAQs</a>.    <br>    Complaints? Compliments? Suggestions? <a target="_blank"        href="http://developer.sun.com/contact/tutorial_feedback.jsp">Give    us your feedback</a>.<br><br>    <a target="_blank" href="../../information/copyright.html">Copyright</a>    1995-2006 Sun Microsystems, Inc.  All rights reserved.    <span id=Download></span></div>     </div>    <div class=PrintHeaders>        <b>Previous page:</b> Introduction to Painting Concepts        <br><b>Next page:</b> Summary    </div>    </body></html> 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -