亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Graphics2D 繪制不正確,它只繪制橢圓形的一部分

Graphics2D 繪制不正確,它只繪制橢圓形的一部分

神不在的星期二 2023-09-27 15:21:42
我正在嘗試使用 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;    }感謝大家為我提供的幫助^^
查看完整描述

1 回答

?
慕工程0101907

TA貢獻1887條經驗 獲得超5個贊

我沒有收到您收到的錯誤。然而,這不是在 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.

?*?

?*/

@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);

? ? }

}

您所做的另一件不必要的事情是:


@Override

public 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);

? ? ? ? });


? ? }

}


查看完整回答
反對 回復 2023-09-27
  • 1 回答
  • 0 關注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號