Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Drawing a string without use of Main(String[] args)

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Drawing a string without use of Main(String[] args)

    Basically I have this code and another program that rotates text around the screen and I'm trying to merge them. How do I draw a string to this code without the use of something like

    public static void main( String args[] ) //
    { //
    (new ViewOfDisplay()).setVisible(true); // Start application
    }

    I can't change the barebones of the code, just add to

    package Clients;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
     
    import java.util.List;
    import java.util.Map;
    import java.util.Observer;
    import java.util.Observable;
     
    import Middle.*;
     
    class ViewOfDisplay implements Observer
    {
      private static final int H = 300;       // Height of window pixels
      private static final int W = 400;       // Width  of window pixels
     
      public ViewOfDisplay( RootPaneContainer rpc )
      {
        // Add code to setup a graphical view of the display
      }
     
     
     
      public void update( Observable aModelOfDisplay, Object arg )
      {
         // Code to update the graphical display with the current
         //  state of the system
         //  Orders awaiting processing
         //  Orders being picked in the 'warehouse. 
         //  Orders awaitig collection
      }
     
     
    }





    This is the rotating text program

    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.image.BufferedImage;
     
    // Note Menu bar is at the top of the area 
     
    class Main
    {
      public static void main( String args[] )  // 
      {                                         // 
        (new Application()).setVisible(true);   // Start application 
      }                                         // 
    }
     
    class Application extends JFrame
    {
      private static final int H = 300;         // Height of window 
      private static final int W = 400;         // Width  of window 
     
      private Animate animation;                // Active object 
      private String  message = 
              "Rotating message ";
      private Font font =
           new Font("Monospaced",Font.BOLD,24);
     
      public Application()
      {
        setSize( W, H );                        // Size of drawing area 
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        animation = new Animate();              // Start chimney smoking 
        animation.start();
      }
     
      public void update( Graphics g )          // Called by repaint 
      {                                         // 
        drawScreen( (Graphics2D) g );           // Draw Screen 
      }
     
      public void paint( Graphics g )           // When 'Window' is first 
      {                                         //  shown or damaged 
        drawScreen( (Graphics2D) g );           // Draw Screen 
      }
     
      private Dimension     theAD;              // Alternate Dimension 
      private BufferedImage theAI;              // Alternate Image 
      private Graphics2D    theAG;              // Alternate Graphics 
     
      public void drawScreen( Graphics2D g )    // Double buffer 
      {                                         //  allow resize 
        Dimension d    = getSize();             // Size of image 
     
        if (  ( theAG == null )  ||
              ( d.width  != theAD.width ) ||
              ( d.height != theAD.height ) )
        {                                       // New size 
          theAD = d;
          theAI = (BufferedImage) createImage( d.width, d.height );
          theAG = theAI.createGraphics();
          AffineTransform at = new AffineTransform();
          at.setToIdentity();
          at.scale( ((double)d.width)/W, ((double)d.height)/H );
          theAG.transform(at);
        }
     
        drawActualScreen( theAG );               // Draw actual screen 
        g.drawImage( theAI, 0, 0, this );
      }
     
      public void drawActualScreen( Graphics2D g )// Actual draw screen 
      {
        g.setPaint( Color.white );              // Paint Colour 
        g.fill( new Rectangle2D.Double( 0, 0, W, H ) );
     
        g.setPaint( Color.black );              // Paint Colour 
        g.setFont ( font );
     
        FontMetrics fm = getFontMetrics( font );
        int width = fm.stringWidth(message);
     
        int offset = animation.getPos();
     
        g.drawString( message, offset, 100 );
        g.drawString( message, W + offset, 100 );
      }
     
      class Animate extends Thread
      {
        private int       pos = 1;              // 
     
        public synchronized int getPos()
        {
          return pos;
        }
     
        public synchronized void moveString()   // Move string 
        {
          pos--;                                //  New position 
          if ( pos < -W ) pos = 0;
        }
     
        public void run()                       // Active part 
        {
          while ( true )                        // Forever 
          {                                     // 
            try                                 // 
            {                                   // 
              moveString();                     //  Move mess 
              repaint();                        //   -> update 
              Thread.sleep( 25 );               //  Sleep 
            }                                   // loop 
            catch (InterruptedException e) {}   // Ignore any error 
          }
        }
      }
     
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Drawing a string without use of Main(String[] args)

    The program needs either an explicit main method or an implicit one (such as with an applet) in order to run. I know of no way around this other than using a static initializer kludge.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Drawing a string without use of Main(String[] args)

    @curmudgeon I've lost my example of how to start a class (one without a main() method) using a static initializer using the java command. Could you post a SSCCE for it?
    Thanks.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Drawing a string without use of Main(String[] args)

    I think I may be mistaken on that one. I thought that you could get some code up and running from a static initializer block without a main, but on looking into this, I'm not so sure any more.

  5. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Drawing a string without use of Main(String[] args)

    Maybe they changed something with 7.

    --- Update ---

    According to the (unchanged) documentation for the java executable it "launches a Java application. It does this by starting a Java runtime environment, loading a specified class, and invoking that class's main method".

    So does the following wrok?

    public class Silly {
        static {
            Silly app = new Silly();
            app.displayWhy();
                // We exit before the java executable gets a chance to complain
                // about not finding main()
            System.exit(666);
        }
     
        void displayWhy() {
            System.out.println("Because we can!");
        }
    }

    It's fine in 6 update 26, but results in an error in 7.

  6. The Following 2 Users Say Thank You to pbrockway2 For This Useful Post:

    curmudgeon (December 17th, 2012), Norm (December 18th, 2012)

Similar Threads

  1. Not sure if run() or main(String[] args)
    By Brock Lee in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 25th, 2012, 12:19 PM
  2. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  3. Why main(String args[]) method needs to be public?
    By rohan22 in forum Java Theory & Questions
    Replies: 11
    Last Post: December 7th, 2011, 11:41 PM
  4. Java main method args
    By mattxo in forum Java Theory & Questions
    Replies: 3
    Last Post: May 16th, 2011, 09:45 AM
  5. Strange problem with drawing string
    By Asido in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 26th, 2010, 03:38 PM