lechniak
lechniak

Reputation: 87

What is wrong with how I'm trying to draw an image?

I have got problem with my project:/

Why drawPac isn't working correctly. Black rect is drawing, but my image not:/ why

I made 4 classes, here is cod of 3 classes, without main class which extends JFrame, and there is added JPanel Game.

File #1

package pacman;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;

public class Game extends JPanel { 

   Pac pacman = new Pac();

   public void Game() {
     setFocusable(true);
     setBackground(Color.BLACK);
     setDoubleBuffered(true);
   }

@Override
public void paint(Graphics g){
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.black);
    g2d.fillRect(0, 0,this.getWidth(),this.getHeight());
    drawPac(g2d);
}

public void drawPac(Graphics2D g2d){
    g2d.drawImage(pacman.image, pacman.x, pacman.y, 100, 100, this);
    }
}

File #2

package pacman;
import java.awt.Image;
import javax.swing.ImageIcon;

public class Actor {
    int x,y;
    int dv;
    Image image;
    public void Actor(){
    }
}

File #3

package pacman;
import pacman.Game;
import javax.swing.ImageIcon;
import java.awt.Graphics2D;

public class Pac extends Actor {

public void Pac(){

    try{
    image = new ImageIcon(Pac.class.getResource("../img/Pac00.gif")).getImage(); 
    x=0;
    y=0;
        }
    catch (Exception e){
        System.out.println("Blad prz otwieraniu");
        System.exit(0);  }
 }

}

Upvotes: 1

Views: 209

Answers (2)

hekomobile
hekomobile

Reputation: 1388

Ok @lechniak update Game class

public class Game extends JPanel { 
 Pac pacman = new Pac();

public Game() {
  setFocusable(true);
  setBackground(Color.BLACK);
  setDoubleBuffered(true);
}
@Override
public void paint(Graphics g){
   super.paint(g);
   Graphics2D g2d = (Graphics2D) g;
   g2d.setColor(Color.black);
   g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
   drawPac(g2d);
}

public void drawPac(Graphics2D g2d){
   g2d.drawImage(pacman.image, pacman.x, pacman.y, 200, 200, this);
}

public static void main(String[] args) {
     JFrame f = new JFrame("Image Draw");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new Game());
        f.setSize(350,300);
        f.setLocation(300,300);
        f.setVisible(true);
 }
}

And Pac class

//import javax.swing.ImageIcon;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import javax.imageio.ImageIO;
import java.io.*;

public class Pac /*extends Actor*/ {
   int x = 0;
   int y = 0;
   BufferedImage  image;

       public Pac() {
          try {
            image = ImageIO.read(new File("/Users/hlozano/java/swing/programmerBorn.jpg"));
            //new ImageIcon(Pac.class.getResource("../img/Pac00.gif")).getImage();
            x = 0;
            y = 0;
           } catch (Exception e) {
            System.out.println("Blad prz otwieraniu " + e);
            System.exit(0);
               }
       }
}

And image output

enter image description here

I hope these helps.

Upvotes: -1

davidbuzatto
davidbuzatto

Reputation: 9424

1 - The "Pac" class should be responsible for draw itself, don't you agree?

2 - For JPanels and every JComponent's sons, you should override the paintComponent method, not paint, that is responsible to delegate the painting work to paintComponent, paintBorder and paintChildren. You should call the super version of paintComponent too, as you are doing in paint. Take a look at the documentation.

3 - In most of the cases, is recomended to create a new graphics context (based on the original). So, your line:

Graphics2D g2d = (Graphics2D) g;

Should be replaced to:

Graphics2D g2d = (Graphics2D) g.create();

And you need to dispose (g2d.dispose()) this new context after using it (at the last line of the paintComponent).

4 - Is the code of the Pac class correct? Is it compiling? The image field is a member of Actor?

I think you have some problems in your code that you not showed in your question...

Upvotes: 2

Related Questions