?
Solved

FlowLayout - canvas does not display

Posted on 1999-01-19
3
Medium Priority
?
422 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 200 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

762 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