Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have developed an application to produce the following output:- enter image description here

I overrided the JPanel's getPrefferredSize but why i am not observing any changes in the output when i change the size.Even if i set the size to 0,0 the diagram is not translated at all and I get the same output as shown above .

Here is my full code:-

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class Skeleton extends JFrame{
Skeleton()
{
super("Donut");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);

add(new Board());
setVisible(true);
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable(){public void run(){new Skeleton();}});
}
}

class Board extends JPanel
{
public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    Graphics2D g2=(Graphics2D)g;
    RenderingHints rh=new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    //the following statement has no noticeable effect.
    rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2.setRenderingHints(rh);
    Ellipse2D e=new Ellipse2D.Double(0, 0, 80, 130);
    g2.setStroke(new BasicStroke(1));
    g2.setColor(Color.RED);
    for(int deg=0;deg<360;deg+=5)
    {
        AffineTransform at=AffineTransform.getTranslateInstance(getWidth()/2,getHeight()/2);
        at.rotate(Math.toRadians(deg));
        //returns a new Shape object after it has been undergone the 'at' transformation.
        g2.draw(at.createTransformedShape(e));
    }
}
public Dimension getPreferredSize()
{
    return new Dimension(100,100);
}
}
share|improve this question
    
please format your code with some consistent indentation ... –  kleopatra Nov 2 '12 at 15:51

1 Answer 1

up vote 5 down vote accepted

Because of your layout manager. By default BorderLayout ignores preferred size but fills all available space. Use e.g. FlowLayout and yu will see the difference.

share|improve this answer
    
:What does 'getWidth()' in the 'paintComponent()' of 'JPanel' return.Is it the width of frame or the width of JPanel,which i have provided in 'getPreferredSize()'. Is the value returned will be same as returned if I replace 'getWidth()' with 'getSize().getWidth()'. –  Insane Coder Nov 2 '12 at 14:54
1  
The getWidth() returns real width of component. Sometimes it's preferred but in general preferred just tells to layout manager what's desired size. Then layout manager decides how to process the size. –  StanislavL Nov 2 '12 at 15:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.