Java - 使用方法和构造函数绘制图形

时间:2012-08-14 14:54:19

标签: java graphics

我正在开发一个java 2d游戏库。每次调用paintImage()时,我都希望名为graphics.drawImage()的方法执行paintImage()

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

感谢您的时间,请留下一个建议,抱歉,但有点难以解释。

2 个答案:

答案 0 :(得分:3)

单张图片显示器

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
}

更新:用于多张图片显示

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

输出 enter image description here 看看这个answer
评论如果你不理解任何事情,希望这会有所帮助

答案 1 :(得分:1)

我认为你要做的是对你班级中的某些状态进行更改,然后根据这些状态更改重新绘制图像 - 换句话说,也许你正在寻找动画。如果是这样,那么您的图像绘制应该在paintComponent方法中使用其Graphics对象完成,或者在paintComponent调用的另一个方法中完成,该方法使用传递给paintCocalzmponent的Graphics对象。这可以通过将Graphics参数传递给另一个方法来完成。然后,您的anotherMethod将通过调用repaint()请求JVM重新绘制GUI。例如:

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

完整的例子如下:

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