Solved

FlowLayout - canvas does not display

Posted on 1999-01-19
3
414 Views
Last Modified: 2008-03-03
i am unable to display a canvas on a panel with a FlowLayout.
If i use a GridLayout for the panel, the canvas appears.
the .jpg file is smaller than the size of the frame.
What is the problem?  test code follows.
you will need a file called /pix/test.jpg to run the code as it is.

---------------------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
//
//  FlowLayoutTest
//
//  experiment to get canvas to appear in flowlayout
//
////////////////////////////////////////////////////////////////////////////////

import utilities.GenericCanvas;

import java.awt.*;
import java.awt.event.*;

////////////////////////////////////////////////////////////////////////////////
public class FlowLayoutTest extends Frame {
 
  private Panel mainPnl;           // create panel
 
  //  constructor that creates the frame and menu ------------------------------
  public FlowLayoutTest()
  {
    super (" FlowLayoutTest");  // call Frame constructor, pass in title

    mainPnl  = new Panel();           // create panel
   
    mainPnl.setLayout(new FlowLayout());      // canvas does not appear
//    mainPnl.setLayout(new GridLayout(1,1)); // canvas appears when this is used

    Button testBtn = new Button ("on panel");
    GenericCanvas cnvs = new GenericCanvas(); // text, r,c
   
    //mainPnl.add (new Button ("hello"));  // this appears with either GridLayout or FlowLayout
    mainPnl.add (cnvs);  

    this.enableEvents(AWTEvent.WINDOW_EVENT_MASK);

    add (mainPnl); // add panel to dialog

} // end constructor FlowLayoutTest


  // main method used to start application --------------------------------------
  public static void main (String args[])
  {
    FlowLayoutTest f = new FlowLayoutTest(); // instantiate self
    f.setSize (450,300);
    f.show();

  } // end main

  public void processWindowEvent(WindowEvent e){
    if (e.getID()==WindowEvent.WINDOW_CLOSING)
      System.exit(0);
    else super.processWindowEvent(e); // pass unhandled events to superclass, but not necessary
  }  // end processWindowEvent

} // end FlowLayoutTest

------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
//
//  Class GenericCanvas
//
////////////////////////////////////////////////////////////////////////////////
package utilities;

import java.awt.Canvas;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.MediaTracker;
import java.awt.Toolkit;

public class GenericCanvas extends Canvas
{
  Image image;

  static final private String pictureDir = "\\pix\\";

  // constructor -----------------------------------------------------------
  public GenericCanvas()
  {
    super();

    loadImage();

  } // end constructor


  // load the picture into Image object ------------------------------------------
  public void loadImage()
  {
    String sUserDir = System.getProperty("user.dir");

    Toolkit toolkit = getToolkit();

    image = toolkit.getImage (sUserDir + pictureDir + "test.jpg");

    MediaTracker tracker = new MediaTracker (this);
    tracker.addImage (image,8);

    try  { tracker.waitForID(8); }
    catch ( InterruptedException ex ) {} // end try/catch

  } // end loadImage

  // load the picture into Image object ------------------------------------------
  public void loadImage(String filenameString)
  {
    String sUserDir = System.getProperty("user.dir");

    Toolkit toolkit = getToolkit();

    image = toolkit.getImage (sUserDir + pictureDir + filenameString);

    MediaTracker tracker = new MediaTracker (this);
    tracker.addImage (image,8);

    try  { tracker.waitForID(8); }
    catch ( InterruptedException ex ) {} // end try/catch

  } // end loadImage


  // paint ----------------------------------------------------------
  public void paint(Graphics g)
  {
      g.drawImage(image, 0, 0, this);

  } // end paint

} // end class GenericCanvas

-----------------------------------------------------------

0
Comment
Question by:mickeydog
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
heyhey_ earned 50 total points
ID: 1234282
you'll have to implement
at least getPreferredSize() method in your GenericCanvas.
 public Dimension getPreferredSize()


(probably getMinimumSize/getMaximumSize too)

at the moment your Canvas has prefferred size 0x0 so FlowLayout makes it exactly 0x0 big

best regards
  heyhey

0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1234283
something like this should work

public Dimension getPreferredSize()
{
      if (image != null)
            return new Dimension(image.getWidht(null), image.getHeight(null));
      else
            return new Dimension(10, 10);
}

0
 

Author Comment

by:mickeydog
ID: 1234284
thanks, that was the problem.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question