Solved

FlowLayout - canvas does not display

Posted on 1999-01-19
3
411 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
Comment Utility
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_
Comment Utility
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
Comment Utility
thanks, that was the problem.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Python Assistance 7 31
What is the latest versions eclipse neon 2 116
eclipse formatting 6 38
Java JRE greater than 1.6 5 0
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now