Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Post Drawing BufferedImage or Graphics2D to window

    Basically what I want to do is make a window and show the created image in the window. I don't know anything about GUIs, and searched around, finding nothing that worked. This method is called every time the data is finished processing, and optimally I would use this method to draw the created BufferedImage or Graphics2D to the window.
    Thanks for your help.
    Java Code:
    	public static void draw(int frame) throws Exception
    	{
    		double[] center = new double[2];
    		double x, y;
    		BufferedImage graphicsObj = new BufferedImage(vars.width, vars.height, BufferedImage.TYPE_INT_RGB);
    		Graphics2D img = graphicsObj.createGraphics();
    		for (int i = 0; i < getLastFull(); i++)
    		{
    			if (vars.particleList[i].mass != 0)
    			{
    				img.setColor(new Color((int)vars.particleList[i].color[0],(int)vars.particleList[i].color[1], (int)vars.particleList[i].color[2]));
    				center = convert(vars.particleList[i].position, vars.height, vars.width);
    				x = center[0] - vars.particleList[i].radius;
    				y = center[1] - vars.particleList[i].radius;
    				img.fill(new Ellipse2D.Double(x, y, vars.particleList[i].radius * 2, vars.particleList[i].radius * 2));
    				continue;
    			}
    			break;
    		}
    		write(graphicsObj, frame);
    		img.dispose();
    	}

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    what I want to do is make a window and show the created image in the window.
    There is a Window class in Java. You could create an instance of it, override its paint method and draw the created image there. The paint method is called with a reference to a Graphics object which you could use to do your drawing.

  3. #3
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default

    Allright. I created a new Window, and I'm assuming that I had to create a new Frame because that was one of the required arguements. So my code looks like this
    In the vars class:
    Java Code:
    static Frame graphicsFrame = new Frame("Window");
    static Window window = new Window(graphicsFrame);
    I figured I had to set the size and such, so there is some init code before the main loop:
    Java Code:
    vars.graphicsFrame.setVisible(true);
    vars.graphicsFrame.setEnabled(true);
    vars.graphicsFrame.setSize(vars.width, vars.height);
    You said that there was a paint method, so I called that in the draw method.
    Java Code:
    vars.window.paint(img);
    vars.window.update(img);
    This produces a grey window and nothing happens. The code continues to run as expected, and the paint calls are happening. What am I doing wrong?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,304
    Rep Power
    20

    Default

    Forget about what you've done so far and go through this tutorial: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,304
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    There is a Window class in Java. You could create an instance of it, override its paint method and draw the created image there. The paint method is called with a reference to a Graphics object which you could use to do your drawing.
    Nope. Please see the tutorial liked in the previous post.

    db

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,304
    Rep Power
    20

    Default

    And by far the easiest method to display an already loaded image is
    Java Code:
    JOptionPane.showMessageDialog(null, new ImageIcon(image));
    db

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    This code seems to create a window that displays an image:
    Java Code:
       Window   win = new Window(new Frame()){
         java.awt.image.BufferedImage img = javax.imageio.ImageIO.read(new File("http://www.java-forums.org/images/duke.gif"));
         public void paint(Graphics g) {
           g.drawImage(img, 0,0, null);
         }
       };
       win.setSize(200, 200);
       win.setVisible(true);
    this works, but is certainly not the recommended way.
    Last edited by Norm; 06-16-2011 at 02:38 PM.

Similar Threads

  1. Open a new window and drawing map into it
    By cselic in forum AWT / Swing
    Replies: 2
    Last Post: 08-27-2010, 11:23 AM
  2. creating a BufferedImage from a drawing
    By chappa in forum Java 2D
    Replies: 2
    Last Post: 01-10-2010, 06:04 PM
  3. Callback when window is done drawing/loading?
    By rodrigo in forum SWT / JFace
    Replies: 0
    Last Post: 08-21-2009, 02:01 AM
  4. drawing window
    By BlitzA in forum New To Java
    Replies: 1
    Last Post: 01-15-2009, 12:55 PM
  5. drawing window
    By BlitzA in forum Advanced Java
    Replies: 0
    Last Post: 12-30-2007, 05:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •