Graphics2D 绘制不正确,它只绘制椭圆形的一部分

我正在尝试使用 Graphics2D 在 JFrame 上绘制一个椭圆形,我希望它随窗口调整大小,从技术上讲它确实如此,它只是不绘制大约三分之一的椭圆形。这肯定是我做错了,我是 Java 的 Graphics2D 部分的新手。


我不确定这是否是我的电脑的问题,所以我尝试在另一台电脑上运行我的代码以再次发生这种情况,所以我不确定我哪里出了问题。


import javax.swing.*;

import java.awt.*;


public class ClockViewer {

    public static void main(String[] args) {

        //create frame

        JFrame frame = new JFrame();

        final int Frame_Width = 110;

        final int Frame_Height = 130;


        //set frame attributes

        frame.setSize(Frame_Width, Frame_Height);

        frame.setTitle("A Really Descriptive Title...");

        frame.setVisible(true);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        //get pane attributes

        System.out.println(frame.getContentPane().getWidth());

        System.out.println(frame.getContentPane().getHeight());


        //create ellipse

        EllipseComponent ellipse = new EllipseComponent();


        //add ellipse to frame

        while(frame.getContentPane().getHeight() > 0 && frame.getContentPane().getWidth() > 0) {

            int posX = Math.round(frame.getContentPane().getWidth() / 100) * 20;

            int posY = Math.round(frame.getContentPane().getHeight() / 100) * 20;

            int Width = Math.round(frame.getContentPane().getWidth() / 100) * 80;

            int Height = Math.round(frame.getContentPane().getHeight() / 100) * 80;

            ellipse.setAll(posX, posY, Width, Height);

            frame.add(ellipse);

        }

    }

}

这是下一个文件:


import javax.swing.*;

import java.awt.*;

import java.awt.geom.*;


public class EllipseComponent extends JComponent {

    //global variables for ellipse drawing

    int posX = 0;

    int posY = 0;

    int Width = 0;

    int Height = 0;


    //getters for getting variables values

    public int getPosX() {

    return this.posX;

    }

    public int getPosY() {

        return this.posY;

    }


感谢大家为我提供的帮助^^


神不在的星期二
浏览 111回答 1
1回答

慕工程0101907

我没有收到您收到的错误。然而,这不是在 Swing 中进行自定义绘画的正确方法。长话短说,你在这种while情况下尝试的东西是行不通的。相反,让组件根据其父级大小和坐标进行绘制,而不必将它们设置为显式:/** * @Overide paint method was a thing in AWT. * In Swing you must override paintComponent (and call super.paintComponent()) * in order to respect the paint chain. *  */@Overrideprotected void paintComponent(Graphics g) {    super.paintComponent(g);    if (getParent() != null) { //Paint according to parent        Graphics g2 = (Graphics2D) g;        //Calculations        int posX = Math.round(getParent().getWidth() / 100) * 20;        int posY = Math.round(getParent().getHeight() / 100) * 20;        int Width = Math.round(getParent().getWidth() / 100) * 80;        int Height = Math.round(getParent().getHeight() / 100) * 80;        g2.drawOval(posX, posY, Width, Height);    }}您所做的另一件不必要的事情是:@Overridepublic int getWidth() {    return this.Width;}重写一个组件的getWidthandgetSize不会带你到任何地方。完整的例子:public class ClockViewer {    public static void main(String[] args) {        SwingUtilities.invokeLater(()->{            // create frame            JFrame frame = new JFrame();            final int Frame_Width = 110;            final int Frame_Height = 130;            // set frame attributes            frame.setSize(Frame_Width, Frame_Height);            frame.setTitle("A Really Descriptive Title...");            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);            // get pane attributes            System.out.println(frame.getContentPane().getWidth());            System.out.println(frame.getContentPane().getHeight());            // create ellipse            JComponent ellipse = new JComponent() {                @Override                protected void paintComponent(Graphics g) {                    super.paintComponent(g);                    if (getParent() != null) { //Paint according to parent                        Graphics g2 = (Graphics2D) g;                        //Calculations                        int posX = Math.round(getParent().getWidth() / 100) * 20;                        int posY = Math.round(getParent().getHeight() / 100) * 20;                        int Width = Math.round(getParent().getWidth() / 100) * 80;                        int Height = Math.round(getParent().getHeight() / 100) * 80;                        g2.drawOval(posX, posY, Width, Height);                    }                }            };            frame.add(ellipse);            frame.setVisible(true);        });    }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java