Jump to content

  • Log In with Google      Sign In   
  • Create Account

Paint method not called


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
3 replies to this topic

#1 Morrowa   Members   -  Reputation: 131

Like
0Likes
Like

Posted 26 July 2014 - 03:04 AM

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?



Sponsor:

#2 Avalander   Members   -  Reputation: 1133

Like
0Likes
Like

Posted 26 July 2014 - 03:37 AM

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.


Edited by Avalander, 26 July 2014 - 03:42 AM.


#3 Bacterius   Crossbones+   -  Reputation: 9610

Like
1Likes
Like

Posted 26 July 2014 - 06:08 AM


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.


Edited by Bacterius, 26 July 2014 - 06:16 AM.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#4 TheChubu   Crossbones+   -  Reputation: 5215

Like
1Likes
Like

Posted 26 July 2014 - 04:02 PM

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





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