Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

FlowLayout - canvas does not display

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

650 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