java 如何在AWT中将图像添加到帧中?

rdrgkggo  于 2023-03-21  发布在  Java
关注(0)|答案(2)|浏览(126)

我试过这个,但它不工作。

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.Panel;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class Gfx
{
  public static void main(String[] args)
  {
    Frame f = new Frame ("Gfx");
    f.addWindowListener (new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          f.dispose (); }});
    Panel p = new Panel ();
    f.add(p);
    BufferedImage image = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_ARGB);
    p.add(image);
    f.pack();
    f.setVisible (true);
  }
}

错误为:

Gfx.java:21: error: no suitable method found for add(BufferedImage)
    p.add(image);
     ^
    method Component.add(PopupMenu) is not applicable
      (argument mismatch; BufferedImage cannot be converted to PopupMenu)
    method Container.add(Component) is not applicable
      (argument mismatch; BufferedImage cannot be converted to Component)

BufferedImage不是Component。如何将其转换为Component

carvr3hs

carvr3hs1#

如果你想使用AWT来绘图,你可以使用Canvas。在Java Swing中,你可以创建一个绘图JPanel
我修改了你的代码来创建下面的GUI:

我缩小了图像大小,以便GUI更好地适应答案。
下面是完整的可运行代码。

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;

public class Gfx {
    public static void main(String[] args) {
        Frame f = new Frame("Gfx");
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                f.dispose();
            }
        });

        Canvas canvas = new Canvas() {
            private static final long serialVersionUID = 1L;

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }

            @Override
            public void paint(Graphics g) {
                super.paint(g);
                BufferedImage image = new BufferedImage(200, 200,
                        BufferedImage.TYPE_INT_ARGB);
                g.drawImage(image, 0, 0, this);
            }
        };

        f.add(canvas);
        f.pack();
        f.setVisible(true);
    }
}
7bsow1i6

7bsow1i62#

完整示例及部分图纸:

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;

public class Gfx
{
  static final int width = 800;
  static final int height = 600;

  static Frame frame (String title)
  {
    Frame frame = new Frame (title);
    frame.addWindowListener (new WindowAdapter ()
      {
        @Override
        public void windowClosing (WindowEvent ev) { frame.dispose (); }
      });
    return frame;
  }

  static BufferedImage image ()
  {
    BufferedImage image = new BufferedImage (width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics graphics = image.getGraphics();
    graphics.setColor (Color.RED);
    graphics.fillPolygon (new int[] {width, 0, 0}, new int[] {height/2, 0, height}, 3);
    graphics.dispose ();
    return image;
  }

  static Canvas canvas (Image image)
  {
    Canvas canvas = new Canvas ()
      {
        @Override
        public Dimension getPreferredSize () {
          return new Dimension (width, height); }
        @Override
        public void paint (Graphics graphics)
        {
          super.paint (graphics);
          graphics.drawImage (image, 0, 0, this); // this?
        }
      };
    return canvas;
  }

  public static void main (String[] args)
  {
    Frame frame = frame ("Gfx");
    Image image = image ();
    Canvas canvas = canvas (image);
    frame.add (canvas);
    frame.pack ();
    frame.setVisible (true);
  }
}

相关问题