简体   繁体   English

在 Java 中绘制最简单的方法是什么?

[英]What is the simplest way to draw in Java?

What is the simplest way to draw in Java?在 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.这不起作用,我不知道如何让任何东西出现。

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.您只需要扩展JPanel并覆盖面板的paintComponent方法。

I'd like to reiterate that you should not be overriding the paint method.我想重申一下,您应该覆盖paint方法。

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);
}
// 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();

To make something appear in paint(Graphics g) you need to call the drawing methods (like fillRect) on that Graphics.要使某些内容出现在paint(Graphics g) 中,您需要调用该Graphics 上的绘图方法(如fillRect)。 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);
}

jjnguy already wrote how to do it right ... but here why it does not work in your example: jjnguy 已经写了如何正确地做到这一点......但是这里为什么它在你的例子中不起作用:

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.这里有一个与 Swing 或 AWT 没有任何关系的类。 (By the way, you may want to select another name to avoid confusion with java.awt.Canvas .) (顺便说一句,您可能需要选择另一个名称以避免与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.在这里,您将创建一个新的 JPanel(也称为canvas混淆),并将其添加到框架中。 Is is this panel's paint and paintComponent methods which are called when the system shows your frame. Is 是该面板的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);
    }

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.这个paint方法从不使用(因为它不是组件的一部分),如果它会被调用,那么你只是在绘制一些BufferedImage,而不是屏幕。

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM