Only 1 Corner is Rounded when I call fillRoundRect()

自古美人都是妖i 提交于 2020-06-27 15:31:07

问题


When run this code:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JButton;
import javax.swing.JLabel;

public class CustomButton extends JButton {

    int width = 100;
    int height = 50;
    int radius = 10;
    JLabel lab;

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(Color.ORANGE);
        g2.fillRoundRect(0, 0, width, height, radius, radius);
        
        g2.dispose();
        super.paintComponent(g2);
        super.paintComponent(g);

    }

}

And my other class:

package custom.frame;

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class CustomFrame {

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new FlowLayout());
        f.setSize(500,500);
        f.setLocationRelativeTo(null);
        
        JPanel pane = new JPanel();
        pane.setBounds(0,0,500,500);
        
        CustomButton btn = new CustomButton();
        pane.add(btn);
        f.add(btn);
        f.setVisible(true);
        
    }

}

I get a get a regular rectangle with only 1 rounded side. Please see the image below.

enter image description here

Is this the expected function? If not, how can I fix this.

Edit

I can get 2 rounded corners if I do this: g2.fillRoundRect(0, 0, 50, 50, 7, 7);


回答1:


The only thing I can really think of is that the window containing the rectangle is too small and is cutting off the other three corners, but that seems pretty unlikely.



来源:https://stackoverflow.com/questions/21359095/only-1-corner-is-rounded-when-i-call-fillroundrect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!