Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Problem with Background (JAVA)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 PsionicTransvection   Members   -  Reputation: 262

Like
0Likes
Like

Posted 22 March 2012 - 10:57 AM

I wrote a class that is suppose to draw a red rectangle in a JPanel with and the background of the panel is suppose to be black here is my code


import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;

public class View extends JFrame
{
private  JPanel panel = new JPanel();
public void paint(Graphics g)
{
  g.setColor(Color.RED);
  g.drawRect(20, 50, 100, 100);
}
public View()
{
  setSize(640, 480);
  setResizable(true);
  setVisible(true);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setLocationRelativeTo(null);
  setBackground(Color.BLACK);
  panel.setSize(640, 480);
  panel.setBackground(Color.BLACK);
  panel.paint(null);
  this.add(panel);
}

public static void main(String[] args)
{
  View view = new View();



}
}

and here is what I get as a result there appears to be some glitch in the background

WTF.PNG
(this is a screen of the gui from my code not a minimized Eclipse)

and here is how it is without the paint method it does as it suppose to making a window with a black background

without paint method.PNG


Is it something wrong with my code that it does like that or there is something wrong with my computer ?

Sponsor:

#2 Angex   Members   -  Reputation: 884

Like
0Likes
Like

Posted 22 March 2012 - 04:20 PM

The "paint()" method you've implemented is overriding the paint of the JFrame.
So when the window is rendered, you're telling it to draw a red box.

In this case I think it would be better to override paintComponent, as this won't interfere with the draw order of the container and child components.

You need todo something like this, but there are many other ways:

private  JPanel panel = new JPanel()
{
	public void paintComponent(Graphics g)
	{
		g.setColor(Color.RED);
		g.drawRect(20, 50, 100, 100);
	}
};

The extra braces create an anonymous class which extends JPanel and overrides it's paintComponent method.

Also if you're relying on Swing to draw everything, don't call any paints directly from your own code (Unless for buferring). Use something like repaint(), to request a render.
E.g. You have "panel.paint(null);" in the constructor.
This is very bad, it won't work correctly and will cause lots of bugs.
Assuming you avoid the NullPointerException; do some research on the "AWT Event dispatch thread".

In the future you may want to consider "Active Rendering". I only mention it because someone else usually does anyway Posted Image




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS