简体   繁体   中英

Drawing imageS in java using AWT

EDIT: First question solved, see Roberto Attias 's answer and maybe read the comment . Still there's the second issue.

I have to do a small 2D game in Java and I want to stick to AWT as much as possible, ( so no SWING nor Java2D unless absolutely necessary).

I have a window and I can draw an image on it but there's two issues. First I can't draw more than one Image. In fact with some of my test when in debug I can see that my program will draw my two images only to delete them and re-draw the first image. Second, that first image which is re-drawn is not at the coordinate it should ( its slightly on the left and on below )

For now I have something like that:

public class AwtManager {
    private Frame frame;
    private Panel panel;
    public AwtManager(){
        frame = new Frame("a");
        frame.setSize(800,600);
        frame.setLocation(100, 100);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowEnvent){
                System.exit(0);
            }
        });

    panel = new Panel();
    // here I tried to setBounds to solve my second issue, it didn't work
    //panel.setBounds(0, 0, 800, 600);
    frame.add(panel);
    frame.setVisible(true);
    }

This part open my windows and works quite nicely but my second issue seems to be caused by the borders of my frame / panel so there might be some changement to do here.

    public void showMytwoImagesFFS(ArrayList<ImageComponent> images){
        for (int i = 0; i < images.size(); i++) {
            panel.add(images.get(i);
            //And this is where I'm lost

        }
        // or here maybe
        frame.setVisible(true);
    }
}

In this second part I tried every combination of Component.paint(g), Component.update(g), Component.repaint(), Component.setVisible(true) I could think of.

My object ImageComponent is simply this:

public class ImageComponent extends Component {
    private static final long serialVersionUID = 1L;
    private BufferedImage img;
    private int x;
    private int y;

    public ImageComponent(String path,int x, int y){
        try{
            img = ImageIO.read(new File(path));
            this.x = x;
            this.y = y;
        }
        catch (IOException e){
            e.printStackTrace();
        }
    }

This function getPreferredSize() disturbs me like hell, it should return the preferred size of the ImageComponent but apparently I have to put the size of my frame/ panel otherwise it won't work.

    public Dimension getPreferredSize(){
        return new Dimension(800,600);
    }

And finally my paint, update, repaint:

    public void paint(Graphics g){
        g.drawImage(img, x, y,null);    
    }

    public void update(Graphics g){
        super.update(g);
    }

    public void repaint(){
        this.getGraphics().drawImage(img, x, y, null);
    }
}

I highly doubt that those three look like what they should but I find the documents on the subject very hard to understand.

So pleas, could you help me with those issues, and by the way if you know how Component.setVisible(boolean) works i would like to know because in debug mod, this function made me loose some hair.

EDIT:

Here's a screenshot of my window knowing that I asked for two red square (there are Images not Rectangle), one a 0,0, the other at 200, 200.

我的程序结果

EDIT 2: Here's a fully runnable code (i think):

import java.awt.*;
import java.util.*;
import java.awt.event.*;

public class AwtManager {
    private Frame frame;
    private Panel panel;

    public static void main(String args[]) {
        new AwtManager();
        ArrayList<ImageComponent> images = new ArrayList<>();
        images.add(new ImageComponent("myimage.jpg", 0, 0));
        images.add(new ImageComponent("myimage.jpg", 200, 200));
        showMytwoImagesFFS(images); 
    }  


    public AwtManager(){
        frame = new Frame("a");
        frame.setSize(800,600);
        frame.setLocation(100, 100);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowEnvent){
                System.exit(0);
            }
        });

        panel = new Panel();
        frame.add(panel);
        frame.setVisible(true);
    }

    public void showMytwoImagesFFS(ArrayList<ImageComponent> images){
        for (int i = 0; i < images.size(); i++) {
            panel.add(images.get(i));
        }
        frame.setVisible(true);
    }
}

import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
public class ImageComponent extends Component {
    private static final long serialVersionUID = 1L;
    private BufferedImage img;
    private int x;
    private int y;

    public ImageComponent(String path,int x, int y){
        try{
            img = ImageIO.read(new File(path));
            this.x = x;
            this.y = y;
        }
        catch (IOException e){
            e.printStackTrace();
        }
    }

    public Dimension getPreferredSize(){
        return new Dimension(800,600);
    }

    public void paint(Graphics g){
        g.drawImage(img, x, y,null);    
    }

    public void update(Graphics g){
        super.update(g);
    }

    public void repaint(){
        this.getGraphics().drawImage(img, x, y, null);
    }
}

Just a quick thought, but do you know the visibility of each of your images in that ArrayList? Ie

public void showMytwoImagesFFS(ArrayList<ImageComponent> images){
    for (int i = 0; i < images.size(); i++) {
        images.get(i).setVisible(true);//is the visibility actually true?
        panel.add(images.get(i);

    }
    // or here maybe
    frame.setVisible(true);
}

Also, do you have screenshots of how your program looks at the moment? With gui problems, I find they're easier to solve if I can see what's going on.

This is your code, slightly modified to run. In the future, please post fully runnable examples.

import java.awt.*;
import java.util.*;
import java.awt.event.*;

public class AwtManager {
    private Frame frame;
    private Panel panel;

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


    public AwtManager(){
        frame = new Frame("a");
        frame.setSize(800,600);
        frame.setLocation(100, 100);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowEnvent){
                System.exit(0);
            }
        });

    panel = new Panel();
    // here I tried to setBounds to solve my second issue, it didn't work
    //panel.setBounds(0, 0, 800, 600);
    frame.add(panel);
    ArrayList<ImageComponent> images = new ArrayList<>();
    images.add(new ImageComponent("myimage.jpg", 0, 0));
    showMytwoImagesFFS(images);
    frame.setVisible(true);
  }

   public void showMytwoImagesFFS(ArrayList<ImageComponent> images){
        for (int i = 0; i < images.size(); i++) {
            panel.add(images.get(i));
        }
    }
}

import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;

public class ImageComponent extends Component {
    private static final long serialVersionUID = 1L;
    private BufferedImage img;
    private int x;
    private int y;

    public ImageComponent(String path,int x, int y){
        try{
            img = ImageIO.read(new File(path));
            this.x = x;
            this.y = y;
        }
        catch (IOException e){
            e.printStackTrace();
        }
    }

   public Dimension getPreferredSize(){
        return new Dimension(800,600);
    }
   public void paint(Graphics g){
        g.drawImage(img, x, y,null);
    }

    public void update(Graphics g){
        super.update(g);
    }

    public void repaint(){
        this.getGraphics().drawImage(img, x, y, null);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM