Jpanel Dashed Border?

B

Barkster

Is there a way to make a border dashed around a panel? I'm sure it
could be painted somehow but I have no clue on that. Any examples on
how to do this? Thanks
 
T

Thomas Fritsch

Barkster said:
Is there a way to make a border dashed around a panel? I'm sure it
could be painted somehow but I have no clue on that. Any examples on
how to do this? Thanks
You could implement your own Border implementation, probably as a subclass
of AbstractBorder. At least you need to override the getBorderInsets(...)
and paintBorder(...) methods. Then set that border onto your JPanel by
setBorder(...).
See
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/border/Border.html
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/border/AbstractBorder.html
 
B

Barkster

Thanks, those sound logical but I'm new to this and that is a bit over
my head, neither actually showed dashed border example. May be in over
my head on this one.
 
V

Vova Reznik

Barkster said:
Thanks, those sound logical but I'm new to this and that is a bit over
my head, neither actually showed dashed border example. May be in over
my head on this one.

class DashBorder implements Border {
private final Insets insets = new Insets(1, 1, 1, 1);
private final int length = 5;
private final int space = 3;
public boolean isBorderOpaque() {
return false;
}
public void paintBorder(Component c, Graphics g, int x, int y,
int width, int height) {
g.setColor(Color.RED);
// --- draw horizontal ---
for (int i = 0; i < width; i += length) {
g.drawLine(i, y, i + length, y);
g.drawLine(i, height - 1, i + length, height - 1);
i += space;
}
// --- draw vertical ---
for (int i = 0; i < height; i += length) {
g.drawLine(0, i, 0, i + length);
g.drawLine(width - 1, i, width - 1, i + length);
i += space;
}
}
public Insets getBorderInsets(Component c) {
return insets;
}
}
 
A

Andrey Kuznetsov

public void paintBorder(Component c, Graphics g, int x, int y,
int width, int height) {
g.setColor(Color.RED);
// --- draw horizontal ---
for (int i = 0; i < width; i += length) {
g.drawLine(i, y, i + length, y);
g.drawLine(i, height - 1, i + length, height - 1);
i += space;
}
// --- draw vertical ---
for (int i = 0; i < height; i += length) {
g.drawLine(0, i, 0, i + length);
g.drawLine(width - 1, i, width - 1, i + length);
i += space;
}
}

why ignore java2D and existing classes?

class DashBorder extends LineBorder {

//make getters and setters for stroke as exercise ;-)
BasicStroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL, 1, new float[]{5, 5}, 0);

public void paintBorder(Component c, Graphics g, int x, int y, int
width, int height) {
Graphics2D g2d = (g2d)g.create();
g2d.setStroke(stroke);
super.paintBorder(c, g, x, y, width, height);
g2d.dispose();
}
}

Andrey
 
B

Barkster

Hmmn, I'll look at those, I got this to work but I want to be able to
change from dashed back to solid and dashed again using a checkbox. I
can get it to go dashed but I can't figure out how to apply this class
again the the panel pn? Here is simplified version of what I'm trying
to do?



//declare new dashed panel
DashedBorderPanel pn = new DashedBorderPanel();

//watch checkbox
public void jCheckBox1_actionPerformed(ActionEvent e) {
if(this.jCheckBox1.isSelected()) {
pn.setBorder(BorderFactory.createLineBorder(Color.black));
System.out.println("Checked");
}else {
pn.??????
System.out.println("UnChecked");
}
}



import javax.swing.JPanel;
import java.awt.LayoutManager;
import java.awt.Color;
import java.awt.*;
import javax.swing.border.*;

public class DashedBorderPanel extends JPanel {

class DashedBorder extends LineBorder {

public DashedBorder(Color p0) {
super(p0);
}

public DashedBorder(Color p0, int p1) {
super(p0, p1);
}

public DashedBorder(Color p0, int p1, boolean p2) {
super(p0, p1, p2);
}

public void paintBorder(Component comp,Graphics g, int x1,int x2,
int y1,int y2){
Stroke old=((Graphics2D)g).getStroke();
BasicStroke bs=new BasicStroke(5.0f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 10.0f, new
float[]{8.0f}, 2.0f);
((Graphics2D)g).setStroke(bs);
super.paintBorder(comp,g, x1,x2, y1,y2);
((Graphics2D)g).setStroke(old);
}



}
public DashedBorderPanel() {
DashedBorder db=new DashedBorder(Color.black);
this.setBorder(db);
}

public DashedBorderPanel(LayoutManager p0, boolean p1) {
super(p0, p1);
}

public DashedBorderPanel(LayoutManager p0) {
super(p0);
}

public DashedBorderPanel(boolean p0) {
super(p0);
}
public static void main(String[] args) {
DashedBorderPanel dashedBorderPanel1 = new DashedBorderPanel();
}
}
 
V

Vova Reznik

Andrey said:
public void paintBorder(Component c, Graphics g, int x, int y,
int width, int height) {
g.setColor(Color.RED);
// --- draw horizontal ---
for (int i = 0; i < width; i += length) {
g.drawLine(i, y, i + length, y);
g.drawLine(i, height - 1, i + length, height - 1);
i += space;
}
// --- draw vertical ---
for (int i = 0; i < height; i += length) {
g.drawLine(0, i, 0, i + length);
g.drawLine(width - 1, i, width - 1, i + length);
i += space;
}
}

why ignore java2D and existing classes?

class DashBorder extends LineBorder {

//make getters and setters for stroke as exercise ;-)
BasicStroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL, 1, new float[]{5, 5}, 0);

public void paintBorder(Component c, Graphics g, int x, int y, int
width, int height) {

Good idea if:
Graphics2D g2d = (g2d)g.create();
Graphics2D g2d = (Graphics2D)g.create();
g2d.setStroke(stroke);
super.paintBorder(c, g, x, y, width, height);
super.paintBorder(c, g2d, x, y, width, height);
 
B

Barkster

Sorry for my ignorance but I having a problem implementing this
solution. I get an error:
"LineBorder(java.awt.Color,int,int) in com.sun.javaws.ui.general
LineBorder cannot be applied to ()" I'm pretty new to programming and
to java and have quite developed an understanding of implementing the
classes.

class DashBorder extends LineBorder {
//make getters and setters for stroke as exercise ;-)
BasicStroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL, 1, new
float[]{5, 5}, 0);

public void paintBorder(Component c, Graphics g, int x, int y, int
width, int height) {
Graphics2D g2d = (Graphics2D)g.create();
g2d.setStroke(stroke);
super.paintBorder(c, g2d, x, y, width, height);
g2d.dispose();
}
}
 
V

Vova Reznik

Barkster said:
Sorry for my ignorance but I having a problem implementing this
solution. I get an error:
"LineBorder(java.awt.Color,int,int) in com.sun.javaws.ui.general
LineBorder cannot be applied to ()" I'm pretty new to programming and
to java and have quite developed an understanding of implementing the
classes.

That is why I offered implementation of Border, not LineBorder
class DashBorder extends LineBorder {
//make getters and setters for stroke as exercise ;-)
BasicStroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL, 1, new
float[]{5, 5}, 0);

You may, for example:
DashBorder (){
super(Color.RED);

}

LineBorder has no empty constructor.
But it has:
LineBorder(Color)
LineBorder(Color, int thickness)
LineBorder(Color, int thickness, boolean rounded)

You need to call super constructor when you
extending a class.
 
V

Vova Reznik

Barkster said:
Sorry for my ignorance but I having a problem implementing this
solution. I get an error:
"LineBorder(java.awt.Color,int,int) in com.sun.javaws.ui.general
LineBorder cannot be applied to ()" I'm pretty new to programming and
to java and have quite developed an understanding of implementing the
classes.

class DashBorder extends LineBorder {
//make getters and setters for stroke as exercise ;-)
BasicStroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL, 1, new
float[]{5, 5}, 0);

public DashBorder(){
super(Color.RED);
}


You may read docs yourself.
 
A

Andrey Kuznetsov

public void paintBorder(Component c, Graphics g, int x, int y, int
Good idea if:

Graphics2D g2d = (Graphics2D)g.create();

yes, of course,
I just tipped it direct in outlook
and 37.8 fever is not the best condition for progamming ;-)

Andrey
 
A

Andrey Kuznetsov

Sorry for my ignorance but I having a problem implementing this
solution. I get an error:

this should work and gives you possibility to switch between solid and
dashed border.
I hope this is not your homework.

public class DashBorder extends LineBorder {

public DashBorder(Color c) {
this(c, 1);
}

public DashBorder(Color c, int thickness) {
this(c, thickness, new float[] {5, 5});
}

public DashBorder(Color c, int thickness, float [] dash) {
super(c, thickness);
this.stroke = new BasicStroke(thickness, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL, 1, dash, 0);
}

private boolean solid;
private BasicStroke stroke;

public void paintBorder(Component c, Graphics g, int x, int y, int
width, int height) {
if(!solid) {
Graphics2D g2d = (Graphics2D)g.create();
g2d.setStroke(stroke);
super.paintBorder(c, g2d, x, y, width, height);
g2d.dispose();
}
else {
super.paintBorder(c, g, x, y, width, height);
}
}
}

Andrey
 
B

Barkster

Thanks Andrey, I'll see if I can get it implemented

Andrey said:
Sorry for my ignorance but I having a problem implementing this
solution. I get an error:

this should work and gives you possibility to switch between solid and
dashed border.
I hope this is not your homework.

public class DashBorder extends LineBorder {

public DashBorder(Color c) {
this(c, 1);
}

public DashBorder(Color c, int thickness) {
this(c, thickness, new float[] {5, 5});
}

public DashBorder(Color c, int thickness, float [] dash) {
super(c, thickness);
this.stroke = new BasicStroke(thickness, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL, 1, dash, 0);
}

private boolean solid;
private BasicStroke stroke;

public void paintBorder(Component c, Graphics g, int x, int y, int
width, int height) {
if(!solid) {
Graphics2D g2d = (Graphics2D)g.create();
g2d.setStroke(stroke);
super.paintBorder(c, g2d, x, y, width, height);
g2d.dispose();
}
else {
super.paintBorder(c, g, x, y, width, height);
}
}
}

Andrey

--
Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities
 
B

Barkster

Hello Andrey, I got the class implemented finally but I'm having a hard
time changing the border. From what I see when paint is called it
checks to see if it is solid else it paints dashed? But when I run it
and change the border to something other than solid it just changes to
that. How do I swap back to dashed? Here is what I'm using

if(this.jCheckBox1.isSelected()) {
pn.setBorder(BorderFactory.createLineBorder(Color.black));
System.out.println("Checked");
}else {
pn.setBorder(BorderFactory.createEtchedBorder());
System.out.println("UnChecked");
}

when I debug the paint in dahedborder class

if (!solid) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setStroke(stroke);
super.paintBorder(c, g2d, x, y, width, height);
g2d.dispose();
System.out.println("Not Solid");
} else {
super.paintBorder(c, g, x, y, width, height);
System.out.println("Solid");
}

it only prints "Not Solid" on load after than when I change the
checkbox it only prints Check/Unchecked? Any help would be
appreciated, I'm struggling with learning java.

Thank said:
Thanks Andrey, I'll see if I can get it implemented

Andrey said:
Sorry for my ignorance but I having a problem implementing this
solution. I get an error:

this should work and gives you possibility to switch between solid and
dashed border.
I hope this is not your homework.

public class DashBorder extends LineBorder {

public DashBorder(Color c) {
this(c, 1);
}

public DashBorder(Color c, int thickness) {
this(c, thickness, new float[] {5, 5});
}

public DashBorder(Color c, int thickness, float [] dash) {
super(c, thickness);
this.stroke = new BasicStroke(thickness, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL, 1, dash, 0);
}

private boolean solid;
private BasicStroke stroke;

public void paintBorder(Component c, Graphics g, int x, int y, int
width, int height) {
if(!solid) {
Graphics2D g2d = (Graphics2D)g.create();
g2d.setStroke(stroke);
super.paintBorder(c, g2d, x, y, width, height);
g2d.dispose();
}
else {
super.paintBorder(c, g, x, y, width, height);
}
}
}

Andrey

--
Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities
 
A

Andrey Kuznetsov

Hello Andrey, I got the class implemented finally but I'm having a hard
time changing the border. From what I see when paint is called it
checks to see if it is solid else it paints dashed? But when I run it
and change the border to something other than solid it just changes to
that. How do I swap back to dashed? Here is what I'm using

if(this.jCheckBox1.isSelected()) {
pn.setBorder(BorderFactory.createLineBorder(Color.black));
System.out.println("Checked");
}else {
pn.setBorder(BorderFactory.createEtchedBorder());
System.out.println("UnChecked");
}

when I debug the paint in dahedborder class

if (!solid) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setStroke(stroke);
super.paintBorder(c, g2d, x, y, width, height);
g2d.dispose();
System.out.println("Not Solid");
} else {
super.paintBorder(c, g, x, y, width, height);
System.out.println("Solid");
}

it only prints "Not Solid" on load after than when I change the
checkbox it only prints Check/Unchecked? Any help would be
appreciated, I'm struggling with learning java.

you should really learn java first, but ok.
I told you already that you have to implement getter and setter for solid by
yourself.
this is pretty simple thing:

class DashedBorder {


.....

//getter:
public boolean isSolid() {
return solid;
}

//setter:
public void setSolid(boolean b) {
this.solid = b;
}
}

now you can use setter in your checkbox handler:

JCheckBox dashBox = new JCheckBox("Dashed Border");
dashBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dashBorder.setSolid(!dashBox.isSelected());
}
}

HTH

Andrey
 
B

Barkster

Thank Andrey, I think I getting through it ok for a beginner just not
used to most of the terminology and methodology. I appreciate the
help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top