Sign in to follow this  

[java] Paint NOT Being Called Correctly

This topic is 3143 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

title says it all, here is my code:
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.lang.*;
import java.applet.*;
import java.lang.reflect.Method;
import javax.swing.JOptionPane;
import javax.swing.JApplet;

public class halfdoomed extends Component implements Runnable, ActionListener, MouseListener, KeyListener, MouseMotionListener
{
	Thread runner;
	JFrame HDf;
	JFrame HDF;
	int XPos;
	int YPos;
	String POS = "Main Menu";
	String[] MMimgFilenames = {"HD0.PNG", "HD1.PNG", "HD2.PNG"};
	int framerate = 3;
	private Image[] mmframes = new Image[3];
	int frame;
	int PCll = 0;
	public static void main(String arg[])
	{
		halfdoomed game = new halfdoomed(arg);
	}
	public halfdoomed(String arg[])
	{
		GraphicsDevice dev = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); 
		GraphicsConfiguration gc = dev.getDefaultConfiguration();
		DisplayMode mode = new DisplayMode(1024, 768, 32, DisplayMode.REFRESH_RATE_UNKNOWN);
		HDf = new JFrame("Half-Doomed BETA v. 0.0.1.2");
		HDf.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		HDf.setUndecorated(true);
		HDf.setVisible(false);
		HDf.add(new halfdoomed(HDf));		
		HDf.pack();
		HDf.setResizable(false);
		dev.setFullScreenWindow(HDf); 
		if(dev.isDisplayChangeSupported()) dev.setDisplayMode(mode);
	}
	public halfdoomed(JFrame HDF)
	{
		try
		{
			URL url = null;
			for(int i = 0; i < 3; i++)
			{
				url = this.getClass().getClassLoader().getResource(MMimgFilenames[i]);
				mmframes[i] = ImageIO.read(url);
			}
		}
		catch (IOException e)
		{
			e.printStackTrace();
			return;
		}
		frame = 1;
		HDF.setVisible(true);
		HDF.addMouseListener(this);
		HDF.addKeyListener(this);
		HDF.addMouseMotionListener(this);
		runner = new Thread(this);
		runner.start();
	}
	public void mouseClicked (MouseEvent me)
	{
	}
	public void mouseEntered (MouseEvent me)
	{
		
	}
	public void mousePressed (MouseEvent me)
	{
			
	}
	public void mouseReleased (MouseEvent me)
	{
		
	}
	public void mouseExited (MouseEvent me)
	{
		
	} 
	public void mouseDragged (MouseEvent mme)
	{
		
	}
	public void mouseMoved (MouseEvent mme)
	{
		XPos = mme.getX();
		YPos = mme.getY();
	}
	public void actionPerformed(ActionEvent e)
	{
	}
	void PlaySound(String URLWAV)
	{
		try {
       		URL url = new URL(URLWAV);
       		AudioClip clip = Applet.newAudioClip(url);
	        clip.play();
		} catch (MalformedURLException e) {}
	}
	public void run()
	{
		repaint();
		try{Thread.sleep(1000 / framerate);}
		catch(InterruptedException ex) {}
	}
	public void paint(Graphics gg)
	{
		Graphics2D g = (Graphics2D)gg;
		if(POS == "Main Menu")
		{
			g.setColor(Color.decode("0xFF0000"));
			g.fill3DRect(0,0,1024,768,true);
			if(frame == 4)
			{
				g.drawImage(mmframes[1], 256, 55, null);
				frame = 0;
			}
			if(frame == 3)
			{
				g.drawImage(mmframes[2], 256, 55, null);
				frame = 4;
			}
			if(frame == 2)
			{
				g.drawImage(mmframes[1], 256, 55, null);
				frame = 3;
			}
			if(frame == 1)
			{
				g.drawImage(mmframes[0], 256, 55, null);
				frame = 2;
			}
			if(frame == 0)
			{
				frame = 1;
			}
			g.setColor(Color.decode("0x000000"));
			g.setFont(new Font("Western",Font.BOLD, 14));
			g.drawString("Play Half-Doomed (Single Player)", 300, 150);
			g.drawString("Play Half-Doomed (Multi Player)", 300, 165);
			g.drawString("Options", 300,180);
			g.drawString("Exit", 300, 195);
			g.drawString("Times Called: "+PCll, 500, 200);
			PCll ++;
		}
	}
	public void update(Graphics gg)
	{
		paint(gg);
	}
	public void keyTyped(KeyEvent event)
	{
		int got = 0;
		char keyP = event.getKeyChar();
	}
	public void keyPressed(KeyEvent event)
	{

	}
	public void keyReleased(KeyEvent event)
	{

	}
}

If you can figure out why paint is only called 3-5 times please I beg you help me out You can compile and run just make three images "HD0.PNG" "HD1.PNG" "HD2.PNG" and it should be runnable I just want to make this Main Menu screen animated...

Share this post


Link to post
Share on other sites
Well, what in your code would cause paint to be called repeatedly? It seems to not be happening because you're not doing it.

Share this post


Link to post
Share on other sites
I don't know why repaint(); was now being called correctly but after hours of trial and error then posting here i finally found a solution:
Make Run into this:
public void run()
{
while (runner == Thread.currentThread() )
{
repaint();
try{Thread.sleep(1000 / framerate);}
catch(InterruptedException ex) {System.out.println("ERROR: "+ex);}
}
}

(i would of never thought of this, can anyone explain why i need the while loop in the run void)

Share this post


Link to post
Share on other sites
I'm not sure what you were expecting to happen before. The thread you created issues a single redraw request, waits a little while, and then ends. Now it issues redraw requests, waits, then loops back to continue issuing redraw requests. It doesn't really matter what you put in the while-loop's condition.

EDIT: Were you assuming that threads automatically re-run themselves after returning? They don't.

Share this post


Link to post
Share on other sites
repaint() requests that the Component gets paint() called on it at some point in the future. I strongly suggest you read this brilliant article on rendering in Java.

Regards
elFarto

Share this post


Link to post
Share on other sites
Youll need to use active rendering and paint the components yourself, "Repainting" For games is no good atall.

Share this post


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