| | |
DrawHouse Code Problem
![]() |
•
•
Join Date: Aug 2004
Posts: 15
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Aug 2004
Posts: 15
Reputation:
Solved Threads: 0
•
•
•
•
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.
![]() |
Similar Threads
- Code Problem (Modifying Book Code to adapt to program) (Java)
- login code problem (VB.NET)
- can anyone help me with this progam,and write the code for this problem? (C)
- Code problem with an array displaying list (C#)
- Problem with a snippet of code in a shell program (C)
- do while; if else? am I using the right code for the problem? (C++)
- DrawHouse code help... (Java)
Other Threads in the Java Forum
- Previous Thread: Out of memory
- Next Thread: DrawHouse code help...
| Thread Tools | Search this Thread |
2dgraphics @param account affinetransform android api apple applet application arc arguments array automation banking binary binarytree bluetooth chatprogramusingobjects class client code color compare component count database derby design detection eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide if_statement image inheritance integer j2me java java.xls javadesktopapplications javaprojects jlabel jni jpanel julia keytool keyword linux list macintosh map method methods midlethttpconnection mobile monitoring netbeans nullpointerexception object open-source pong problem producer program project projectideas property reference replaysolutions ria rim scanner server set size sms sourcelabs splash sql swing terminal threads transforms tree ui unicode validation web windows





