Jump to content
  • Advertisement
Sign in to follow this  
guyver23

Java Difficulty

This topic is 4679 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I am in the processs of learning Java--of course with the intent of making games, and my first basic graphics demo is already causing problems. When I make a jar out of the code, the window displays, but the background is gray. Only when I maximize it or change its size does the background change color and the rectangle display. If I cover the window with another (like opening another program), the color reverts and the display won't be right until I move it again! Can someone please tell me what is wrong with the code? Thanks!
[source lang = "java"]

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

public class Main extends JFrame
{
    private Insets limit; // used to determine window boundries 
    private Container c; // used to display stuff
    private Color happyRect;
    
    public Main()
    {
        super("Color Test");
        
        c = getContentPane();
       
        setSize(300,300);
        c.setBackground(Color.blue);
        setVisible(true);
    }
    
    public void paint(Graphics g)
    {
        limit = c.getInsets();
        
        g.setColor(Color.orange);
        g.fillRect(10 + limit.left, 10 + limit.top, 30, 30);
    }
    
    public static void main(String args[])
    {
        Main app = new Main();
        
        app.addWindowListener( new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
    }
}


Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
If I read this correctly, try using repaint().

Share this post


Link to post
Share on other sites
I have an excellent Tutorial that I have found.

Java Invaders

It goes from your first window, to the fully fledged space invaders clone. It covers double buffering and other things that are needed in basic game design within Java.

I Hope this helps.

H_o_p_s

Share this post


Link to post
Share on other sites
Use the Java Invaders link. It gets to your issue very quickly. Basically the idea is you want to draw to a Canvas rather than a JFrame. You need a custom Canvas subclass, either instead of or in addition to your JFrame subclass.

The Space Invaders tutorial is a real gem. Thank you, H_o_p_s, for pointing it out.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!