Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem with custom painting using JApplet and JPanel

843789Dec 9 2009 — edited Dec 10 2009
Hello,
I recently started work on a basic tetris clone (as a personal project, to enhance my skills.. or lack thereof!), however I've been running into quite a few problems with swing. I've solved most of them, and my basic GUI shows up fine now.

However I came to paint and realised I needed to use custom painting. Hence I went through all the tutorials (http://java.sun.com/docs/books/tutorial/uiswing/painting/step2.html) and my code is now:
// SimpleApplet.java
// An example of the JApplet class.  For use with the applet.html file.
//
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

public class TetrisClone extends JApplet {

	public void init() {

		setSize( 450, 500 );

		Container content = getContentPane();
		content.setLayout(new GridBagLayout());
		content.add( new createGUI() );
	}

}

class createGUI extends JPanel {

	int currentLevel = 0;
	int currentScore = 0;
	int currentLines = 0;
	BufferedImage img = null;

	public createGUI() {

		setLayout(new BorderLayout());

		// Main bit where blocks fall
		JPanel main = new JPanel();
		main.setLayout( new FlowLayout() );
		main.setPreferredSize( new Dimension( 325, 500 ) );
		main.setBackground( new Color(68,75,142) );

		add( main, BorderLayout.CENTER );

		// The right panel. Has quite a few bits. Starts here..
		FlowLayout flow = new FlowLayout();
		flow.setVgap( 20 );

		JPanel right = new JPanel();
		right.setLayout( flow );
		right.setPreferredSize( new Dimension( 125, 500 ) );
		right.setBackground( new Color(27,34,97) );
		right.setBorder( BorderFactory.createMatteBorder(0,2,0,0,Color.black) );

		// Next block bit
		JPanel rightNext = new JPanel();
		rightNext.setPreferredSize( new Dimension( 100, 100 ) );
		rightNext.setBackground( new Color(130,136,189) );
		rightNext.setBorder( BorderFactory.createEtchedBorder(EtchedBorder.LOWERED) );

		Font rightFont = new Font( "Courier", Font.BOLD, 18 );

		// The player's playing details
		JLabel rightLevel = new JLabel("Level: " + currentLevel, JLabel.LEFT );
		rightLevel.setFont( rightFont );
		rightLevel.setForeground( Color.white );

		JLabel rightScore = new JLabel("Score: " + currentScore, JLabel.LEFT );
		rightScore.setFont( rightFont );
		rightScore.setForeground( Color.white );

		JLabel rightLines = new JLabel("Lines: " + currentLines, JLabel.LEFT );
		rightLines.setFont( rightFont );
		rightLines.setForeground( Color.white );

		JPanel margin = new JPanel();
		margin.setPreferredSize( new Dimension( 100, 50 ) );
		margin.setBackground( new Color(27,34,97) );

		JButton rightPause = new JButton("Pause");

		try {
		    img = ImageIO.read(new File("MadeBy.gif"));
		}
		catch (IOException e) { }

		right.add( rightNext );
		right.add( rightLevel );
		right.add( rightScore );
		right.add( rightLines );
		right.add( margin );
		right.add( rightPause );

		add( right, BorderLayout.LINE_END );

		validate();
	}

	public Dimension getPreferredSize() {
		return new Dimension( 450, 500 );
	}

	public void paintComponent( Graphics g ) {
		super.paintComponent(g);

		g.setColor( Color.black );

		g.drawString("This does not show up",10,20);
		g.fillRect(10,10,100,100); // This won't show up either
		g.drawImage( img, 300, 300, null ); // Nor this!
	}

}
However despite following the tutorial's ideas and using an over-ridden paintComponent, nothing I paint in the paintComponent bit ends up showing up on my screen.

I've spent a while on this and followed numerous tutorials - I'd appreciate any insight into what I'm doing wrong :)

Kind Regards,

Tristan Perry
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 7 2010
Added on Dec 9 2009
3 comments
109 views