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!

Custom Search
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) ]


Top 10 read Java Articles
 Get free "1000 Java Tips eBook"

 Java Calendar and Date: good to know facts and code examples

 Array vs ArrayList vs LinkedList vs Vector: an excellent overview and examples

 How can I convert any Java Object into byte array? And byte array to file object

 The Java Lesson 1: What is Java?

 How do I compare two dates and times, date between dates, time between times and

 Maven vs Ant or Ant vs Maven?

 How to open, read, write, close file(s) in Java? Examples on move, rename and de

 Java Array

 Java: JLabel font and color


[ More in News Section ]
Java Lessons

The Java Lesson 1:
What is Java?
The Java Lesson 2:
Anatomy of a simple Java program
The Java Lesson 3:
Identifiers and primitive data types
The Java Lesson 4:
Variables, constants, and literals
The Java Lesson 5:
Arithmetic operations, conversions, and casts
The Java Lesson 6:
Boolean expressions and operations
The Java Lesson 7:
Bitwise operations
The Java Lesson 8:
Flow control with if and else
The Java Lesson 9:
switch statements
The Java Lesson 10:
for, while, and do-while statements
The Java Lesson 11:
Using break and continue
The Java Lesson 12:
Class methods and how they are called
The Java Lesson 13:
Using the Math class
The Java Lesson 14:
Creating and calling custom class methods
The Java Lesson 15:
Overloading class methods
The Java Lesson 16:
An introduction to objects and object references
The Java Lesson 17:
The String class
The Java Lesson 18:
The StringBuffer class
The Java Lesson 19:
Initializing and processing arrays of primitives
The Java Lesson 20:
Initializing and processing arrays of objects
The Java Lesson 23:
Inheritance and overriding inherited methods
The Java Lesson 24:
abstract classes and polymorphism
The Java Lesson 25:
Interfaces, instanceof, and object conversion and casting
The Java Lesson 26:
Introduction to graphical programming and the java.awt packa
The Java Lesson 27:
The Component class
The Java Lesson 28:
Containers and simple layout managers
The Java Lesson 29:
The Color and Font classes
The Java Lesson 30:
Drawing geometric shapes
The Java Lesson 31:
Choice, List, and Checkbox controls
The Java Lesson 32:
Using the Scrollbar graphical control
The Java Lesson 33:
Menus and submenus
The Java Lesson 34:
An introduction to applets and the Applet class
The Java Lesson 35:
Essential HTML to launch an applet and pass it parameters
The Java Lesson 36:
Mouse event processing
Java Lesson 37:
Menus and submenus
Java Lesson 38:
The WindowListener interface and the WindowAdapter class
Java Lesson 39:
An introduction to GridBagLayout
Java Lesson 40:
An introduction to the Java Collections API
Java Lesson 41:
Exception handling with try, catch, and finally blocks
Java Lesson 42:
Claiming and throwing exceptions
Java Lesson 43:
Multithreading, the Thread class, and the Runnable interface
Java Lesson 44:
An introduction to I/O and the File and FileDialog classes
Java Lesson 45:
Low-level and high-level stream classes
Java Lesson 46:
Using the RandomAccessFile class
Java Lessons by
Joh Huhtala: Update

Latest articles
 Java Profiler JProbe to Resolve Performance Problems Faster

 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


[ 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