2

I have a buffered image with the sizes of my frame:

public BufferedImage img;
public static int WIDTH = 800;
public static int HEIGHT = 600;
img=new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);

How can I draw it so I can see just a black image filling the frame? without using Canvas

I want to use only the drawImage function from graphics without using the paint or paintComponent functions

If it is possible, how can i assign an 1D array [WIDTH*HEIGHT] to that image?

SIMPLY: I want to create an image ,convert the values from an array to pixels (0=black,999999999=lightblue etc.) and draw it to the screen.

EDIT:

This is the code that does not work as expected (it should be a frame with a black drawn image on it) but is just a blank frame.Why the image is not added tot the frame?

enter image description here

  import javax.swing.*;

  import java.awt.Canvas;
  import java.awt.Graphics;
  import java.awt.image.BufferStrategy;
  import java.awt.image.BufferedImage;
  import java.awt.image.DataBufferInt;

  public class test extends Canvas{

public static JFrame frame;
public static int WIDTH = 800;
public static int HEIGHT = 600;

public test(){
    
}

public static void main(String[] a){
    
        test t=new test();
        frame = new JFrame("WINDOW");
        frame.add(t);
        frame.pack();
        frame.setVisible(true);
        frame.setSize(WIDTH, HEIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        t.start();
    
}

public void start(){
    
    BufferedImage img = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);
    int[] pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
    boolean running=true;
    while(running){
        BufferStrategy bs=this.getBufferStrategy();
        if(bs==null){
            createBufferStrategy(4);
            return;
        }
        for (int i = 0; i < WIDTH * HEIGHT; i++)
            pixels[i] = 0;
        
        Graphics g= bs.getDrawGraphics();
        g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
        g.dispose();
        bs.show();
        
    }
}}
5
  • If you want a black BG, add a panel (with black BG) and set that as the content pane. If you want an image in a frame, add the image to an ImageIcon, use the icon to make a JLabel, and add the label to a frame. As an aside, it is generally better to describe the end effect or the user feature, than tell us what you "want" or "don't want". The goal is the feature, wheres you are simply focusing on a (probably flawed) strategy to achieve that goal. Jun 22, 2012 at 21:03
  • i want a buffered image on frame (an image created with code, not loaded) .I want it black because i'll modify the pixels with an array and then add the array's values to the image.For example i'll draw a rectangle using my own algorithm(i don't like to use the drawrect function)
    – Ionel Lupu
    Jun 22, 2012 at 21:07
  • All this stuff that starts with "I want" does not tell me anything I can use to help you. What is the goal? What is the feature you are offering? E.G. "Provides multilingual support" is a feature "Whiter, brighter, more sudsy" is a feature.. Jun 23, 2012 at 5:15
  • It's for a graphics engine made from scratch (using my own algorithms to draw stuff) .I'm trying to transform an array into an image and then draw that image on the screen.(Hope I was clear)
    – Ionel Lupu
    Jun 23, 2012 at 12:08
  • OK. That is more clear. For better help sooner, post an SSCCE of your best attempt. Jun 23, 2012 at 12:36

1 Answer 1

9

As far as I understand what you are trying to achieve (which is 'not a lot'), this might give you some tips. The construction of the frame and image still seems untidy to me, but have a look over this.

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.*;

public class TestImageDraw {

    public static JFrame frame;
    BufferedImage img;
    public static int WIDTH = 800;
    public static int HEIGHT = 600;

    public TestImageDraw() {
    }

    public static void main(String[] a){

        TestImageDraw t=new TestImageDraw();

        frame = new JFrame("WINDOW");
        frame.setVisible(true);

        t.start();
        frame.add(new JLabel(new ImageIcon(t.getImage())));

        frame.pack();
//      frame.setSize(WIDTH, HEIGHT);
        // Better to DISPOSE than EXIT
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    public Image getImage() {
        return img;
    }

    public void start(){

        img = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);
        int[] pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
        boolean running=true;
        while(running){
            BufferStrategy bs=frame.getBufferStrategy();
            if(bs==null){
                frame.createBufferStrategy(4);
                return;
            }
            for (int i = 0; i < WIDTH * HEIGHT; i++)
                pixels[i] = 0;

            Graphics g= bs.getDrawGraphics();
            g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
            g.dispose();
            bs.show();

        }
    }
}

General Tips

  • Please use a consistent and logical indent for code blocks.
  • Please learn common Java naming conventions (specifically the case used for the names) for class, method & attribute names & use it consistently.
  • Give test classes a meaningful name e.g. TestImageDraw.
  • Create and update Swing GUIs on the EDT.
  • Don't mix Swing & AWT components without good reason.
7
  • I'm impressed ,it works perfectly.GOOD JOB. But I tried to put this code in a separate class so it can be accessed using an object.And when I created the object for this, it calls the constructor so in the constructor I need to call the other functions(needed to rename the main) and then it creates the object again with TestImageDraw t=new TestImageDraw(); and again calls the constructor and so on.How can I use this code by importing the class and creating an object and then appears this black window?
    – Ionel Lupu
    Jun 23, 2012 at 19:36
  • I must admit I do not quite understand the reason for the call to createBufferStrategy() as opposed to using a standard BufferedImage as instantiated in the 1st code snippet. That complicates the order of setting the frame visible, packing the frame to the size of the image, and getting the image. Before anything else, I would look to change that. Jun 23, 2012 at 19:47
  • OK.Shortly: how could you give to a friend a file with this code, and when he creates an object of this class he has the same result.
    – Ionel Lupu
    Jun 23, 2012 at 19:59
  • (shrugs) It depends. Was there any real reason for using the buffer strategy to create the image? Jun 23, 2012 at 20:12
  • 2
    "you are in?" (laughs) What is your budget? If 0$, then please understand that I have a lot of projects of my own that I am more interested in. Your best bet to get collaboration is to start an open source project on SourceForge or Github or the like. Once you have an idea that interests some other people, they start using the API, you will get collaboration. Glad you got the immediate problem sorted. :) Jun 23, 2012 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.