如何在框架内调整组件大小和绘制组件

发布于 2024-12-07 11:17:39 字数 934 浏览 1 评论 0原文

编写一个程序,用一个大椭圆填充窗口。即使窗口大小被调整,椭圆也应该接触窗口边界。

我有以下代码:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JComponent;

public class EllipseComponent extends JComponent {
    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;

        Ellipse2D.Double ellipse = new Ellipse2D.Double(0,0,150,200);
        g2.draw(ellipse);
        g2.setColor(Color.red);
        g2.fill(ellipse);
    }
}

和主类:

import javax.swing.JFrame;

public class EllipseViewer {
   public static void main(String[] args)
   {
       JFrame frame = new JFrame();
       frame.setSize(150, 200);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       EllipseComponent component = new EllipseComponent();
       frame.add(component);

       frame.setVisible(true);
   }
}

Write a program that fills the window with a larrge ellipse. The ellipse shoud touch the window boundaries, even if the window is resized.

I have the following code:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JComponent;

public class EllipseComponent extends JComponent {
    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;

        Ellipse2D.Double ellipse = new Ellipse2D.Double(0,0,150,200);
        g2.draw(ellipse);
        g2.setColor(Color.red);
        g2.fill(ellipse);
    }
}

And the main class:

import javax.swing.JFrame;

public class EllipseViewer {
   public static void main(String[] args)
   {
       JFrame frame = new JFrame();
       frame.setSize(150, 200);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       EllipseComponent component = new EllipseComponent();
       frame.add(component);

       frame.setVisible(true);
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

她比我温柔 2024-12-14 11:17:39

在 EllipseComponent 中,您执行以下操作:

Ellipse2D.Double ellipse = new Ellipse2D.Double(0,0,getWidth(),getHeight());

我还推荐 Hovercraft Full Of Eels 给出的更改。在这个简单的情况下,这可能不是问题,但随着 PaintComponent 方法变得越来越复杂,您确实希望在 PaintComponent 方法中进行尽可能少的计算。

in your EllipseComponent you do:

Ellipse2D.Double ellipse = new Ellipse2D.Double(0,0,getWidth(),getHeight());

I'd also recommend the changes given by Hovercraft Full Of Eels. In this simple case it might not be an issue but as the paintComponent method grows in complexity you realy want as little as possible to be computed in the paintComponent method.

蘸点软妹酱 2024-12-14 11:17:39

不要调整paintComponent内组件的大小。事实上,不要在此方法中创建对象或执行任何程序逻辑。该方法需要精简、尽可能快、进行绘图,就是这样。您必须了解,您无法完全控制何时或即使调用此方法,并且您当然不希望向其中添加不必要的代码,这可能会减慢速度。

您应该在类的构造函数中创建椭圆。要根据 JComponent 的大小和大小更改来调整其大小,请使用 ComponentListener。:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JComponent;

public class EllipseComponent extends JComponent {
    Ellipse2D ellipse = null;

    public EllipseComponent {
        ellipse = new Ellipse2D.Double(0,0,150,200);
        addComponentListener(new ComponentAdapter() {
           public void componentResized(ComponentEvent e) {
              // set the size of your ellipse here 
              // based on the component's width and height 
           }
        });
    }

    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
        g2.draw(ellipse);
        g2.setColor(Color.red);
        g2.fill(ellipse);
    }
}

警告:代码未运行或测试

Do not resize components within paintComponent. In fact, do not create objects or do any program logic within this method. The method needs to be lean, fast as possible, do drawing, and that's it. You must understand that you do not have complete control over when or even if this method is called, and you certainly don't want to add code to it unnecessarily that may slow it down.

You should create your ellipse in the class's constructor. To resize it according to the JComponent's size and on change of size, use a ComponentListener.:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JComponent;

public class EllipseComponent extends JComponent {
    Ellipse2D ellipse = null;

    public EllipseComponent {
        ellipse = new Ellipse2D.Double(0,0,150,200);
        addComponentListener(new ComponentAdapter() {
           public void componentResized(ComponentEvent e) {
              // set the size of your ellipse here 
              // based on the component's width and height 
           }
        });
    }

    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
        g2.draw(ellipse);
        g2.setColor(Color.red);
        g2.fill(ellipse);
    }
}

Caveat: code not run nor tested

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文