0

This is all I have so far. I have read the oracle documentation on drawing and creating images, But I still cant figure it out.

        final BufferedImage image = ImageIO.read(new File("BeachRoad_double_size.png"));

    final JPanel pane = new JPanel();

    frame.add(pane);


    int delay = 1000; //milliseconds
    ActionListener taskPerformer = new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            Graphics gg = image.getGraphics();
            System.out.println("sdfs");
            pane.paintComponents(gg);
            //g.drawImage(image, 0, 0, null);
        }
    };
    new Timer(delay, taskPerformer).start();
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Saucymeatman
  • 587
  • 3
  • 6
  • 8
  • Start by taking a look at [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/), [Painting in AWT and Swing](Painting in AWT and Swing) and [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) – MadProgrammer Jul 31 '13 at 21:14
  • You could also take a look at [this example](http://stackoverflow.com/questions/14432816/how-to-move-an-image-animation/14436331#14436331) which shows how to render an image and simple animation – MadProgrammer Jul 31 '13 at 21:16
  • Don't invoke the painting rethods directly. You should be invoking repaint() on the component. Post your `SSCCE` demonstrating the problem. – camickr Jul 31 '13 at 21:17
  • Thanks! Ill take a look at both of those. – Saucymeatman Jul 31 '13 at 21:17

1 Answers1

3
  1. Painting is the responsibility to of the Swing framework. It decides what, when and how much should be painted. You can make requests to the system to perform updates, but it will be up to the sub system to decide when it will be executed.
  2. You should never have a need to call paintComponent directly, in fact, you should never have a need to call paint directly
  3. Your example is actually painting the component to the image.

Instead. Create a custom component, say from something like JPanel, override it's paintComponent method and perform all you custom painting there...

public class ImagePane extends JPanel {
    private BufferedImage bg;

    public ImagePane(BufferedImage bg) {
        this.bg = bg;
    }

    public Dimension getPreferredSize() {
        return bg = null ? new Dimension(200, 200) : new Dimension(bg.getWidth(), bg.getHeight());
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (bg != null) {
            g.drawImage(bg, 0, 0, this);
        }
    }
}

Take a look at

For more details

nudelchef
  • 3
  • 2
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366