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
Code :
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
Code :
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
}
}
}
}
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.
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.
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.
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?
Code :
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.