Today we made a border class, which is very simple. It inherits from the border actborder. Generally, if we want to write our own border class, we will choose to inherit from this class in most cases.
The abstractborder class is the default implementation of the border interface. The border interface has three Abstract METHODS:
(1)
Public void paintborder (component C, int X, int y, int width, int height );
(2)
Public insets getborderinset (component C );
(3)
Public Abstract Boolean isborderopaque ()
Note that the P in the first method is in lower case, because this afternoon also missed some time.
If we write a border class, we generally need to override the first method.
Here is an example from my book:
Never draw borders in the component area. It is a golden rule for creating borders.
Public class wrongborder extends actborder {
Public wrongborder (){
}
Public void paintborder (component C, graphics g, int X, int y, int width, int height ){
G. setcolor (color. Black );
G. fillrect (X, Y, width, height); // bad
}
Public Boolean isborderopaque (){
Retrun true;
}
Public insets isborderinserts (componet c ){
Return new insets (20, 20, 20 );
}
}
It should be as follows:
Public void paintborder (componet C, graphic g, int X, int y, int width, int height ){
Insets = getborderinsets (C );
G. setcolor (color. Black );
G. fillrect (X, Y, width, insets. Top );
G. fillrect (X, Y, insets. Left, height );
G. fillrect (x + width-insets.right, Y, insets. Right, height );
G. fillrect (X, Y + height-insets.bottom, width, insets. Bottom );
}
This afternoon I did not do this: the border made is as follows:
Code:
Import javax. Swing. Border. abstractborder;
Import java. AWT .*;
Import java. AWT. image. bufferedimage;
Public class imageborder extends actborder {
Image top_center, top_left, top_right;
Image left_center, right_center;
Image bottom_left, bottom_center, bottom_right;
Insets;
Public imageborder (image top_left, image top_center, image top_right,
Image left_center, image right_center,
Image bottom_left, image bottom_center, image bottom_right ){
Super ();
This. top_center = top_center;
This. top_left = top_left;
This. top_right = top_right;
This. left_center = left_center;
This. right_center = right_center;
This. bottom_left = bottom_left;
This. bottom_center = bottom_center;
This. bottom_right = bottom_right;
}
Public void setinsets (insets ){
This. insets = insets;
}
Public insets getborderinsets (component C ){
If (insets! = NULL ){
Return insets;
} Else {
Return new insets (top_center.getheight (null), left_center.getwidth (null ),
Bottom_center.getheight (null), right_center.getwidth (null ));
}
}
Public bufferedimage createbufferedimage (image IMG ){
Bufferedimage buff = new bufferedimage (IMG. getwidth (null), IMG. getheight (null), bufferedimage. type_int_argb );
Graphics GFX = buff. creategraphics ();
GFX. drawimage (IMG, 0, 0, null );
GFX. Dispose ();
Return Buff;
}
Public void filltexture (graphics2d G2, image IMG, int X, int y, int W, int h ){
Bufferedimage buff = createbufferedimage (IMG );
Rectangle anchor = new rectangle (X, Y, IMG. getwidth (null), IMG. getheight (null ));
Texturepaint paint = new texturepaint (buff, anchor );
G2.setpaint (paint );
G2.fillrect (X, Y, W, H );
}
Public void paintborder (component C, graphics g, int X, int y, int width, int height ){
Insets = getborderinsets (C );
G. setcolor (color. Black );
G. fillrect (X, Y, width, height );
Graphics2d g2 = (graphics2d) g;
Int tlw = top_left.getwidth (null );
Int tlh = top_left.getheight (null );
Int TCW = top_center.getwidth (null );
Int tch = top_center.getheight (null );
Int TRW = top_right.getwidth (null );
Int TRH = top_right.getheight (null );
Int LCW = left_center.getwidth (null );
Int LCH = left_center.getheight (null );
Int RCW = right_center.getwidth (null );
Int RCH = right_center.getheight (null );
Int blw = bottom_left.getwidth (null );
Int bLH = bottom_left.getheight (null );
Int BCW = bottom_center.getwidth (null );
Int BCH = bottom_center.getheight (null );
Int BRW = bottom_right.getwidth (null );
Int BRH = bottom_right.getheight (null );
Filltexture (G2, top_left, X, Y, tlw, tlh );
Filltexture (G2, top_center, x + tlw, Y, width-tlw-TRW, TCH );
Filltexture (G2, top_right, x + width-TRW, Y, TRW, TRH );
Filltexture (G2, left_center, X, Y + tlh, LCW, height-tlh-bLH );
Filltexture (G2, right_center, x + width-RCW, Y + TRH, RCW, height-TRH-BRH );
Filltexture (G2, bottom_left, X, Y + height-bLH, blw, BLH );
Filltexture (G2, bottom_center, x + blw, Y + height-BCH, width-blw-BRW, BCH );
Filltexture (G2, bottom_right, x + width-BRW, Y + height-BRH, BRW, BRH );
}
}
Test code:
Import javax. Swing .*;
Public class testimageborder {
Public static void main (string [] ARGs ){
Jframe frame = new jframe ("imageborder ");
Jpanel Panel = new jpanel ();
Jbutton button = new jbutton ("test image border ");
Panel. Add (button );
Imageborder = new imageborder (
New imageicon ("images/top_left.png"). getimage (), new imageicon ("images/top_center.png"). getimage (),
New imageicon ("images/top_right.png"). getimage (), new imageicon ("images/left_center.png"). getimage (),
New imageicon ("images/right_center.png"). getimage (), new imageicon ("images/bottom_left.png"). getimage (),
New imageicon ("images/bottom_center.png"). getimage (), new imageicon ("images/bottom_right.png"). getimage ()
);
Panel. setborder (imageborder );
Frame. getcontentpane (). Add (panel );
Frame. Pack ();
Frame. setvisible (true );
}
}
Because I put the eight images in the wrong position, the IMG. getwidth () and IMG. getheight () Methods return "-1.
So the program keeps reporting errors. This is just because you have not understood the relative concept.
If we create an item in eclipse, for example, the project name is not csdn, then the above program uses
Where should I find the images folder?
Three options:
(1). csdn directory
(2). csdn/src directory
(3). csdn/bin
I always thought it was in Bin, because the compiled files are placed in this folder.
However, the answer is one. In the csdn directory.
This is why I was mistaken. I only blame myself for failing to understand this problem.
I think I am too stupid. The IMG. getwidth () method returns-1, but it is definitely not found in the IMG image. It must be a path problem, but I did not expect it at the time.