小编use*_*233的帖子

显示Windows 7的ImageIcon png文件的正确途径是什么?

我想测试一个带有简单png图像的程序.我写了一个简短的程序来做到这一点,但我似乎无法让路径正确.我已经检查过,再次检查,重新检查,并检查了我的路径名称四倍,但没有正确,但无论我做什么,这个图像都不会显示.我使用了Oracle在ImageIcon文档(creaetImageIcon())中编写的一个简短的类来实现这一目标,但它似乎没有帮助.我将在下面发布整个程序,因为它很短.

package practiceImages;

import java.awt.BorderLayout;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ImageIconGUITest {

    public static void main(String[] args) {
        ImageIconGUITest gui = new ImageIconGUITest();
        gui.display();
    }

    private ImageIcon createImageIcon(String path, String description) {
        java.net.URL imgURL = getClass().getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL, description);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    private void display() {
        JFrame frame = new JFrame();
        JLabel label = new JLabel(createImageIcon(
                "Users/Evan/javaItems/Sprites_and_Other_Art/green.png", …
Run Code Online (Sandbox Code Playgroud)

java swing path embedded-resource imageicon

4
推荐指数
2
解决办法
4万
查看次数

使用JFileChooser保存BufferedImage会引发一些问题

我有一个相当简单的绘图程序,它使用BufferedImage作为画布.我有一个单独的类(IOClass)来处理保存图像和打开另一个图像.我通过我的saveImage()方法保存BufferedImage有点麻烦.这是整个班级:

 package ui;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

// class for Saving & Opening images (for the bufferedImage called 'background'
public class IOClass {
    static BufferedImage image;

    public IOClass(BufferedImage image) {
        this.image = image;
    }

    public static final JFileChooser fileChooser = new JFileChooser();
    public void saveImage() {
        int saveValue = fileChooser.showSaveDialog(null);
        if (saveValue == JFileChooser.APPROVE_OPTION) {
            try {
                ImageIO.write(image, "png", new File(fileChooser
                        .getSelectedFile().getAbsolutePath()
                        + fileChooser.getSelectedFile().getName()));
            } catch (IOException e) …
Run Code Online (Sandbox Code Playgroud)

java swing bufferedimage jfilechooser nimbus

1
推荐指数
1
解决办法
3594
查看次数

我如何确保这个方块移动并且不使用openGL扩展?

我有一个Pong游戏,我正在努力.现在,我正在努力让玩家划桨.这就是现在发生的事情.比赛开始,球拍位于屏幕上.向上按压桨板使方形向上延伸.按下则相反.我需要的是广场实际上作为单个对象移动并在每次移动时删除它自己以使其在整个时间内保持正方形.而不是桨变得更大,我需要它保持相同的形状,但只是移动.我将如何在openGL中执行此操作?这是我的两个班级.

启动(主要课程):

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

import com.evanklein.pong.entitity.Player;

public class Startup {

    // set up display
    public void start() {
        try {
            Display.setDisplayMode(new DisplayMode(600, 400)); // these numbers
                                                                // pending
            Display.setTitle("Evan's Pong!");
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(0);
        }

        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, 600, 400, 0, 1, -1);

        while (!Display.isCloseRequested()) {

            // render OpenGL here
            GL11.glBegin(GL11.GL_QUADS);
            GL11.glColor3f(3.0f, 7.2f, 6.7f);
            GL11.glVertex2d(player.width, player.length);
            GL11.glVertex2d(player.width + 100, player.length);
            GL11.glVertex2d(player.width + 100, player.length + 100);
            GL11.glVertex2d(player.width, player.length …
Run Code Online (Sandbox Code Playgroud)

java opengl lwjgl

1
推荐指数
1
解决办法
111
查看次数

为什么这个Java绘图程序不会绘制多个椭圆?

我有一个Java绘图程序,它使用自定义JPanel进行绘制.当点击JPanel时会画一个小椭圆(或圆圈,如果你愿意的话),每当你点击另一个地方时,椭圆会消失.坐标也会更新,但椭圆不会停留,它会移动到用户点击下一个位置...这是自定义JPanel的代码:

int xCord, yCord;

    public class PaintPanel extends JPanel implements MouseListener {
        // default serial whatever...
        private static final long serialVersionUID = -6514297510194472060L;

        // initial values
        int xCord = -10;
        int yCord = -10;

        public PaintPanel() {
            addMouseListener(this);
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(ProgramUI.currentColor);
            g.fillOval(xCord, yCord, 8, 8);
            repaint();
        }

        @Override
        public void mouseClicked(MouseEvent m) {
        }

        @Override
        public void mouseEntered(MouseEvent m) {
        }

        @Override
        public void mouseExited(MouseEvent m) {
        }

        @Override
        public void mousePressed(MouseEvent m) { …
Run Code Online (Sandbox Code Playgroud)

java swing repaint paintcomponent

0
推荐指数
1
解决办法
514
查看次数