0

I would like to know about the error that I am getting. I am trying to set individual pixels on a jframe with the bufferedimage class, but for some reason when I try to add them to the frame, I get an error saying that no suitable method has been found.

Here is my code and the error can someone please tell me how to add the bufferedimage to the frame please.

import javax.swing.JFrame;
import java.awt.image.BufferedImage;

public class gui {

  public static void main(String[] args) {
    int width = 40;
    int height = 80;
    int[] data = new int [width * height];
    JFrame frame = new JFrame("gui");
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    image.setRGB(0, 0, width, height, data, 0, width);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(image);
    frame.setSize(400, 400);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    }
}

Error:

gui.java:15: error: no suitable method found for add(BufferedImage)
    frame.add(image);
         ^
     method Container.add(Component,Object,int) is not applicable
      (actual and formal argument lists differ in length)
    method Container.add(Component,Object) is not applicable
      (actual and formal argument lists differ in length)
    method Container.add(Component,int) is not applicable
      (actual and formal argument lists differ in length)
    method Container.add(String,Component) is not applicable
      (actual and formal argument lists differ in length)
    method Container.add(Component) is not applicable
      (actual argument BufferedImage cannot be converted to Component by method invocation conversion)
    method Component.add(PopupMenu) is not applicable
      (actual argument BufferedImage cannot be converted to PopupMenu by method invocation conversion)
1 error
PakkuDon
  • 1,627
  • 4
  • 22
  • 21
user3263358
  • 11
  • 1
  • 3
  • Not 100% relational, but it should help guide you to an answer: http://stackoverflow.com/a/1065014/1786065 . `ImageIcon` wrapped in a `Container` might be another route, haven't tested it however. – Rogue Feb 04 '14 at 14:04
  • 1
    The error itself is because, as stated, there is no method that accepts that object type, however. – Rogue Feb 04 '14 at 14:05

2 Answers2

1

You can pass the BufferedImage to an ImageIcon and then pass the ImageIcon to a JLabel. Finally, add this JLabel, which contains nothing but your image, as you would any other JLabel.

Martin Dinov
  • 8,757
  • 3
  • 29
  • 41
1

As already indicated by the error message JFrame#add is undefined for a non-component such as BufferedImage. You could do

frame.add(new JLabel(new ImageIcon(image)));
Reimeus
  • 158,255
  • 15
  • 216
  • 276