Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Swing Chapter 3. (The basics) Frames, Panels, and Borders. Easy for reading, Click here!

Swing Chapter 3. (The basics) Frames, Panels, and Borders. Easy for reading, Click here!

[ Return to Swing (Book) ]

Page: 3/3 


Previous Page Previous Page (2/3)
Subpages: 1. Frames and Panels overview
2. Borders
3. Creating a custom border

3.3 Creating a custom border

To create a custom border we can implement the javax.swing.Border interface and define the following three methods:

void paintBorder(Component c, Graphics g): perform the border rendering--only paint within the Insets region.

Insets getBorderInsets(Component c): return an Insets instance representing the top, bottom, left, and right thicknesses.

boolean isBorderOpaque(): return whether or not the border is opaque or transparent.

The following class is a simple implementation of a custom rounded-rectangle border which we call OvalBorder.

Figure 3.9 A custom rounded-corner Border implementation.

<<file figure3-9.gif>>

The Code: OvalBorder.java

see \Chapter3\2

import java.awt.*;

import javax.swing.*;

import javax.swing.border.*;

public class OvalBorder implements Border

{

  protected int m_w=6;

  protected int m_h=6;

  protected Color m_topColor = Color.white;

  protected Color m_bottomColor = Color.gray;

  public OvalBorder() {

    m_w=6;

    m_h=6;

  }

  public OvalBorder(int w, int h) {

    m_w=w;

    m_h=h;

  }

  public OvalBorder(int w, int h, Color topColor,

   Color bottomColor) {

    m_w=w;

    m_h=h;

    m_topColor = topColor;

    m_bottomColor = bottomColor;

  }

  public Insets getBorderInsets(Component c) {

    return new Insets(m_h, m_w, m_h, m_w);

  }

  public  boolean isBorderOpaque() { return true; }

  public void paintBorder(Component c, Graphics g,

   int x, int y, int w, int h) {

    w--;

    h--;

    g.setColor(m_topColor);

    g.drawLine(x, y+h-m_h, x, y+m_h);

    g.drawArc(x, y, 2*m_w, 2*m_h, 180, -90);

    g.drawLine(x+m_w, y, x+w-m_w, y);

    g.drawArc(x+w-2*m_w, y, 2*m_w, 2*m_h, 90, -90);

    g.setColor(m_bottomColor);

    g.drawLine(x+w, y+m_h, x+w, y+h-m_h);

    g.drawArc(x+w-2*m_w, y+h-2*m_h, 2*m_w, 2*m_h, 0, -90);

    g.drawLine(x+m_w, y+h, x+w-m_w, y+h);

    g.drawArc(x, y+h-2*m_h, 2*m_w, 2*m_h, -90, -90);

  }

  public static void main(String[] args) {

    JFrame frame = new JFrame("Custom Border: OvalBorder");

    JLabel label = new JLabel("OvalBorder");

    ((JPanel) frame.getContentPane()).setBorder(new CompoundBorder(

      new EmptyBorder(10,10,10,10), new OvalBorder(10,10)));

    frame.getContentPane().add(label);

    frame.setBounds(0,0,300,150);

    frame.setVisible(true);

  }

}

Understanding the Code

This border consists of a raised shadowed rectangle with rounded corners. Instance variables:

int m_w: left and right inset value.

int m_h: top and bottom inset value.

Color m_topColor: non-shadow color.

Color m_bottomColor: shadow color.

Three constructors are provided to allow optional specification of the width and height of left/right and top/bottom inset values respectively. We can also specify the shadow color (bottom color) and non-shadow color (top color). The inset values default to 6, the top color defaults to white, and the shadow color defaults to gray.

The isBorderOpaque() method always returns true to signify that this border's region will always be completely filled. getBorderInsests() simpley returns an Insets instance made up of the left/right and top/bottom inset values specified in a constructor.

The paintBorder() method is responsible of rendering our border and simply paints a sequence of four lines and arcs in the approproate colors. By simply reversing the use of bottomColor and topColor we can switch from a raised look to a lowered look (a more flexible implementation might include a raised/lowered flag, and an additional constructor parameter used to specify this).

The main() method creates a JFrame with content pane surrounded by a CompoundBorder. The outer border is an EmptyBorder to provide white space, and the inner border is an instance of our OvalBorder class with inset values of 10.

Running the Code

Figure 3.9 illustrates. Try running this class and resize the parent frame. Note that with a very small width or height the border does not render itself pleasantly. A more professional implementation would take this into account in the paintBorder() routine.



[ Return to Swing (Book) ]


Search Books
Custom Search
Latest articles
 SSL with GlassFish v2, page 5

 SSL with GlassFish v2, page 4

 SSL with GlassFish v2, page 3

 SSL with GlassFish v2, page 2

 The Java Lesson 2: Anatomy of a simple Java program, page 2

 New site about Java for robots and robotics: both software and hardware.

 Exceptions -III: What's an exception and why do I care?

 Exceptions -II: What's an exception and why do I care?

 Exceptions: What's an exception and why do I care?

 Double your Java code quality in 10 minutes, here is receipt

 Murach's Java Servlets and JSP

 How to get ascii code from a char in Java?

 Can we just try without catch? Yes!

 Make Tomcat page load faster

 Make your Tomcat More secure - limit network address for certain IP addresses

 New Java book online starts now here...

 Implementing RESTful Web Services in Java

 Firefox trimming from 1 GB to 40 Mb with many tabs opened

 SSL with GlassFish v2

 My request to replublish Tech Tips

 Search JavaFAQ.nu site here

 New Advanced Installer for Java 6.0 brings XML updates and imports 3rd party MSI

 EJB programming restrictions

 Maven vs Ant or Ant vs Maven?

 Why Java does not use default value which it should?

 How to unsign signed bytes in Java - your guide is here

 The Java Lesson 3: Identifiers and primitive data types. Page 2

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 4

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 3

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 2


[ More in News Section ]


Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy