There is a simpler way.  JFrame's paint method is responsible for painting
the menus and the area inside the frame. So when you  override the paint
method, the menus do not draw correct. What you should do is draw your
BufferedImage on a JPanel and add the JPanel to the JFrame. I attached a
working version of your code.

At 04:51 PM 7/18/01 -0400, Michael Grouse wrote:
>Hi:
>
>I'm just getting started with Java 2D and am having problems getting a very
>simple example app to work.
>
>All it does is try to open an image and display it in a window. The window
>has also been assigned a menu bar. The problem I'm having is with the paint
>(). Depending on the order of the calls in the paint() and the repaint(),
>the BufferedImage and the Menu create various artifacts. Either the menu
>will be painted over, and only show up Item by Item, when one holds down
>the mouse button and moves the mouse around, or the (blue) background of
>the JPanel will show up when the SubMenu's are un-drawn.
>
>I'm considering that I might need to write new paint() handlers for the
>Frame class (so it can re-draw the Buffered-Image) or some such, but I
>thought I would ask to see if there was a far simpler way that I was
>missing.
>
>I've attatched some code just in case my dilema wasn't clear from my
>description
>
>Thanks
>Michael(See attached file: App2.java)
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import java.awt.Toolkit.*;
import javax.swing.*;
import java.net.*;

public class App2 extends javax.swing.JFrame {

  /** Creates new form App */
  public App2() {
                initComponents ();
                setSize(640,480);
  }

        JPanel paintPanel = new JPanel() {
    public void paint(Graphics g) {
      g.drawImage(theScreen, 0, 0, this);
    }
  };

  public void run(String s) {
    //once again this is a little too big. should subtract Insets
    theScreen = new BufferedImage(640,480, BufferedImage.TYPE_INT_RGB);
    //get Graphics2D
    Graphics2D buffG2D = (Graphics2D)theScreen.getGraphics();
    URL file = null;
    this.setTitle(s);
    // load the gif and paint at 0,0
    try{
      //file = new URL("/images/java_logo.gif");
      Toolkit Tk = Toolkit.getDefaultToolkit();
      Image img = Tk.getImage("images/clouds.jpg");
      MediaTracker tracker = new MediaTracker(this);
      tracker.addImage(img, 1);
      tracker.waitForID(1);

      int width = img.getWidth(this);
      int height = img.getHeight(this);

      buffG2D.setColor(Color.cyan);
      buffG2D.fillRect(0, 0, 7, 7);
      buffG2D.draw(new Line2D.Double(100, 100, 200, 200));

      //draw image to buffered image
      buffG2D.drawImage(img, 0, 0, this);
    } catch (Exception e) {
        e.printStackTrace();
    }
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the FormEditor.
   */
  private void initComponents () {
      menuBar = new javax.swing.JMenuBar ();
      fileMenu = new javax.swing.JMenu ();
      openMenuItem = new javax.swing.JMenuItem ();
      saveMenuItem = new javax.swing.JMenuItem ();
      saveAsMenuItem = new javax.swing.JMenuItem ();
      exitMenuItem = new javax.swing.JMenuItem ();
      editMenu = new javax.swing.JMenu ();
      cutMenuItem = new javax.swing.JMenuItem ();
      copyMenuItem = new javax.swing.JMenuItem ();
      pasteMenuItem = new javax.swing.JMenuItem ();
      deleteMenuItem = new javax.swing.JMenuItem ();
      helpMenu = new javax.swing.JMenu ();
      contentsMenuItem = new javax.swing.JMenuItem ();
      aboutMenuItem = new javax.swing.JMenuItem ();

        fileMenu.setText ("File");

          openMenuItem.setText ("Open");
          fileMenu.add (openMenuItem);

          saveMenuItem.setText ("Save");
          fileMenu.add (saveMenuItem);

          saveAsMenuItem.setText ("Save As ...");
          fileMenu.add (saveAsMenuItem);

          exitMenuItem.setText ("Exit");
          exitMenuItem.addActionListener (new java.awt.event.ActionListener () {
              public void actionPerformed (java.awt.event.ActionEvent evt) {
                  exitMenuItemActionPerformed (evt);
              }
          }
          );

          fileMenu.add (exitMenuItem);
        menuBar.add (fileMenu);
        editMenu.setText ("Edit");

          cutMenuItem.setText ("Cut");
          editMenu.add (cutMenuItem);

          copyMenuItem.setText ("Copy");
          editMenu.add (copyMenuItem);

          pasteMenuItem.setText ("Paste");
          editMenu.add (pasteMenuItem);

          deleteMenuItem.setText ("Delete");
          editMenu.add (deleteMenuItem);

        menuBar.add (editMenu);
        helpMenu.setText ("Help");

          contentsMenuItem.setText ("Contents");
          helpMenu.add (contentsMenuItem);

          aboutMenuItem.setText ("About");
          helpMenu.add (aboutMenuItem);
        menuBar.add (helpMenu);

      //eventually the View manager should take care of this?
      addWindowListener (new java.awt.event.WindowAdapter () {
        public void windowClosing (java.awt.event.WindowEvent evt) {
            exitForm (evt);
        }
      });
      setJMenuBar(menuBar);
      setContentPane(paintPanel);
      setResizable(false);
  }

  private void exitMenuItemActionPerformed (java.awt.event.ActionEvent evt) {
    System.exit (0);
  }

  /** Exit the Application */
  private void exitForm(java.awt.event.WindowEvent evt) {
    System.exit (0);
  }

  /**
  * @param args the command line arguments
  */
  public static void main (String args[]) {
          App2 app = new App2();
          app.show ();
          app.run("test");
  }

  // Variables declaration - do not modify
  private javax.swing.JMenuBar menuBar;
  private javax.swing.JMenu fileMenu;
  private javax.swing.JMenuItem openMenuItem;
  private javax.swing.JMenuItem saveMenuItem;
  private javax.swing.JMenuItem saveAsMenuItem;
  private javax.swing.JMenuItem exitMenuItem;
  private javax.swing.JMenu editMenu;
  private javax.swing.JMenuItem cutMenuItem;
  private javax.swing.JMenuItem copyMenuItem;
  private javax.swing.JMenuItem pasteMenuItem;
  private javax.swing.JMenuItem deleteMenuItem;
  private javax.swing.JMenu helpMenu;
  private javax.swing.JMenuItem contentsMenuItem;
  private javax.swing.JMenuItem aboutMenuItem;

  private BufferedImage theScreen;

}

Reply via email to