NumberC
NumberC

Reputation: 596

JComponent setSize() and setLocation() not working

I am trying to setup a container JComponent that draws a rectangle with a dashed border. The problem is that setLocation() and setSize() don't work. I've tried a few things, such as super.setSize() and super.setPrefferedSize().

For any context, I am adding this to a JPanel with a FlowLayout. The JFrame has a width of 500 and height of 300. This is being run on Linux, in case OS matters.

public class RectangleContainer extends JComponent{
int x = 0;
int y = 0;
int length = 50;
int dashLength = 10;

RectangleContainer(){
    super();
    setSize(60, 60);
    setPreferredSize(new Dimension(60, 60)); //length+dashLength
}

// @Override
// public void setSize(int width, int height){
//     this.length = (width > height) ? height:width;
//     super.setSize(length+dashLength, length+dashLength);
//     super.setPreferredSize(new Dimension(length+dashLength, length+dashLength));
//     revalidate();
// }

//TODO: add dashLength or not?
@Override
public Dimension getSize(){
    return new Dimension(length+dashLength, length+dashLength);
}

@Override
public void setLocation(int x, int y){
    super.setLocation(x, y);
    // System.out.println(String.format("X: %d", x));
    // System.out.println(String.format("Y: %d", y));

    // this.x = x;
    // this.y = y;
    // //super.setSize(length+dashLength+x, length+dashLength+y);
    // //super.setPreferredSize(new Dimension(length+dashLength+x, length+dashLength+y));
    // //repaint();
    // revalidate();
}

@Override
public Point getLocation(){
    return new Point(this.x, this.y);
}

//Code altered, but mainly from http://www.java2s.com/Code/Java/2D-Graphics-GUI/Dashedstroke.htm
@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);
    setBackground(Color.white);

    Graphics2D g2 = (Graphics2D) g;

    g2.setStroke(new BasicStroke(3.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, dashLength, new float[]{dashLength}, 0.0f));
    g2.setPaint(Color.black);
    Rectangle r = new Rectangle(x, y, length, length);

    g2.draw(r);
}
}

Upvotes: 0

Views: 604

Answers (1)

Sync it
Sync it

Reputation: 1198

If all you are trying to accomplish is to create a component with dashed border at a specific location then I hope this sample will help get you started

    public class DashTest
    {
     public static void main(String args[])
     {
      JFrame frame=new JFrame("Dash Test");
    
      frame.setContentPane(new JPanel(null)); /*A panel with no layout as the main container*/
    
      JButton button=new JButton("Dashed Button");
    
      button.setBorder(BorderFactory.createDashedBorder(Color.red));
    /*swing comes inbuilt with custom borders*/
    
  
     button.setBounds(100,100,150,30);/*set the x,y,width,height of the button works only if you have no layout set as shown above*/
    
     frame.add(button);
    
     frame.setSize(500,500);
    
     frame.setVisible(true);
     }
    }    

For your purpose there is no need to subclass your component to JComponent just for using borders,however if you are trying to do more fancy stuff inside your component and not just a border then you have to override that components paint component method

class MyComponent extends JComponent
{  
 MyComponent ()
 {
  super();
    
  setBorder(BorderFactory.createDashedBorder(Color.red));/*keep the red border*/
  }

 @Override
 public void paintComponent(Graphics g)
{
 super.paintComponent(g);/*NEVER FORGET THIS*/

 Graphics2D g2d=(Graphics2D)g;

 /*do more creative stuff now with graphics*/
 }
}

Upvotes: 1

Related Questions