Skip to content Skip to sidebar Skip to footer

Drawing Lines Arounda Circle Java

Every JComponent can have 1 or more borders. Borders are incredibly useful objects that, while non themselves components, know how to draw the edges of Swing components. Borders are useful non only for drawing lines and fancy edges, but also for providing titles and empty infinite around components.


Note:

Our examples set borders on JPanels, JLabelsouth, and custom subclasses of JComponent. Although technically yous can set the border on whatever object that inherits from JComponent, the expect and experience implementation of many standard Swing components doesn't work well with user-fix borders. In general, when you lot desire to set a border on a standard Swing component other than JPanel or JLabel, we recommend that yous put the component in a JPanel and gear up the edge on the JPanel.


To put a edge effectually a JComponent, you employ its setBorder method. You can use the BorderFactory form to create most of the borders that Swing provides. If you need a reference to a border — say, considering you want to apply it in multiple components — you can save it in a variable of blazon Edge. Hither is an example of lawmaking that creates a bordered container:

JPanel pane = new JPanel(); pane.setBorder(BorderFactory.createLineBorder(Color.black));          

Here's a flick of the container, which contains a label component. The black line drawn by the edge marks the edge of the container.

A line border

The rest of this page discusses the following topics:

  • The BorderDemo Example
  • Using the Borders Provided by Swing
  • Creating Custom Borders
  • The Edge API
  • Examples of Using Borders

The BorderDemo Example

The following pictures show an application called BorderDemo that displays the borders Swing provides. We bear witness the code for creating these borders a little afterwards, in Using the Borders Provided by Swing.

Click the Launch push button to run the BorderDemo instance using Java™ Spider web Starting time (download JDK 7 or later). Alternatively, to compile and run the case yourself, consult the example alphabetize.

Launches the BorderDemo example

BorderDemo: Simple Borders

The next picture shows some matte borders. When creating a matte border, you specify how many pixels it occupies at the tiptop, left, bottom, and right of a component. You lot then specify either a color or an icon for the matte border to depict. You demand to be careful when choosing the icon and determining your component's size; otherwise, the icon might become chopped off or have mismatch at the component's corners.

BorderDemo: Matte Borders

The adjacent picture shows titled borders. Using a titled edge, you tin can convert any border into one that displays a text description. If y'all don't specify a edge, a wait-and-experience-specific edge is used. For example, the default titled border in the Java look and experience uses a gray line, and the default titled border in the Windows look and experience uses an etched border. By default, the title straddles the upper left of the border, every bit shown at the height of the following figure.

BorderDemo: Titled Borders

The next picture shows compound borders. With compound borders, you can combine whatever 2 borders, which can themselves be chemical compound borders.

BorderDemo: Compound Borders

Using the Borders Provided by Swing

The code that follows shows how to create and set the borders yous saw in the preceding figures. Y'all tin can discover the plan'due south code in BorderDemo.java.

//Keep references to the next few borders, //for employ in titles and compound borders. Border blackline, raisedetched, loweredetched,        raisedbevel, loweredbevel, empty;  blackline = BorderFactory.createLineBorder(Color.black); raisedetched = BorderFactory.createEtchedBorder(EtchedBorder.RAISED); loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED); raisedbevel = BorderFactory.createRaisedBevelBorder(); loweredbevel = BorderFactory.createLoweredBevelBorder(); empty = BorderFactory.createEmptyBorder();  //Simple borders jComp1.setBorder(blackline); jComp2.setBorder(raisedbevel); jComp3.setBorder(loweredbevel); jComp4.setBorder(empty);  //Matte borders ImageIcon icon = createImageIcon("images/wavy.gif",                                  "wavy-line border icon"); //20x22  jComp5.setBorder(BorderFactory.createMatteBorder(                                    -1, -1, -1, -1, icon)); jComp6.setBorder(BorderFactory.createMatteBorder(                                     1, 5, i, one, Color.red)); jComp7.setBorder(BorderFactory.createMatteBorder(                                     0, twenty, 0, 0, icon));  //Titled borders TitledBorder championship; title = BorderFactory.createTitledBorder("title"); jComp8.setBorder(title);  title = BorderFactory.createTitledBorder(                        blackline, "championship"); title.setTitleJustification(TitledBorder.CENTER); jComp9.setBorder(title);  title = BorderFactory.createTitledBorder(                        loweredetched, "title"); title.setTitleJustification(TitledBorder.Right); jComp10.setBorder(championship);  title = BorderFactory.createTitledBorder(                        loweredbevel, "title"); title.setTitlePosition(TitledBorder.ABOVE_TOP); jComp11.setBorder(title);  championship = BorderFactory.createTitledBorder(                        empty, "title"); title.setTitlePosition(TitledBorder.Lesser); jComp12.setBorder(title);  //Compound borders Border chemical compound; Border redline = BorderFactory.createLineBorder(Color.red);  //This creates a overnice frame. compound = BorderFactory.createCompoundBorder(                           raisedbevel, loweredbevel); jComp13.setBorder(compound);  //Add a scarlet outline to the frame. compound = BorderFactory.createCompoundBorder(                           redline, compound); jComp14.setBorder(compound);  //Add a title to the red-outlined frame. compound = BorderFactory.createTitledBorder(                           chemical compound, "title",                           TitledBorder.CENTER,                           TitledBorder.BELOW_BOTTOM); jComp15.setBorder(compound);          

Every bit yous probably noticed, the code uses the BorderFactory form to create each edge. The BorderFactory class, which is in the javax.swing package, returns objects that implement the Border interface.

The Edge interface, too as its Swing-provided implementations, is in the javax.swing.border package. You lot often don't need to direct apply anything in the edge package, except when specifying constants that are specific to a item border grade or when referring to the Border type.

Creating Custom Borders

If BorderFactory doesn't offering you enough control over a edge's form, then y'all might need to directly use the API in the edge package — or fifty-fifty define your own edge. In addition to containing the Border interface, the border package contains the classes that implement the borders you lot've already seen: LineBorder, EtchedBorder, BevelBorder, EmptyBorder, MatteBorder, TitledBorder, and CompoundBorder. The edge bundle also contains a grade named SoftBevelBorder, which produces a outcome like to BevelBorder, but with softer edges.

If none of the Swing borders is suitable, you tin implement your ain edge. Generally, you do this by creating a subclass of the AbstractBorder class. In your subclass, y'all must implement at to the lowest degree one constructor and the following 2 methods:

  • paintBorder, which contains the cartoon code that a JComponent executes to draw the edge.
  • getBorderInsets, which specifies the amount of infinite the border needs to draw itself.

If a custom border has insets (and they typically accept insets) yous need to override both AbstractBorder.getBorderInsets(Component c) and AbstractBorder.getBorderInsets(Component c, Insets insets) to provide the correct insets.

For examples of implementing borders, see the source code for the classes in the javax.swing.border parcel.

The Edge API

The following tables listing the commonly used border methods. The API for using borders falls into 2 categories:

  • Creating a Border with BorderFactory
  • Setting or Getting a Component's Border
Creating a Edge with BorderFactory
Method Purpose
Border createLineBorder(Color)
Edge createLineBorder(Color, int)
Create a line border. The beginning argument is a java.awt.Color object that specifies the color of the line. The optional 2d argument specifies the width in pixels of the line.
Border createEtchedBorder()
Border createEtchedBorder(Color, Color)
Border createEtchedBorder(int)
Border createEtchedBorder(int, Color, Color)
Create an etched edge. The optional Color arguments specify the highlight and shadow colors to be used. The methods with int arguments allow the border methods to be specified every bit either EtchedBorder.RAISED or EtchedBorder.LOWERED. The methods without the int arguments create a lowered etched border.
Border createLoweredBevelBorder() Create a border that gives the illusion of the component existence lower than the surrounding area.
Border createRaisedBevelBorder() Create a border that gives the illusion of the component being higher than the surrounding surface area.

Edge createBevelBorder(int, Colour, Color)
Border createBevelBorder(int, Colour, Colour, Color, Color)
Create a raised or lowered beveled border, specifying the colors to utilise. The integer argument can exist either BevelBorder.RAISED or BevelBorder.LOWERED. With the three-argument constructor, you specify the highlight and shadow colors. With the five-argument constructor, you specify the outer highlight, inner highlight, outer shadow, and inner shadow colors, in that order.
Border createEmptyBorder()
Border createEmptyBorder(int, int, int, int)
Create an invisible edge. If you specify no arguments, then the border takes no space, which is useful when creating a titled border with no visible purlieus. The optional arguments specify the number of pixels that the edge occupies at the top, left, bottom, and right (in that order) of whatever component uses it. This method is useful for putting empty space around your components.
MatteBorder createMatteBorder(int, int, int, int, Colour)
MatteBorder createMatteBorder(int, int, int, int, Icon)
Create a matte border. The integer arguments specify the number of pixels that the border occupies at the top, left, bottom, and right (in that order) of whatsoever component uses it. The color statement specifies the color which with the border should fill its area. The icon argument specifies the icon which with the border should tile its area.
TitledBorder createTitledBorder(String)
TitledBorder createTitledBorder(Border)
TitledBorder createTitledBorder(Border, String)
TitledBorder createTitledBorder(Border, String, int, int)
TitledBorder createTitledBorder(Edge, String, int, int, Font)
TitledBorder createTitledBorder(Edge, String, int, int, Font, Colour)
Create a titled border. The string argument specifies the title to be displayed. The optional font and color arguments specify the font and colour to be used for the title's text. The border argument specifies the edge that should exist displayed forth with the title. If no edge is specified, then a await-and-feel-specific default border is used.

By default, the title straddles the top of its companion edge and is left-justified. The optional integer arguments specify the title'southward position and justification, in that social club. TitledBorder defines these possible positions: ABOVE_TOP, TOP (the default), BELOW_TOP, ABOVE_BOTTOM, Bottom, and BELOW_BOTTOM. You tin specify the justification as LEADING (the default), CENTER, or Abaft. In locales with Western alphabets LEADING is equivalent to LEFT and Trailing is equivalent to Correct.


CompoundBorder createCompoundBorder(Border, Border)
Combine two borders into one. The commencement statement specifies the outer border; the second, the inner border.
Setting or Getting a Component'southward Border
Method Purpose
void setBorder(Border)
Border getBorder()
Set up or become the border of the receiving JComponent.
void setBorderPainted(boolean)
boolean isBorderPainted()
(in AbstractButton, JMenuBar, JPopupMenu, JProgressBar, and JToolBar)
Prepare or go whether the edge of the component should be displayed.

Examples that Use Borders

Many examples in this lesson use borders. The following table lists a few interesting cases.

Example Where Described Notes
BorderDemo This section Shows an example of each type of edge that BorderFactory can create. As well uses an empty border to add breathing space betwixt each pane and its contents.
BoxAlignmentDemo How to Use BoxLayout Uses titled borders.
BoxLayoutDemo How to Use BoxLayout Uses a reddish line to show where the border of a container is, so that you can see how the actress space in a box layout is distributed.
ComboBoxDemo2 How to Use Combo Boxes Uses a compound edge to combine a line border with an empty border. The empty edge provides infinite betwixt the line and the component's innards.

bratchersubmiliand.blogspot.com

Source: https://docs.oracle.com/javase/tutorial/uiswing/components/border.html

Post a Comment for "Drawing Lines Arounda Circle Java"