2

I am working on a java 2d game library. I want a method named paintImage() to do graphics.drawImage() every time paintImage() is called.

public void paintImage(image1, x, y){        
    //i want it to run graphics.drawImage every time it is called.        
}

public void anotherMethod(){        
    paintImage(...);
    paintImage(...);
    //paint as many times as i want.        
}

public void paintComponent(Graphics graphics){
    graphics.drawImage();
    super.paintComponents();
} 

Thanks for your time and please leave a suggestion, sorry but its kind of hard to explain this.

Harmeet Singh
  • 2,555
  • 18
  • 21
Ewen
  • 1,008
  • 2
  • 16
  • 24
  • I think you are looking for constructor? – kosa Aug 14 '12 at 14:55
  • Programming is an exercise in precision and rigor, and compilers are unforgiving. You are going to be just as rigorous yourself when coding and when posting code here, especially with your capitalization and spelling since sloppy mistakes will kill your program and confuse us. Please look into fixing this. For instance it is "public" not "Public". – Hovercraft Full Of Eels Aug 14 '12 at 15:00
  • 1
    Sorry but im posting this on my kindle which has auto cap – Ewen Aug 14 '12 at 15:01
  • 1
    Please clarify your problem a bit more. Tell us exactly what you're trying to do. – Hovercraft Full Of Eels Aug 14 '12 at 17:20
  • 1
    Im making a 2d game library so that when you do paintImage() it draws the image on the jpanel – Ewen Aug 14 '12 at 18:12

2 Answers2

3

For Single Image Display

public class DrawingDemo {    
    private JPanel panel;
    private MyImage imageData;

    public DrawingDemo() {
        ...            
        panel = new JPanel() {
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (imageData != null) {
                    g.drawImage(imageData.getImage(), imageData.getX(), imageData.getY(), this);
                }

            }
        };
        ...
    }

    public void paintImage(Image image1, int x, int y) {
        imageData = new MyImage(image1, x, y);
        panel.repaint();
    }

    public void anotherMethod() {
        paintImage(...);
        paintImage(...);
    }
}

public class MyImage {    // bean class for storing image information
    private Image image;
    private int x;
    private int y;

    public MyImage(Image image, int x, int y) {
        this.image = image;
        this.x = x;
        this.y = y;
    }

    public Image getImage(){
        return image;
    }

    public int getX(){
        return x;
    }

    public int getY(){
        return y;
    }
    ... you can add setter methods
}

UPDATE : For multiple image display

    private JPanel panel;
    private ArrayList<MyImage> imageData; // or any other data structure you like

    public DrawingDemo() {
        imageData = new ArrayList<>();
        JFrame frame = new JFrame();
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel = new JPanel() {

            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                for (MyImage myImage : imageData) {
                    g.drawImage(myImage.getImage(), myImage.getX(), myImage.getY(), this);
                }
            }
        };
        frame.add(panel);
        frame.setVisible(true);

    }

    public void paintImage(Image image1, int x, int y) {
        imageData.add(new MyImage(image1, x, y));
        panel.repaint();
    }

    public void anotherMethod() {
        paintImage(new ImageIcon("/home/blackadmin/Desktop/image.jpg").getImage(), 0, 0);
        paintImage(new ImageIcon("/home/blackadmin/Desktop/image2.jpg").getImage(), 50, 50);
        paintImage(new ImageIcon("/home/blackadmin/Desktop/image3.jpg").getImage(), 100, 100);
    }

OUTPUT : enter image description here Have a look at this answer
Comment if you don't understand anything, hope this will help

Community
  • 1
  • 1
Harmeet Singh
  • 2,555
  • 18
  • 21
1

What I think you're looking to do is to make changes to some states in your class and then redrawing your images with changes based on those state changes -- in other words perhaps you're looking to do animation. If so, then your image drawing should all be done either within the paintComponent method using its Graphics object, or in another method called by paintComponent one that uses the Graphics object passed into paintCocalzmponent. This can be done by passing a Graphics parameter into the other method. Your anotherMethod would then request that the JVM repaint the GUI by calling repaint(). For example:

public void anotherMethod() {
    x++;
    y++;
    repaint(); // this will stimulate JVM to call paint/paintComponent
}

private void paintImage(Graphics g, BufferedImage img, int x, int y2) {
    g.drawImage(img, x, y2, this);
}

protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    paintImage(g, image1, x, y);
}

A complete example of this is as follows:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.beans.Transient;

import javax.swing.*;

public class PaintEg extends JPanel {

    private static final int IMG_W = 30;
    private static final int IMG_H = IMG_W;
    private static final int PREF_W = 400;
    private static final int PREF_H = PREF_W;
    private static final int TIMER_DELAY = 20;
    private BufferedImage image1;
    private int x;
    private int y;

    public PaintEg() {
        image1 = createImg();
        new Timer(TIMER_DELAY, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                anotherMethod();
            }
        }).start();
    }

    private BufferedImage createImg() {
        BufferedImage img = new BufferedImage(IMG_W, IMG_H, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = img.createGraphics();
        g2.setBackground(Color.red);
        g2.clearRect(0, 0, IMG_W, IMG_H);
        g2.setColor(Color.blue);
        g2.fillRect(IMG_W / 4, IMG_H / 4, IMG_W / 2, IMG_H / 2);
        g2.dispose();
        return img;
    }

    public Dimension getPreferredSize() {
        return new Dimension(PREF_W, PREF_H);
    }

    public void anotherMethod() {
        x++;
        y++;
        repaint(); // this will stimulate JVM to call paint/paintComponent
    }

    private void paintImage(Graphics g, BufferedImage img, int x, int y2) {
        g.drawImage(img, x, y2, this);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        paintImage(g, image1, x, y);
    }


    private static void createAndShowGUI() {
        PaintEg paintEg = new PaintEg();

        JFrame frame = new JFrame("PaintEg");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(paintEg);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373