多个 Graphics2D 对象

发布于 2024-08-25 04:47:41 字数 1907 浏览 11 评论 0原文

我有一个 JPanel 的 Graphics 对象,并且工作正常:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;

public class GraphicsTest extends JPanel
{
    private Graphics2D g2d;
    private String state;
    private int x, y;

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g2d = (Graphics2D) g;
        g2d.setClip(0, 0, getWidth(), getHeight());

        g2d.setColor(Color.BLACK);
        g2d.drawString("STATE: " + state, 5, 15);
        g2d.drawString("Mouse Position: " + x + ", " + y, 5, 30);

        g2d.setColor(Color.red);
        Rectangle2D r2d = new Rectangle2D.Double(x,y,10,10);
        g2d.draw(r2d);
        Test t = new Test();
        super.add(t);

        repaint();
    }

    public void setState(String state) { this.state = state; }
    public String getState() { return state; }

    public void setX(int x) { this.x = x; }
    public void setY(int y) { this.y = y; }
}

我正在尝试一个新的 Graphics 组件,当我实例化一个新的 Test 并将其添加到 GraphicsTest 中时,什么也没有发生。我做错了什么?

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

public class Test extends JComponent
{
    private Graphics2D g2d;
    private String state;
    private int x, y;

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

        g2d.setColor(Color.GREEN);
        g2d.fill(new Rectangle2D.Double(60, 60,
                10, 10));

        repaint();
    }

    public void setState(String state) { this.state = state; }
    public String getState() { return state; }

    public void setX(int x) { this.x = x; }
    public void setY(int y) { this.y = y; }

}

谢谢!

I have a Graphics object of JPanel and that is working fine:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;

public class GraphicsTest extends JPanel
{
    private Graphics2D g2d;
    private String state;
    private int x, y;

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g2d = (Graphics2D) g;
        g2d.setClip(0, 0, getWidth(), getHeight());

        g2d.setColor(Color.BLACK);
        g2d.drawString("STATE: " + state, 5, 15);
        g2d.drawString("Mouse Position: " + x + ", " + y, 5, 30);

        g2d.setColor(Color.red);
        Rectangle2D r2d = new Rectangle2D.Double(x,y,10,10);
        g2d.draw(r2d);
        Test t = new Test();
        super.add(t);

        repaint();
    }

    public void setState(String state) { this.state = state; }
    public String getState() { return state; }

    public void setX(int x) { this.x = x; }
    public void setY(int y) { this.y = y; }
}

I was experimenting with a new Graphics component and when I instantiate a new Test and add it in GraphicsTest nothing happens. What is it that I am doing wrong?

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

public class Test extends JComponent
{
    private Graphics2D g2d;
    private String state;
    private int x, y;

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

        g2d.setColor(Color.GREEN);
        g2d.fill(new Rectangle2D.Double(60, 60,
                10, 10));

        repaint();
    }

    public void setState(String state) { this.state = state; }
    public String getState() { return state; }

    public void setX(int x) { this.x = x; }
    public void setY(int y) { this.y = y; }

}

Thanks!

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

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

发布评论

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

评论(1

孤独陪着我 2024-09-01 04:47:41

您应该仅添加一次测试组件并设置布局:

public GraphicTest()
{
    super();
    setLayout(new BorderLayout());
    add(new Test(),BorderLayout.CENTER);
}

此外,不要保存 Graphics2D 对象,因为一旦 paintComponent 完成并且不调用 repaint,它们将无效 paintComponent 内。如果您需要动画并在 setXXX 方法中调用 repaint 来更改组件的外观,请使用计时器。

You should add Test component only once and set a layout:

public GraphicTest()
{
    super();
    setLayout(new BorderLayout());
    add(new Test(),BorderLayout.CENTER);
}

Also, do not save Graphics2D objects because they will be invalid once paintComponent is finished and do not call repaint inside paintComponent. Use timers if you need animation and call repaint in setXXX methods that change the look of the component.

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