•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 455,962 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,578 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 2233 | Replies: 1
![]() |
•
•
Join Date: Aug 2004
Location: Louisville Ky
Posts: 15
Reputation:
Rep Power: 5
Solved Threads: 0
Hello All,
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 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
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
access activation api blogger blogging blogs code com combo dani daniweb data debugging development dreamweaver dropdownlist gdata google gpl html innovation java key linux microsoft microsoft sdk for java microsystems module net news open openbsd platform product programming reuse rss serial software source sun tags vista web wysiwyg xml
- Why won't this code work? (VB.NET)
- Java Problem with running program (Java)
- DrawHouse Code Problem (Java)
- Tutorials & Code Submissions - Questions? (DaniWeb Community Feedback)
- Some Basic Code Hopefully (Help Needed) (HTML and CSS)
Other Threads in the Java Forum
- Previous Thread: DrawHouse Code Problem
- Next Thread: Where are the good Java RSS Feeds?


Linear Mode