JAVA drawImage正在错误的位置绘制Image

时间:2018-02-03 05:11:14

标签: java swing graphics position drawing

我正在使用Graphics类。这是我在某个位置绘制图像的代码:

    public class PixelManager extends JPanel {

    private ArrayList<Pixel> pixels = new ArrayList<Pixel>();

    public PixelManager() {   }

    public void addPixel(int posX, int posY, BufferedImage texture) {
        pixels.add(new Pixel(posX, posY, texture));
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();

        for (Pixel pixel : pixels) {
            g2d.drawImage(pixel.getTexture(), pixel.getX(), pixel.getY(), 20, 20, null);
        }
        g2d.dispose();
    }

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

说明:我将此类用作像素或纹理管理器。在ArrayList中,我保存了一些像素,包括位置和纹理。 in de paintComponent方法我正在迭代ArrayList并尝试绘制它们。

像素类:

   public class Pixel extends JPanel{
     private int _posX, _posY;
     private static int  _width = 20;
     private BufferedImage _texture;

     public Pixel(int posX, int posY, BufferedImage texture) {
         _posX = posX;
         _posY = posY;
         _texture = texture;
    }

     //Getter und Setter
     public int getPosY() {
         return _posY;
     }

     public int getPosX() {
         return _posX;
     }

     public void setPosY(int posY) {
         _posY = posY;
     }

     public void setPosX(int posX) {
         _posX = posX;
     }

     public BufferedImage getTexture() {
        return _texture;
    }

     public void setTexture(BufferedImage texture) {
        _texture = texture;
    }
}

JFrame扩展类:

public class Zeichnen extends JFrame {
    static PixelManager pman;

      public Zeichnen()
      {
             setTitle("Tutorial");
             setSize(400, 400);
             setVisible(true);
             setDefaultCloseOperation(EXIT_ON_CLOSE);           
      }

       public static void main(String[] args)
       {
        Zeichnen fenster = new Zeichnen();
        pman = new PixelManager();
        fenster.add(pman);
        try {
            pman.addPixel(100, 100, ImageIO.read(new File("C:\\Users\\Cronax3\\Documents\\grass.jpg")));
            pman.addPixel(200, 200, ImageIO.read(new File("C:\\\\Users\\\\Cronax3\\\\Documents\\\\blue.png")));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       }
}

所以这就是问题所在。两个图像将被绘制到面板/框架,但总是位于x = 0,y = 0的位置。我把所有的东西都弄到了这一行:

g2d.drawImage(pixel.getTexture(), pixel.getX(), pixel.getY(), 20, 20, null);

像素提供的值是正确的。 drawImage将接收正确的位置和正确的纹理/图像。

Becaus我是Graphics的新手我想请你们帮我解决这个问题。

提前致谢!

格尔茨 Cronax3

3 个答案:

答案 0 :(得分:0)

所以,你的基本问题归结为......

public static class Pixel extends JPanel {

为什么Pixel需要从JPanel扩展?

如果删除extends JPanel,则会触发编译错误,说“找不到符号getX等,但它不是您定义的方法,{ {1}}是。

getPosXgetX的属性,但由于我看不出有必要以这种方式使用JPanel,因此根本不需要它。事实上,你可以让JPanel只画自己,但这是另一天的讨论

答案 1 :(得分:0)

The short answer:
use g2d.drawImage(pixel.getTexture(), pixel.getPosX(), pixel.getPosY(), 20, 20, null); and not g2d.drawImage(pixel.getTexture(), pixel.getX(), pixel.getY(), 20, 20, null);
(To use the values assigned to posX and posY in Pixel)

The long and better answer: implement what suggested by MadProgrammer and Andrew Thompson (see comments):

//see :https://stackoverflow.com/help/mcve
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Zeichnen extends JFrame {

    PixelManager pman;

    public Zeichnen(){
        setTitle("Tutorial");
        //setSize(400, 400); //not needed. see pack()
        pman = new PixelManager();//add pixle manager to frame
        add(pman);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack(); //Causes this Window to be sized to fit the preferred size of content
    }

    //add this method to allow adding pixels
    void addPixel(int posX, int posY, BufferedImage texture) {
            pman.addPixel(posX, posY, texture);
    }

    public static void main(String[] args)
    {
        Zeichnen fenster = new Zeichnen();

        try {
            //to make it mcve use links as suggested by Andrew Thompson
            fenster.addPixel(100, 100, ImageIO.read(new URL("http://i.stack.imgur.com/gYxHm.png")));
            fenster.addPixel(200, 200, ImageIO.read(new URL("http://i.stack.imgur.com/5v2TX.png")));
        } catch (Exception e) { e.printStackTrace(); }
    }
}

//as posinted out by MadProgrammer this class is just a data structure to hold
//pixel information. It does not need to extend JPanel
class Pixel {

    private int _posX, _posY;
    private BufferedImage _texture;

    public Pixel(int posX, int posY, BufferedImage texture) {
        _posX = posX;
        _posY = posY;
        _texture = texture;
   }

    //Getter und Setter
    public int getPosY() { return _posY;  }

    public int getPosX() { return _posX;  }

    public void setPosY(int posY) { _posY = posY; }

    public void setPosX(int posX) { _posX = posX; }

    public BufferedImage getTexture() {  return _texture;  }

    public void setTexture(BufferedImage texture) { _texture = texture; }
}

class PixelManager extends JPanel {

    private ArrayList<Pixel> pixels = new ArrayList<>();

    PixelManager() {  }

    void addPixel(int posX, int posY, BufferedImage texture) {
        pixels.add(new Pixel(posX, posY, texture));
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();

        for (Pixel pixel : pixels) {
             g2d.drawImage(pixel.getTexture(), pixel.getPosX(), pixel.getPosY(), 20, 20, null);
        }
        g2d.dispose();
    }

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

答案 2 :(得分:0)

谢谢你的回答。 是的,这是我的一个愚蠢的错误。 Pixel并不打算扩展JPanel。添加这一行是为了测试一些东西,我忘了删除它.-因为这个我没注意到getX()也是错误的。 Totaly搞砸了,因为我没有注意清理我的测试Pixel级。

谢谢你们!