Domanda

Basically I want to draw an ellipse which its size is relative to JPanel in Java.

for example: Ellipse2D e = new Ellipse2D.Double(0, 0, w, h)

Which w and h is the size of the panel. So by doing this, the ellipse will automatically resize when the panel is changing its size.

I have tried this but actually it doesn't work, I wrote this code for testing only.

public class Help extends JFrame{

    public static void main(String [] agrs){
        Help h = new Help();
        h.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        h.init();
    }

    public void init(){
        this.setLayout(new FlowLayout());
        this.setSize(2000, 1000);

        JPanel a = new JPanel();
        a.setPreferredSize(new Dimension(1000, 500));
        a.setBorder(BorderFactory.createLineBorder(Color.yellow, 3));
        Help_Option k = new Help_Option(a.getPreferredSize().width/2, a.getPreferredSize().height/4);
        k.setPreferredSize(new Dimension(1000, 400));
        a.add(k);

        this.add(a);
        this.validate();
        this.setVisible(true);
    }
}

class Help_Option extends JComponent implements MouseMotionListener{
    private static int x, y;
    private Ellipse2D ellipse = new Ellipse2D.Double(0, 0, x, y);
    private Color c = Color.MAGENTA;    

    public Help_Option(int x, int y){
        Help_Option.x = x;
        Help_Option.y = y;
        this.addMouseMotionListener(this);
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        g2d.setColor(Color.BLUE);
        g2d.draw(ellipse);

        g2d.setColor(c);
        g2d.fill(ellipse);        

        g2d.setColor(Color.BLACK);
        g2d.setFont(new Font("TimesRoman", Font.BOLD, 20));
        g2d.drawString("Here I am", 250, 100);
    }

    public void setColor(Color c){
        this.c = c;
    }

    @Override
    public void mouseDragged(MouseEvent e) {

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        if(ellipse.contains(e.getX(), e.getY())){
            setColor(Color.GREEN);
            repaint();
        }else{
            setColor(Color.MAGENTA);
            repaint();
        }
    }
}
È stato utile?

Soluzione

Thank you for your code snippet. In order to make it work, you need to initalize the ellipse in the Help_Option-Constructor otherwise it is draw at 0, 0 with the widht and height of 0 and 0.

...
private Ellipse2D ellipse = null; //also works w/o this assignment.
private Color c = Color.MAGENTA;    

public Help_Option(int x, int y){
    Help_Option.x = x;
    Help_Option.y = y;
    ellipse = new Ellipse2D.Double(0, 0, x, y);
    this.addMouseMotionListener(this);
}
...

If I change the code accordingly, the ellipse stays at its size even though I resize the window.

Image with unscaled ellipse

Please give me a hint (screenshot) what you mean.

Altri suggerimenti

I want to draw an ellipse which its size is relative to JPanel in Java.

Then you need to create the Ellipse object in the paintComponent() method. You can use the getWidth() and getHeight() methods to get the current size of the panel.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top