无法显示图像 [英] cannot display Image

查看:158
本文介绍了无法显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程的新手,请您告诉我代码中的问题。
fillOval运行良好,但尝试用.gif文件替换它失败,窗口打开
没有查看...

I'm new being in programming, can you please tell me what is the problem in my code. The fillOval is operating well but trying to replace it with .gif file get failed, window is opened by nothing is viewed...

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class Game extends JPanel {

int x;
int y;

int inix=500;
int iniy=500;

int i=0; 
private void moveBall() {
    /*x = x + 1;
    y = y + 1;*/
    double degrees=(double) i;
    double radians=Math.toRadians(degrees);
    double Sinu=Math.sin(radians);
    double Sinu200=Math.sin(radians)*200;
    int SinuInt=(int) Sinu200;
    //y=500+SinuInt;
    y=iniy+SinuInt;
    double Cos=Math.cos(radians);
    double Cos200=Math.cos(radians)*200;
    int CosInt=(int) Cos200;
    //x=500+CosInt;
    x=inix+CosInt;


    i++;
    if (i==360){ i=0;}

            //System.out.println(Sinu+"   "+Sinu200+"   "+SinuInt +"   "+x);

        }


private int sin(double radians) {
    // TODO Auto-generated method stub
    return 0;
}

    @Override
public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    //g2d.setColor(Color.red);
    //g2d.fillOval(x, y, 50, 50);
    Image img1 = Toolkit.getDefaultToolkit().getImage("src/Images/MyIm.gif");
    g2d.drawImage(img1, x, y, this);


}

    public static void main(String[] args) throws InterruptedException {
    JFrame frame = new JFrame("Mini Tennis");
    Game game = new Game();
    frame.add(game);
    frame.setSize(1000, 1000);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    while (true) {
        game.moveBall();
        game.repaint();
        Thread.sleep(2);
    }
}


}


推荐答案

我不确定你的问题在哪里,但我最好的猜测是:

i'm not sure where your problem is, but my very best guess would be:

你的图片位置不是适当! (忽略其他类,它只是一个测试项目^^)

your image location is not suitable! (ignore the other classes, it's just a test project ^^)

我复制/粘贴你的代码,我使用了我的自定义图像,它可以正常使用我!

i've copy/pasted your code, i used my custom image and it works fine with me!

在你的eclipse项目中创建一个文件夹,但不要在你的SOURCE目录(!!!)然后参考这样的图像:

make an folder in your eclipse project BUT NOT IN YOUR SOURCE dir (!!!) and then refer to the image like this:

Image img1 = Toolkit.getDefaultToolkit().getImage("img/index.png");

这篇关于无法显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆