Hello All,

Forgive my ignorance...

I am creating a simple program that draws a simple house. My problem is I need it to run as an application as well as an applet from the same code..I think I am on the right track..Any help would be greatly appreciated check out my code to see if you can see the problems I cannot.

So far It will run as an applet, but so far when I execute it as an application it just produces the frame with nothing in it..


import javax.swing.JApplet;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.event.*;

public class DrawHouse extends JApplet
{


public static void main(String[] args)
{
Frame f = new Frame(); //Create a new frame object

DrawHouse drawHouse = new DrawHouse(); //Create an instance of DrawHouse

//Add the new instance into the frame
// getContentPane().setBackground( Color.WHITE );
f.setLayout(new FlowLayout());
f.add( drawHouse );
f.setSize(600,600);
f.setVisible(true);

//An "anonymous" inner class used to close the window
f.addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
);

drawHouse.init(); //Call the applet's init method
} //End of main

public void paint( Graphics g )
{
//super.paint( g );

Graphics2D g2d = ( Graphics2D ) g;

g2d.setColor( Color.BLUE );
g2d.drawRect(10,10,600,600 );
g2d.fillRect(10,10,600,600 );

Polygon polygon2 = new Polygon();
g2d.setPaint( new GradientPaint( 10, 400, Color.GREEN, 400,500, Color.YELLOW, false) );
polygon2.addPoint( 10, 400 );
polygon2.addPoint( 400, 500 );
polygon2.addPoint( 10, 700 );
polygon2.addPoint( 10, 700 );


g2d.fillPolygon( polygon2 );

//house image
BufferedImage buffImage = new BufferedImage( 10, 10,
BufferedImage.TYPE_INT_RGB );


Graphics2D gg = buffImage.createGraphics();
gg.setColor( Color.WHITE );
gg.fillRect( 0, 0, 10, 10 );
gg.setColor( Color.RED );
gg.fillRect( 2, 4, 7,9 );


// paint buffImage onto the JFrame
g2d.setPaint( new TexturePaint( buffImage,
new Rectangle( 10, 10 ) ) );
// g2d.setColor( Color.RED );
g2d.drawRect(10,300,150,200 );
g2d.fillRect(10,300,150,200 );

//garage image
BufferedImage buffImage2 = new BufferedImage( 10, 10,
BufferedImage.TYPE_INT_RGB );


Graphics2D gg2 = buffImage2.createGraphics();
gg2.setColor( Color.WHITE );
gg2.fillRect( 0, 0, 10, 10 );
gg2.setColor( Color.RED );
gg2.fillRect( 2, 4, 7,9 );


// paint buffImage onto the JFrame
g2d.setPaint( new TexturePaint( buffImage2,
new Rectangle( 10, 10 ) ) );
// g2d.setColor( Color.RED );
g2d.drawRect(160,400,100,100 );
g2d.fillRect(160,400,100,100 );

// windows images
g2d.setPaint( new GradientPaint( 20, 300, Color.WHITE, 40,500, Color.BLUE, false) );
g2d.drawRect(20,320,40,40 );
g2d.fillRect(20,320,40, 40 );

g2d.setPaint( new GradientPaint( 20, 300, Color.WHITE, 40,500, Color.BLUE, false) );
g2d.drawRect(90,320,40,40 );
g2d.fillRect(90,320,40, 40 );


g2d.setPaint( new GradientPaint( 20, 300, Color.WHITE, 40,500, Color.BLUE, false) );
g2d.drawRect(20,380,40,40 );
g2d.fillRect(20,380,40, 40 );

g2d.setPaint( new GradientPaint( 20, 300, Color.WHITE, 40,500, Color.BLUE, false) );
g2d.drawRect(90,380,40,40 );
g2d.fillRect(90,380,40, 40 );


//front door image
g2d.setColor( Color.ORANGE );
g2d.drawRect(55,430,40,70 );
g2d.fillRect(55,430,40,70 );

//garage door image

g2d.setColor( Color.BLACK );
g2d.drawRect(160,430,90,70 );
g2d.fillRect(160,430,90,70 );

}//end paint

}// end class DrawHouse

Hello All,

Forgive my ignorance...

I am creating a simple program that draws a simple house. My problem is I need it to run as an application as well as an applet from the same code..I think I am on the right track..Any help would be greatly appreciated check out my code to see if you can see the problems I cannot.

So far It will run as an applet, but so far when I execute it as an application it just produces the frame with nothing in it..


import javax.swing.JApplet;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.event.*;

public class DrawHouse extends JApplet
{


public static void main(String[] args)
{
Frame f = new Frame(); //Create a new frame object

DrawHouse drawHouse = new DrawHouse(); //Create an instance of DrawHouse

//Add the new instance into the frame
// getContentPane().setBackground( Color.WHITE );
f.setLayout(new FlowLayout());
f.add( drawHouse );
f.setSize(600,600);
f.setVisible(true);

//An "anonymous" inner class used to close the window
f.addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
);

drawHouse.init(); //Call the applet's init method
} //End of main

public void paint( Graphics g )
{
//super.paint( g );

Graphics2D g2d = ( Graphics2D ) g;

g2d.setColor( Color.BLUE );
g2d.drawRect(10,10,600,600 );
g2d.fillRect(10,10,600,600 );

Polygon polygon2 = new Polygon();
g2d.setPaint( new GradientPaint( 10, 400, Color.GREEN, 400,500, Color.YELLOW, false) );
polygon2.addPoint( 10, 400 );
polygon2.addPoint( 400, 500 );
polygon2.addPoint( 10, 700 );
polygon2.addPoint( 10, 700 );


g2d.fillPolygon( polygon2 );

//house image
BufferedImage buffImage = new BufferedImage( 10, 10,
BufferedImage.TYPE_INT_RGB );


Graphics2D gg = buffImage.createGraphics();
gg.setColor( Color.WHITE );
gg.fillRect( 0, 0, 10, 10 );
gg.setColor( Color.RED );
gg.fillRect( 2, 4, 7,9 );


// paint buffImage onto the JFrame
g2d.setPaint( new TexturePaint( buffImage,
new Rectangle( 10, 10 ) ) );
// g2d.setColor( Color.RED );
g2d.drawRect(10,300,150,200 );
g2d.fillRect(10,300,150,200 );

//garage image
BufferedImage buffImage2 = new BufferedImage( 10, 10,
BufferedImage.TYPE_INT_RGB );


Graphics2D gg2 = buffImage2.createGraphics();
gg2.setColor( Color.WHITE );
gg2.fillRect( 0, 0, 10, 10 );
gg2.setColor( Color.RED );
gg2.fillRect( 2, 4, 7,9 );


// paint buffImage onto the JFrame
g2d.setPaint( new TexturePaint( buffImage2,
new Rectangle( 10, 10 ) ) );
// g2d.setColor( Color.RED );
g2d.drawRect(160,400,100,100 );
g2d.fillRect(160,400,100,100 );

// windows images
g2d.setPaint( new GradientPaint( 20, 300, Color.WHITE, 40,500, Color.BLUE, false) );
g2d.drawRect(20,320,40,40 );
g2d.fillRect(20,320,40, 40 );

g2d.setPaint( new GradientPaint( 20, 300, Color.WHITE, 40,500, Color.BLUE, false) );
g2d.drawRect(90,320,40,40 );
g2d.fillRect(90,320,40, 40 );


g2d.setPaint( new GradientPaint( 20, 300, Color.WHITE, 40,500, Color.BLUE, false) );
g2d.drawRect(20,380,40,40 );
g2d.fillRect(20,380,40, 40 );

g2d.setPaint( new GradientPaint( 20, 300, Color.WHITE, 40,500, Color.BLUE, false) );
g2d.drawRect(90,380,40,40 );
g2d.fillRect(90,380,40, 40 );


//front door image
g2d.setColor( Color.ORANGE );
g2d.drawRect(55,430,40,70 );
g2d.fillRect(55,430,40,70 );

//garage door image

g2d.setColor( Color.BLACK );
g2d.drawRect(160,430,90,70 );
g2d.fillRect(160,430,90,70 );

}//end paint

}// end class DrawHouse

I finally figured it out....at 5:00 in the a.m.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.