Advertisement

Paint method not called

Started by July 26, 2014 09:04 AM
2 comments, last by TheChubu 10 years, 6 months ago

Hello!

I've been looking into running fullscreen applications in java, and I went on and tried to run this:


import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.*;


public class Main extends JFrame{
	
	public static final String TITLE = "Pong";
	public static final String VERSION = "1.0";
	
	public static void main(String[] args) {
		JFrame window = new JFrame();
		window.setUndecorated(true);
		window.setResizable(false);
		window.setBackground(Color.BLUE);
		window.setForeground(Color.WHITE);
		DisplayMode dm = new DisplayMode(800, 600, 16, 60);
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		GraphicsDevice gd = ge.getDefaultScreenDevice();
		gd.setFullScreenWindow(window);
		try {
			gd.setDisplayMode(dm);
		}
		catch(IllegalArgumentException ex) {
			System.out.print("Illegal Display Mode");
		}
		
		try {
			Thread.sleep(5000);
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		
		gd.setFullScreenWindow(null);

	}
	
	public void paintComponent(Graphics g) {
		System.out.print("Paint Method Called");
		g.drawString("Fullscreen", 50, 50);
	}
	
}

The problem here, is that I couldn't get the paint method to be called. Why is that?

What do you mean you "couldn't get the paint method to be called"? What is exactly the "paint" method? I don't see any method called "paint" in your code.

Also, why couldn't you get that method called? Is it giving a compiling error? Is throwing an Exception? Did you debug the code and saw that the method is never called?

My wild guess is that you tried to call the method paintComponent in your main method, and you received a compiler error. You need to make the method paintComponent static in order to be able to call it from static methods (and the main method is static).

If you want to get better help, you should give more details about your problem. Several things your post is lacking:

  • Detailed explanation of what is actually happening and what you expected to happen. After reading your post I have no idea what is happening with your code and what do you expect to happen, and I guessed a possible problem by looking at your code, not your explanation. When you get any kind of error, you should always post what the error is. It's far more easy to answer the question "My code is throwing a MongerWindingBuffaloException in the line 12 when the variable 'tomatoe' is 144 and I don't understand why" than the question "My code is failing [and following a code block with a hundred of lines]" Don't count on people making wild guesses about what your error could be.
  • Explain what have you done already to solve the problem. Did you debug the code? Did you search the error in google? What were the results and why didn't helped to solve the problem?

The more time you spend making your problem understandable, the more complete and useful answers you will get. Don't expect anyone to put more effort in answering a question than the effort you've put in asking it.

Advertisement

What do you mean you "couldn't get the paint method to be called"? What is exactly the "paint" method? I don't see any method called "paint" in your code.

Uh, the paintComponent method has a big fat println that says "Paint Method Called". I agree about posting more information, but in this case it seems pretty clear what the paint method is.

Morrowa, the issue is (seems to be, I haven't tested) that you are creating a basic JFrame, which doesn't include your paint method (which belongs to Main, the class inheriting from JFrame you are in). In other words, you are never instantiating your own implementation of JFrame, and so it is not being used. Try this:


JFrame window = new Main();

Or something to that effect. Also, sleeping is probably not the most effective way of keeping a GUI running, since I believe it pauses the main (GUI) thread. Just leave the window open and add some code that quits the program when you click on it or press a key or whatever, that way you can test it more easily without worrying about blocking.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

I'll add that paintComponent() doesn't always get called. It only gets called when the widget (panel, frame, button, etc) needs to be repainted (say, window was resized, button was pressed, etc), otherwise it won't get called.

If you need a component to be redrawn at some point, there exists the "repaint()" method in Swing widgets to achieve that.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

This topic is closed to new replies.

Advertisement