0

I am playing around with the Graphics class. Here is my code for drawing an image at a certain location:

    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);
    }
}

Explanation: I am using this class as kind of an Pixel or Texture Manager. In the ArrayList I am saving some Pixels, which include a position and a texture. in de paintComponent Method I am iterating through the ArrayList and trying to draw them.

Pixel class:

   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;
    }
}

And the JFrame extending class:

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();
        }
       }
}

So here is the problem. The two images will be drawn to the panel/frame but ALWAYS to the postion x=0,y=0. I debuged everything until this line:

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

The values provided by pixel are correct. drawImage will receive the correct position and the correct texture/Image.

Becaus I am new to Graphics I would like to ask you guys helping me out with this problem.

Big Thanks in advance!

Greetz Cronax3

c0der
  • 18,467
  • 6
  • 33
  • 65
  • 1
    1) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. 2) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 3) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 4) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Feb 03 '18 at 05:15

3 Answers3

0

So, your basic problem comes down to...

public static class Pixel extends JPanel {

Why does Pixel need to extend from JPanel?

If you remove extends JPanel, it will trigger a compile error, saying "can not find symbol getX" etc, but it's not the method you had defined, getPosX was.

getX is a property of JPanel, but since I can't see any need to use JPanel in this way, there's simply not need for it. In fact, you could have Pixel just paint itself, but that's a discussion for another day

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
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("https://i.stack.imgur.com/gYxHm.png")));
            fenster.addPixel(200, 200, ImageIO.read(new URL("https://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);  }
}
c0der
  • 18,467
  • 6
  • 33
  • 65
0

Thanks guys for your answers. Yeah, thats a dumb mistake by me. It was not intended that Pixel extends JPanel. This line was added for testing some stuff and I forgot to delete it -.- Because of this I didnt notice that getX() is wrong, too. Totaly messed up because I didnt take care of cleaning my test Pixel-class.

Thank you guys!