paintComponent(Graphics g)

Hi Again…

I have this piece of Code


import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JLabel;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.JProgressBar;
import sun.java2d.SunGraphics2D;

public class MainPanel extends JPanel {

	
	private JPanel controlPanel = null;
	private JPanel statusPanel = null;
	private JLabel hplbl = null;
	private JProgressBar healthPB = null;
	private JLabel mplbl = null;
	private JProgressBar manaPB = null;
	private JProgressBar experiencePB = null;
	private JPanel chatPanel = null;
	private JPanel charUsePanel = null;
	

	private ArrayList actors; 
	private BufferedImage background, backgroundTile;
	private int backgroundY;
	private SunGraphics2D graphics;


	public MainPanel() {
		super();
		initialize();
	}
	public void game(){
		initWorld();
	}

	private  void initialize() {
		this.setLayout(new BorderLayout());
		this.setSize(800,600);
		this.setOpaque(false);
		this.add(getControlPanel(), java.awt.BorderLayout.SOUTH);
	}
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		g.fillRect(10,10,50,50);
		g.setColor(Color.RED);
		g.fillOval(220,250,50,50);
		
		graphics =(SunGraphics2D) g;
	}
	public void initWorld(){
		
		super.paint(graphics);
		graphics.fillRect(20,20,50,50);
		this.paint(graphics);
	}

	private JPanel getControlPanel() {
		if (controlPanel == null) {
			controlPanel = new JPanel();
			controlPanel.setLayout(new BorderLayout());
			controlPanel.add(getChatPanel(), java.awt.BorderLayout.EAST);
			controlPanel.add(getCharUsePanel(), java.awt.BorderLayout.CENTER);
			
		}
		return controlPanel;
	}
 
	private JPanel getStatusPanel() {
		if (statusPanel == null) {
			hplbl = new JLabel();
			mplbl = new JLabel();
			statusPanel = new JPanel();
			GridBagConstraints gbc = new GridBagConstraints();
			statusPanel.setLayout(new GridBagLayout());
			gbc.gridx = 0;
			gbc.gridy = 0;
			gbc.anchor = GridBagConstraints.CENTER;
			gbc.ipadx = 15;
			gbc.insets = new Insets(5,10,5,0);
			hplbl.setText("HP");
			mplbl.setText("MP");
			statusPanel.add(hplbl, gbc);
			gbc.gridx++;
			statusPanel.add(getHealthPB(), gbc);
			gbc.gridx = 0;
			gbc.gridy++;
			statusPanel.add(mplbl, gbc);
			gbc.gridx++;
			statusPanel.add(getManaPB(), gbc);
		}
		return statusPanel;
	}
 
	private JProgressBar getHealthPB() {
		if (healthPB == null) {
			healthPB = new JProgressBar();
			healthPB.setValue(75);
			healthPB.setString("Test");
			healthPB.setStringPainted(true);
		}
		return healthPB;
	}
 
	private JProgressBar getManaPB() {
		if (manaPB == null) {
			manaPB = new JProgressBar();
			manaPB.setString("test2");
			manaPB.setStringPainted(true);
			manaPB.setValue(65);
		}
		return manaPB;
	}

	private JProgressBar getExperiencePB() {
		if (experiencePB == null) {
			experiencePB = new JProgressBar();
			experiencePB.setStringPainted(true);
		}
		return experiencePB;
	}
  
	private JPanel getChatPanel() {
		if (chatPanel == null) {
			chatPanel = new JPanel();
		}
		return chatPanel;
	}

	private JPanel getCharUsePanel() {
		if (charUsePanel == null) {
			charUsePanel = new JPanel();
			charUsePanel.setLayout(new BorderLayout());
			charUsePanel.add(getExperiencePB(), java.awt.BorderLayout.NORTH);
			charUsePanel.add(getStatusPanel(), java.awt.BorderLayout.WEST);
		}
		return charUsePanel;
	}
  }


I have this problem that of which that I am unable to loop the repainting of components…

This means… after i have painted on to the component…

I want to loop the repainting to take place in a loop…

however… since Graphics is an abstract class I can’t seem to figure it out…

any helpers?

Thanks you! :slight_smile:

Where’s your loop anyway? How do you expect to loop through something if you don’t have a loop to start with. :stuck_out_tongue:
If you’re aiming for active rendering you need a game loop, where in every pass your stuff will be painted and game state updated. I can tell you basic logic how it could be done. For details I suggest Killer Game Programming in Java.

you start game
you construct everything on top of some drawable component, like JPanel … use setIgnoreRepaint(true) to turn off OS painting
you enter a loop, ex: while (game_running) { …}
in that loop you call to gameStateUpdate() to update the game and gameRender() to render to some offscreen BufferedImage which you’ll later draw on screen (double buffering)
for that drawable component you override paint() (or paintComponent()?, whatever) method and paint offscreen buffered image you created on screen.
loop repeats until you finish the game or exit, then you set game_running to false and game ends.

@ENC

You can’t (or… shouldn’t) call Component.paintComponent(Graphics) yourself.

If you want to call it in a loop, call Component.repaint();
Everything else will be done for you.

OR go to active rendering.

http://java.sun.com/docs/books/tutorial/extra/fullscreen/rendering.html

Thanks… I will update your all my progress… ;D

Cheers!

As Kova said, you don’t have a loop going.

The second example on this page is what you’re looking for, in simple terms. The ClockApplet

http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter08/demoClock.html

implement runnable
create a new thread
start the new thread
override the run method
have your loop within the run method which calls repaint()

hope this helps.