Results 1 to 3 of 3
- 11-05-2010, 02:56 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 1
- Rep Power
- 0
New to Java GUI. Can't get this to work.
I'm only trying to create Pong. I need a JFrame that has a black background. What am I doing wrong? Here's my code so far...
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class Pong extends JFrame{
public Pong(){
int width = 800;
int height = 600;
setSize(width,height);
setTitle("Wacky Pong 380");
setBackground(Color.BLACK);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
setVisible(true);
}
public void draw(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(0,0,getWidth(),getHeight());
}
public static void main(String[] args){
new Pong();
}
}
- 11-05-2010, 05:41 AM #2
the JFrame background is covered by the content pane.
also, if you want to do drawing in a JFrame, then you should put a JPanel into the JFrame, and override its paintComponent method.
- 11-07-2010, 07:22 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 15
- Rep Power
- 0
this will get you started
Java Code:import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class Pong extends JFrame{ private DrawPanel drawPanel; private Container container; private static int width = 800, height = 600; public Pong(){ container = this.getContentPane(); container.setPreferredSize(new Dimension(width,height)); setTitle("Wacky Pong 380"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); setVisible(true); drawPanel = new DrawPanel(); container.add(drawPanel); pack(); } public class DrawPanel extends JPanel { public DrawPanel(){ } public void paintComponent(Graphics g) { g.setColor(Color.black); g.fillRect(0,0,width,height); g.setColor(Color.yellow); g.fillRect(width/3,height/3,width/4,height/4); } } }Last edited by doomsword2001; 11-08-2010 at 01:22 AM.
Similar Threads
-
Out of memory work around for a java application (please help!)
By javameanslife in forum Advanced JavaReplies: 9Last Post: 02-02-2010, 01:52 PM -
Trying to get JAVA to work with SQLite
By mark8569 in forum JDBCReplies: 3Last Post: 04-25-2009, 01:42 AM -
Just how do I get Java to actually work?
By MickY G in forum New To JavaReplies: 5Last Post: 11-19-2008, 03:50 AM -
What are applet and how its work in java?
By pawankumarom in forum New To JavaReplies: 7Last Post: 09-05-2008, 05:26 PM -
Ho to work with enumerations in java
By zizou147 in forum Advanced JavaReplies: 0Last Post: 03-22-2008, 12:53 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks