首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Java drawImage()不绘图

Java drawImage()不绘图
EN

Stack Overflow用户
提问于 2017-06-08 15:29:16
回答 1查看 55关注 0票数 0

在我的程序中,我有以下代码:

代码语言:javascript
运行
复制
package io.github.AdmiralSbs.DiceWars;

import javax.imageio.ImageIO;
import javax.swing.*;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Scanner;

public class HexDisplay extends JPanel {
    private static final long serialVersionUID = 1L;
    public static final int SIZE = 200;
    private int height;
    private int width;
    private Hex[][] hex;
    private BufferedImage myImage;
    private Graphics drawer;

    public HexDisplay(File f) throws IOException {
        Scanner key = new Scanner(f);
        int[] temp = commaSplit(key.nextLine());
        height = temp[0];
        width = temp[1];
        hex = new Hex[width][height];
        for (int w = 0; w < width; w++) {
            for (int h = 0; h < height; h++) {
                //temp = commaSplit(key.nextLine());
                if (h % 2 == 0)
                    hex[w][h] = new Hex((int) (SIZE * (w + 0.5)),
                            (int) (SIZE * (h + 0.5)), SIZE);
                else
                    hex[w][h] = new Hex((int) (SIZE * (w + 1.5)),
                            (int) (SIZE * (h + 0.5)), SIZE);
                System.out.println(hex[w][h].getX() + " " + hex[w][h].getY());
            }
        }
        key.close();
        starting();
        ImageIO.write(myImage, "jpg", new File("outPic.jpg"));
    }

    public void starting() {
        setPreferredSize(new Dimension(400,400));
        setLayout(new FlowLayout());
        myImage = new BufferedImage(400, 400, BufferedImage.TYPE_INT_RGB);
        drawer = myImage.getGraphics();
        drawer.setColor(Color.BLUE);
        drawer.fillRect(0, 0, 400, 400);
        drawStuff(drawer);
        repaint();
    }

    public int[] commaSplit(String s) {
        String[] str = s.split(",");
        int[] ret = new int[str.length];
        for (int i = 0; i < str.length; i++) {
            ret[i] = Integer.parseInt(str[i]);
        }
        return ret;
    }

    public void paintComponents(Graphics g) {
        g.drawImage(myImage, 0, 0, this);
        System.out.println("Painted");
    }

    public void drawStuff(Graphics g) {
        for (int w = 0; w < width; w++) {
            for (int h = 0; h < height; h++) {
                hex[w][h].draw(g);
            }
        }
        System.out.println("Drew");
        revalidate();
        repaint();
        paintComponents(g);
    }
}

我所期望的是包含这个JPanel的框架显示图像,但它没有。所有System.out.println()方法都按预期调用,并且我能够将图像保存为.jpg文件。但是,该图像没有显示在GUI中。我们能做些什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-08 15:31:43

你有一个错误:

代码语言:javascript
运行
复制
//                        v
public void paintComponents(Graphics g) {

应该是paintComponent

并且应该包含对超级方法的调用:

代码语言:javascript
运行
复制
super.paintComponent(g);

作为方法中的第一行

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44439878

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档