1

I have many planes(threads) that move in window, and I want switch the ImageIcon according to the direction of the plane. For example: if a plane goes to the right, the imageIcon of the plane is right and then plane goes to the left, exchange the imageIcon for the plane is left. How can I do that in method paintComponent? Sorry for my bad english.

  • 1
    See [flip an image horizontally](http://stackoverflow.com/questions/13742365/how-do-i-flip-an-image-horizontally-flip-with-glreadpixels-bufferedimage-and-o/13756357#13756357) for part of it. But do it at start-up, rather than in `paintComponent(..)`. What part are you having trouble with? – Andrew Thompson Dec 10 '12 at 02:25

3 Answers3

3

If you're talking about swapping the ImageIcon displayed by a JLabel, then you should not switch ImageIcons in paintComponent but rather should do this in the non-paintComponent region of code, perhaps in a Swing Timer. Even if you're not talking about a JLabel, the paintComponent method should not be used for changing the state of an object.

Your question however leaves too much unsaid to allow us to be able to answer it completely and well. Consider telling and showing more.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
3

If you are looking for a logic thingy, then a small example is here, though you might have to modify it for your needs.

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.*;

public class FlyingAeroplane
{
    private Animation animation;
    private Timer timer;
    private ActionListener timerAction = new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent ae)
        {
            animation.setValues();
        }
    };

    private void displayGUI()
    {
        JFrame frame = new JFrame("Aeroplane Flying");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        animation = new Animation();
        frame.setContentPane(animation);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
        timer = new Timer(100, timerAction);
        timer.start();
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new FlyingAeroplane().displayGUI();
            }
        });
    }
}

class Animation extends JPanel
{
    private final int HEIGHT = 150;
    private final int WIDTH = 200;
    private int x;
    private int y;
    private ImageIcon image;
    private boolean flag;
    private Random random;

    public Animation()
    {
        x = 0;
        y = 0;
        image = new ImageIcon(getClass().getResource("/image/aeroplaneright.jpeg"));
        flag = true;
        random = new Random();
    }

    public void setValues()
    {
        x = getXOfImage();
        y = random.nextInt(70);
        repaint();
    }

    private int getXOfImage()
    {
        if (flag)
        {           
            if ((x + image.getIconWidth()) == WIDTH)
            {
                flag = false;
                x--;
                return x;
            }
            x++;
            image = new ImageIcon(getClass().getResource("/image/aeroplaneright.jpeg"));
        }
        else if (!flag)
        {
            if (x == 0)
            {
                flag = true;
                x++;
                return x;
            }
            x--;
            image = new ImageIcon(getClass().getResource("/image/aeroplaneleft.jpeg"));
        }
        return x;
    }

    @Override
    public Dimension getPreferredSize()
    {
        return (new Dimension(WIDTH, HEIGHT));
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(image.getImage(), x, y, this);
    }
}

IMAGES USED :

AEROPLANERIGHT AEROPLANELEFT

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • Please do read how to [add Images to your Project manually](http://stackoverflow.com/a/11372350/1057230), or this [thread](http://stackoverflow.com/a/9866659/1057230) – nIcE cOw Dec 10 '12 at 05:01
2

On setting the direction you should set the image icon too, or have a getImageIcon(direction).

In the paintComponent no heavy logic should happen; it should be as fast as possible. You have no (total) control when and how often paintComponent is called.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138