简体   繁体   English

代码中的错误(java.awt.image.RasterFormatException)

[英]Error in the code ( java.awt.image.RasterFormatException)

there was a problem, I used this code here: 出现问题,我在这里使用了以下代码:

Kode KODE

I corrected him a little for themselves, now the code looks like this: 我为他自己做了一些更正,现在代码如下:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class gamet {

    public static void main(String[] args) {
        new gamet();
    }

    public gamet() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (InstantiationException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (UnsupportedLookAndFeelException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } catch (ClassNotFoundException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new WorldPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class WorldPane extends JPanel {

        private static final long serialVersionUID = 1L;
        private BufferedImage map;
        private BufferedImage party;
        private Point viewPort;
        private Point partyPoint;
        private BufferedImage view;

        public WorldPane() {
            try {
                map = ImageIO.read(new File("C:\\Users\\дНМ\\workspace\\GameK\\image\\Maps.png"));
                party = ImageIO.read(new File("C:\\Users\\дНМ\\workspace\\GameK\\image\\Maps.png"));

                viewPort = new Point(0, (map.getHeight() / 2) - 100);
                partyPoint = new Point(party.getWidth() / 2, (map.getHeight() / 2)); // Virtual Point...

            } catch (IOException exp) {
                exp.printStackTrace();
            }

            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            ActionMap am = getActionMap();

            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "goRight");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "goLeft");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "goUp");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "goDown");

            am.put("goRight", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    moveParty(10, 0);
                }
            });
            am.put("goLeft", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    moveParty(-10, 0);
                }
            });

            am.put("goUp", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    moveParty(0, -10);
                }
            });
            am.put("goDown", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    moveParty(0, 10);
                }
            });

        }

        protected void moveParty(int xDelta, int yDelta) {
            partyPoint.x += xDelta;
            partyPoint.y += yDelta;
            Point view = fromWorld(partyPoint);
            if (view.x > getWidth() - (party.getWidth() / 2)) {
                viewPort.x += xDelta;
                if (viewPort.x + getWidth() > map.getWidth()) {
                    viewPort.x = map.getWidth() - getWidth();
                    partyPoint.x = map.getWidth() - (party.getWidth() / 2) - 1;
                }
                invalidate();
            } else if (view.x < party.getWidth() / 2) {
                viewPort.x += xDelta;
                if (viewPort.x < 0) {
                    viewPort.x = 0;
                    partyPoint.x = (party.getWidth() / 2);
                }
                invalidate();
            }
            System.out.println(view + "; " + getHeight());
            if (view.y > getHeight() - (party.getHeight() / 2)) {
                viewPort.y += yDelta;
                if (viewPort.y + getHeight() > map.getHeight()) {
                    viewPort.y = map.getHeight() - getHeight();
                    partyPoint.y = map.getHeight() - (party.getHeight() / 2) - 1;
                }
                invalidate();
            } else if (view.y < party.getHeight() / 2) {
                viewPort.y += yDelta;
                if (viewPort.y < 0) {
                    viewPort.y = 0;
                    partyPoint.y = (party.getHeight() / 2);
                }
                invalidate();
            }
            repaint();
        }

        @Override
        public void invalidate() {
            view = null;
            super.invalidate();
        }

        public BufferedImage getView() {

            if (view == null && getWidth() > 0 && getHeight() > 0) {

                view = map.getSubimage(viewPort.x, viewPort.y, getWidth(), getHeight());

            }

            return view;

        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            if (map != null) {
                g2d.drawImage(getView(), 0, 0, this);

                Point real = fromWorld(partyPoint);

                int x = real.x - (party.getWidth() / 2);
                int y = real.y - (party.getHeight()/ 2);
                g2d.drawImage(party, x, y, this);
            }
            g2d.dispose();
        }

        protected Point fromWorld(Point wp) {

            Point p = new Point();

            p.x = wp.x - viewPort.x;
            p.y = wp.y - viewPort.y;

            return p;

        }
    }
}

But the problem is that when I start it I see it: 但是问题是,当我启动它时,我看到了:

Exception in thread "AWT-EventQueue-0" java.awt.image.RasterFormatException: (x + width) is outside of Raster
    at sun.awt.image.ByteInterleavedRaster.createWritableChild(Unknown Source)
    at java.awt.image.BufferedImage.getSubimage(Unknown Source)
    at gamet$WorldPane.getView(gamet.java:164)
    at gamet$WorldPane.paintComponent(gamet.java:182)

Please help solve the problem) 请帮助解决问题)

I would use there pictures, but they are not lined ( 我会用那里的图片,但它们没有内衬(

UPD UPD

Reduced the size of the picture now I see this error: 缩小图片的大小现在我看到此错误:

Exception in thread "AWT-EventQueue-0" java.awt.image.RasterFormatException: y lies outside the raster
    at sun.awt.image.ByteInterleavedRaster.createWritableChild(Unknown Source)
    at java.awt.image.BufferedImage.getSubimage(Unknown Source)
    at gamet$WorldPane.getView(gamet.java:164)
    at gamet$WorldPane.paintComponent(gamet.java:182)

The exception you are getting: 您得到的异常:

Exception in thread "AWT-EventQueue-0" java.awt.image.RasterFormatException: (x + width) is outside of Raster

This makes me think that you are outside the dimensions of the image. 这使我认为您超出了图像的尺寸。

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

相关问题 Selenium:java.awt.image.RasterFormatException:(y +高度)在使用元素大小和位置时在Raster之外 - Selenium: java.awt.image.RasterFormatException: (y + height) is outside of Raster when using element size and location Java AWT自定义CompositeContext和消除锯齿:在客户区外部绘制时出现RasterFormatException - Java AWT custom CompositeContext & anti-aliasing: RasterFormatException when drawing outside of the client area 错误:无法将sun.awt.image.ToolkitImage强制转换为java.awt.image.BufferedImage - Error: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage 为什么不将java.awt.Image视为java.awt.Component? - Why java.awt.Image is not considered as java.awt.Component? Java AWT-EventQueue-0错误 - Java AWT-EventQueue-0 Error 缩放图像会在 java.awt.image.ReplicateScaleFilter 处产生错误。<init> (来源不明)) - scaling an image produce the error at java.awt.image.ReplicateScaleFilter.<init>(Unknown Source) ) 在java中将java.awt.Image保存到磁盘 - Save java.awt.Image to disk in java 如何将Java(AUT)代码转换为JSP代码 - how to convert java (awt) code into jsp code Android上java.awt.image.BufferedImage的ClassNotFoundException - ClassNotFoundException for java.awt.image.BufferedImage on Android 如何将图像添加到java.awt画布? - How to add an image to a java.awt canvas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM