用Java最简单的绘图方法是什么?

发布于 2024-10-22 01:52:55 字数 688 浏览 2 评论 0原文

用Java最简单的绘图方法是什么?

import java.awt.*;
import javax.swing.*;

public class Canvas
{
    private JFrame frame;    
    private Graphics2D graphic;
    private JPanel canvas;

    public Canvas()
    {
        frame = new JFrame("A title");
        canvas = new JPanel();
        frame.setContentPane(canvas);
        frame.pack();
        frame.setVisible(true);
    }

    public void paint(Graphics g){
        BufferedImage offImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Grapics2D g2 = offImg.createGraphics();
        g2.setColor(new Color(255,0,0));
        g2.fillRect(10,10,200,50);
    }
}

这不起作用,我不知道如何让任何东西出现。

What is the simplest way to draw in Java?

import java.awt.*;
import javax.swing.*;

public class Canvas
{
    private JFrame frame;    
    private Graphics2D graphic;
    private JPanel canvas;

    public Canvas()
    {
        frame = new JFrame("A title");
        canvas = new JPanel();
        frame.setContentPane(canvas);
        frame.pack();
        frame.setVisible(true);
    }

    public void paint(Graphics g){
        BufferedImage offImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Grapics2D g2 = offImg.createGraphics();
        g2.setColor(new Color(255,0,0));
        g2.fillRect(10,10,200,50);
    }
}

This doesn't work and I have no idea how to get anything to appear.

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

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

发布评论

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

评论(4

暗喜 2024-10-29 01:52:56

jjnguy 已经写了如何正确执行此操作...但这里为什么它在您的示例中不起作用:

import java.awt.*;
import javax.swing.*;

public class Canvas

这里您有一个与 Swing 或 AWT 没有任何关系的类。
(顺便说一下,您可能需要选择另一个名称以避免与 java.awt.Canvas 混淆。)

{
    private JFrame frame;    
    private Graphics2D graphic;
    private JPanel canvas;

    public Canvas()
    {
        frame = new JFrame("A title");
        canvas = new JPanel();
        frame.setContentPane(canvas);

在这里,您将创建一个新的 JPanel(为了混淆也命名为 canvas) ),并将其添加到框架中。
这是该面板的 paintpaintComponent 方法,当系统显示框架时调用它们。

        frame.pack();
        frame.setVisible(true);
    }

    public void paint(Graphics g){
        BufferedImage offImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Grapics2D g2 = offImg.createGraphics();
        g2.setColor(new Color(255,0,0));
        g2.fillRect(10,10,200,50);
    }

这个绘制方法根本不会被使用(因为它不是组件的一部分),如果调用它,那么您只是绘制到一些 BufferedImage,而不是绘制到屏幕。

}

jjnguy already wrote how to do it right ... but here why it does not work in your example:

import java.awt.*;
import javax.swing.*;

public class Canvas

Here you have a class which does not relate in any way to Swing or AWT.
(By the way, you may want to select another name to avoid confusion with java.awt.Canvas.)

{
    private JFrame frame;    
    private Graphics2D graphic;
    private JPanel canvas;

    public Canvas()
    {
        frame = new JFrame("A title");
        canvas = new JPanel();
        frame.setContentPane(canvas);

Here you are creating a new JPanel (for confusion also named canvas), and add it to the frame.
Is is this panel's paint and paintComponent methods which are called when the system shows your frame.

        frame.pack();
        frame.setVisible(true);
    }

    public void paint(Graphics g){
        BufferedImage offImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Grapics2D g2 = offImg.createGraphics();
        g2.setColor(new Color(255,0,0));
        g2.fillRect(10,10,200,50);
    }

This paint method is never used at all (since it is not part of a component), and if it would be called, then you are only painting to some BufferedImage, not to the screen.

}
2024-10-29 01:52:55

最简单的方法:

public class Canvas extends JPanel {

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        // painting code goes here.
    }
}

您只需扩展 JPanel 并重写面板的 paintComponent 方法即可。

我想重申,您不应该覆盖 paint 方法。

这是一个非常简单且有效的示例。

public static void main(String[] args) {
    JFrame f = new JFrame();
    JPanel p = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawLine(0, 0, 100, 100);
        }
    };
    f.add(p);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

Easiest way:

public class Canvas extends JPanel {

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        // painting code goes here.
    }
}

You simply need to extend JPanel and override the paintComponent method of the panel.

I'd like to reiterate that you should not be overriding the paint method.

Here is a very minimalistic example that works.

public static void main(String[] args) {
    JFrame f = new JFrame();
    JPanel p = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawLine(0, 0, 100, 100);
        }
    };
    f.add(p);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}
度的依靠╰つ 2024-10-29 01:52:55
// No. 1
// Create a graphics context on the buffered image
Graphics2D g2d = bimage.createGraphics();

// Draw on the buffered image
g2d.setColor(Color.red);
g2d.fill(new Ellipse2D.Float(0, 0, 200, 100));
g2d.dispose();

// No.2
// In case the buffered image supports transparency

g2d = bimage.createGraphics();

// Transparency is created on all the filled pixels
Color transparent = new Color(0, 0, 0, 0);
g2d.setColor(transparent);
g2d.setComposite(AlphaComposite.Src);
g2d.fill(new Rectangle2D.Float(20, 20, 100, 20));
g2d.dispose();
// No. 1
// Create a graphics context on the buffered image
Graphics2D g2d = bimage.createGraphics();

// Draw on the buffered image
g2d.setColor(Color.red);
g2d.fill(new Ellipse2D.Float(0, 0, 200, 100));
g2d.dispose();

// No.2
// In case the buffered image supports transparency

g2d = bimage.createGraphics();

// Transparency is created on all the filled pixels
Color transparent = new Color(0, 0, 0, 0);
g2d.setColor(transparent);
g2d.setComposite(AlphaComposite.Src);
g2d.fill(new Rectangle2D.Float(20, 20, 100, 20));
g2d.dispose();
那些过往 2024-10-29 01:52:55

要使某些内容出现在 Paint(Graphics g) 中,您需要调用该 Graphics 上的绘图方法(如 fillRect)。您正在创建位图,然后绘制到位图,而不是屏幕。

public void paint(Graphics g){
    g.setColor(new Color(255,0,0));
    g.fillRect(10,10,200,50);
}

To make something appear in paint(Graphics g) you need to call the drawing methods (like fillRect) on that Graphics. You are creating a bitmap and then drawing to the bitmap, not the screen.

public void paint(Graphics g){
    g.setColor(new Color(255,0,0));
    g.fillRect(10,10,200,50);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文