Solved

FlowLayout - canvas does not display

Posted on 1999-01-19
3
421 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

717 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