Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Jpanel Dashed Border?

Reply
Thread Tools

Jpanel Dashed Border?

 
 
Barkster
Guest
Posts: n/a
 
      06-27-2006
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

 
Reply With Quote
 
 
 
 
Thomas Fritsch
Guest
Posts: n/a
 
      06-27-2006
Barkster wrote:
> 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/...er/Border.html
http://java.sun.com/j2se/1.4.2/docs/...actBorder.html

--
Thomas
 
Reply With Quote
 
 
 
 
Vova Reznik
Guest
Posts: n/a
 
      06-27-2006
Barkster wrote:
> 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
>


http://java.sun.com/docs/books/tutor...sc/border.html
 
Reply With Quote
 
Barkster
Guest
Posts: n/a
 
      06-27-2006
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.

Vova Reznik wrote:
> Barkster wrote:
> > 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
> >

>
> http://java.sun.com/docs/books/tutor...sc/border.html


 
Reply With Quote
 
Vova Reznik
Guest
Posts: n/a
 
      06-27-2006
Barkster wrote:
> 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.
>
> Vova Reznik wrote:
>> Barkster wrote:
>>> 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
>>>

>> http://java.sun.com/docs/books/tutor...sc/border.html

>


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;
}
}
 
Reply With Quote
 
Andrey Kuznetsov
Guest
Posts: n/a
 
      06-27-2006
> 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
--
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


 
Reply With Quote
 
Barkster
Guest
Posts: n/a
 
      06-27-2006
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();
}
}

 
Reply With Quote
 
Vova Reznik
Guest
Posts: n/a
 
      06-27-2006
Andrey Kuznetsov wrote:
>> 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);
> g2d.dispose();
> }
> }
>
> Andrey

 
Reply With Quote
 
Barkster
Guest
Posts: n/a
 
      06-27-2006
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();
}
}

 
Reply With Quote
 
Vova Reznik
Guest
Posts: n/a
 
      06-27-2006
Barkster wrote:
> 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.


>
> 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();
> }
> }
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JPanel inside JPanel inside JFrame t1m1976 Java 1 11-07-2010 09:47 PM
[JPanel] Drop Down JPanel Raphael Jolivet Java 1 07-18-2008 12:30 AM
Add a JPanel to a JPanel... Andreas Beresko Java 6 07-16-2004 12:10 AM
how to draw a dashed line between two points Rick Java 3 09-30-2003 07:36 PM
annoying IE problem with dashed CSS borders TheKeith HTML 15 06-30-2003 07:17 AM



Advertisments