Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2008

Tip: Looking for answers? Try searching our database.

some kind hearted gentleman or lady, i humbly request you to please     help me on this assignment

Thread view: 
sundar.b.mani@gmail.com - 28 Apr 2008 18:06 GMT
i am posting the code i have written below, it is supposed to draw a
line on the JInternalframe and also add an origin point.

but i don't see the line or point when i run it. the code compiles and
runs perfectly fine.

some kind hearted gentleman or lady, please help me debug it. my email
id is sundar.b.mani@gmail.com plz send a note to that id as well.

kind regards,
Sundar

package testapp;

import javax.swing.*;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import java.awt.geom.Point2D.Double;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
import java.lang.*;
import java.lang.Integer;

public class TestApp extends javax.swing.JFrame
{

  /** Creates new form TestApp */
  public TestApp()
  {
      initComponents();
  }

  private void initComponents()//GEN-BEGIN:initComponents
  {
      //cenPanel = new javax.swing.JPanel();
      //cenPanel.setLayout(new java.awt.BorderLayout());
      //getContentPane().add(cenPanel, java.awt.BorderLayout.CENTER);
      desktop = new JDesktopPane();
      desktop.setLayout(new java.awt.BorderLayout());
      getContentPane().add(desktop, java.awt.BorderLayout.CENTER);
      jMenuBar1 = new javax.swing.JMenuBar();
      fileMenu = new javax.swing.JMenu();
      newFile = new javax.swing.JMenuItem();

      DrawMenu = new javax.swing.JMenu();
      Line = new javax.swing.JMenuItem();


setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
      addWindowListener(new java.awt.event.WindowAdapter()
      {
          public void windowClosing(java.awt.event.WindowEvent evt)
          {
              exitForm(evt);
          }
      });

      fileMenu.setText("File");
      DrawMenu.setText("Draw");
      newFile.setText("New");
      newFile.addActionListener(new java.awt.event.ActionListener()
      {
          public void actionPerformed(java.awt.event.ActionEvent evt)
          {
              newFileActionPerformed(evt);
          }
      });
      jMenuBar1.add(fileMenu);
      jMenuBar1.add(DrawMenu);
      setJMenuBar(jMenuBar1);

      fileMenu.add(newFile);
      DrawMenu.add(Line);

      java.awt.Dimension screenSize =
java.awt.Toolkit.getDefaultToolkit().getScreenSize();
      setBounds((screenSize.width-400)/2, (screenSize.height-300)/2,
400, 300);
  }

  private void newFileActionPerformed(java.awt.event.ActionEvent evt)
  {

          iframe = new JInternalFrame("Image Animator", true, true,
true, true);
          desktop.add(iframe, java.awt.BorderLayout.CENTER);
          //cenPanel.add(iframe, java.awt.BorderLayout.CENTER);
          Rectangle Rect = new Rectangle();

          Rect = iframe.getNormalBounds();
          int X_ULC = Rect.x;
          int Y_ULC = Rect.y;
          int Y_LLC = Y_ULC - Rect.height;

          Point2D.Double Origin = new Point2D.Double(X_ULC +
0.05*Rect.width, Y_LLC + 0.05*Rect.height);

          String OrigintoString = Origin.toString();
          Graphics Gr = iframe.getGraphics();
          Color C = new Color(255,255,255);
          Gr.setColor(C);
          int FrameWidth = Rect.width;
          int FrameHeight = iframe.getHeight();
          Integer intFW = new Integer(FrameWidth);
          Integer intFH = new Integer(FrameHeight);
          String widthinString = intFW.toString(FrameWidth);
          String heightinString = intFH.toString(FrameHeight);
          Gr.drawString(widthinString,X_ULC,Y_ULC);
          Gr.drawString(OrigintoString, (int)Origin.getX(),
(int)Origin.getY());
          Gr.drawLine(X_ULC,Y_ULC,X_ULC+Rect.width,Y_ULC-
Rect.height);
          iframe.setVisible(true);
  }

  private void exitForm(java.awt.event.WindowEvent evt)
  {
      System.exit(0);
  }

  public static void main(String args[])
  {
      TestApp TA = new TestApp();
      TA.setVisible(true);
  }

  private JInternalFrame iframe ;
  //private javax.swing.JPanel cenPanel;
  private javax.swing.JMenuBar jMenuBar1;
  private JDesktopPane desktop;
  private javax.swing.JMenu fileMenu;
  private javax.swing.JMenuItem newFile;
  private javax.swing.JMenu DrawMenu;
  private javax.swing.JMenuItem Line;

}
Andrew Thompson - 29 Apr 2008 02:51 GMT
On Apr 29, 3:06 am, sundar.b.m...@gmail.com wrote:
> i am posting the code i have written below,

Nice example.  I do not have the means to run it at
the moment, but I suspect I know what the problem
is.  Please note that the first word of every sentence,
as well as the word 'I', should always be uppercase.

>..it is supposed to draw a
> line on the JInternalframe and also add an origin point.
>
> but i don't see the line or point when i run it. the code compiles and
> runs perfectly fine.

The problem starts because the custom painting is not
being done by the paint() or paintComponent() method,
but instead in the newFileActionPerformed method.

>    private void newFileActionPerformed(java.awt.event.ActionEvent evt)
>    {
...

..and you should rarely, if ever, call for the graphics
object of a component directly.

>            Graphics Gr = iframe.getGraphics();

Instead, you might set a class level attribute..
  (boolean) isFileActioned =true;

..then, in paintComponent() (for Swing)
  if ( isFileActioned ) {
    // draw the custom graphics..
    ...

Another thing to be wary of when drawing graphics is
that they actually appear on-screen, as opposed to being
drawn to an area of the graphic that is never shown!
I'll guess for the moment that you are capable of
'adjusting the numbers' to check that.

--
Andrew T.
PhySci.org


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.