Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-04-2009, 10:41 PM
Member
 
Join Date: Jul 2009
Posts: 1
Rep Power: 0
Corder10 is on a distinguished road
Unhappy Why doesn't this work?
I am trying to get my program to draw a black rectangle in a frame. The frame pops up, but there is no rectangle. Could someone de-bug my program and tell me what I need to do to fix it?

I used Eclipse to write the program, and there were no errors found.

Thanks in advance.




import java.awt.*;
import javax.swing.*;

public class Main1 extends JFrame
{
public Main1()
{
super("Paint");
setSize(1000,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public static void Paint(Graphics g)
{
g.setColor(Color.black);
g.fillRect(0, 0, 1000, 500);
}

public static void main(String[] args)
{
@SuppressWarnings("unused")
Main1 thisOne=new Main1();

}
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 07-04-2009, 11:33 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
Default
To override it, your method's must match the old method's signature exactly, and the paint method of a JFrame is not static.

Other suggestions:
1) Don't draw on a JFrame. Instead draw in a JPanel or JComponent and add this to the JFrame's contentPane.
2) When drawing in the component above, override paintComponent, not paint as this will allow for Swing's automatic double buffering.

For instance:

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyPanel extends JPanel {
  
  @Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.red);
    g.fillRect(100, 100, 100, 100);
  }
  
  public MyPanel() {
    setPreferredSize(new Dimension(300, 300));
    setBackground(Color.black);
  }
  
  private static void createAndShowUI() {
    JFrame frame = new JFrame("MyPanel");
    frame.getContentPane().add(new MyPanel());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }

  public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
	createAndShowUI();
      }
    });
  }
}

Last edited by Fubarable; 07-04-2009 at 11:38 PM.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why won't this while loop work? trueblue New To Java 6 05-23-2009 09:10 PM
I need help with my work assig Please tracy-london New To Java 3 01-04-2009 06:50 PM
Synchronization Doesn't seem to work sherinpearl Threads and Synchronization 1 04-23-2008 07:30 PM
Pass by ref. A work around? diRisig New To Java 0 02-05-2008 08:25 PM
how would i get this to work...? deeadeed New To Java 6 12-06-2007 03:58 AM


All times are GMT +2. The time now is 02:19 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org