User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 456,234 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,745 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: 2934 | Replies: 1
Reply
Join Date: Aug 2004
Location: Louisville Ky
Posts: 15
Reputation: ZEEPLE is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
ZEEPLE ZEEPLE is offline Offline
Newbie Poster

DrawHouse Code Problem

  #1  
Aug 20th, 2004
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2004
Location: Louisville Ky
Posts: 15
Reputation: ZEEPLE is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
ZEEPLE ZEEPLE is offline Offline
Newbie Poster

Re: DrawHouse Code Problem

  #2  
Aug 21st, 2004
Originally Posted by ZEEPLE
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 5:08 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC