| | |
DrawHouse code help...
![]() |
•
•
Join Date: Aug 2004
Posts: 15
Reputation:
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
![]() |
Similar Threads
- Code Snippet: Sample code for taking user input from Shell (Java)
- Code Snippet: code to split (and reconnect) an Access database (part 1) (Visual Basic 4 / 5 / 6)
- Java Problem with running program (Java)
- Code Snippet: Gray Code Conversion (C)
- Code Snippet: code to split (and reconnect) an Access database (part 2) (Visual Basic 4 / 5 / 6)
- Code Snippet: Send HTML code to Internet Explorer from basic (Legacy and Other Languages)
- DrawHouse Code Problem (Java)
Other Threads in the Java Forum
- Previous Thread: DrawHouse Code Problem
- Next Thread: flash movies
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application arguments array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class client code color component count database derby design eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui homework html ide if_statement image integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia keyword linux list macintosh map method methods midlethttpconnection mobile netbeans newbie nullpointerexception object open-source os problem producer program programming project projectideas property read recursion reference replaysolutions ria scanner search server set size sms sort sourcelabs splash sql sqlite stop string swing threads transforms tree ui unicode validation windows





