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!

Help on basic Java 2D graphics

843785Dec 6 2008 — edited Dec 6 2008
Hello again. It's my first time working with 2D graphics in Java and our class now wants us to make a game or a simulation of a popular game, Diner Dash. I've got no problems with the algorithms for the simulation. But god, do I hate my school for the other half of the project. My retarded school didn't even damn teach us crap about Java GUI or graphics and now I'm down in the dumps trying to figure it all out. While browsing through tutorials on the net, none of them were consistent and it makes me think that there are so many ways to implement it.

So here I am asking for some help. I tried following one of the most common ways I've seen in trying to have a paintComponent draw graphics onto my JPanel.
import java.util.*;
import java.awt.*; 
import java.applet.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class DinerDashGUI extends JFrame implements ActionListener

{
	JPanel GUIPanel;
	
	JLabel CustomersServedLabel;
    JTextField CustomerServedTextField;
    JLabel CustomersLeftLabel;
    JTextField CustomersLeftTextField;
    JLabel DurationLabel;
    JTextField DurationTextField;
    JButton ReplaceThisWithLogo1;
    
    public void paintComponent(Graphics g) 
    	{
    		//super.paintComponent(g);
    		
    		Graphics2D g2D = (Graphics2D)g;
        	setBackground(Color.black);
    		
    		Image c1 = Toolkit.getDefaultToolkit().getImage(getClass().getResource("IMG/c1.gif"));
    		Image c2 = Toolkit.getDefaultToolkit().getImage(getClass().getResource("IMG/c2.gif"));
    		Image c3 = Toolkit.getDefaultToolkit().getImage(getClass().getResource("IMG/c3.gif"));
    		Image c4 = Toolkit.getDefaultToolkit().getImage(getClass().getResource("IMG/c4.gif"));
    		Image c5 = Toolkit.getDefaultToolkit().getImage(getClass().getResource("IMG/c5.gif"));
    		Image c6 = Toolkit.getDefaultToolkit().getImage(getClass().getResource("IMG/c6.gif"));
    		Image c7 = Toolkit.getDefaultToolkit().getImage(getClass().getResource("IMG/c7.gif"));
    		Image c8 = Toolkit.getDefaultToolkit().getImage(getClass().getResource("IMG/c8.gif"));
    		
   			g.drawImage (c1, 25, 65, this);
   			g.drawImage (c2, 100, 65, this);
   			g.drawImage (c3, 175, 65, this);
   			g.drawImage (c4, 250, 65, this);
   			g.drawImage (c5, 325, 65, this);
   			g.drawImage (c6, 400, 65, this);
   			g.drawImage (c7, 475, 65, this);
   			g.drawImage (c8, 550, 65, this);
    	}
    
    public DinerDashGUI() 
    {
    	
    	this.setSize(800,600);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //frame
		this.setVisible(true);
		
		
		GUIPanel = new JPanel();
		this.add(GUIPanel); //add panel to frame
       	GUIPanel.setLayout(null);
    	
    	
        //construct components
        CustomersServedLabel = new JLabel ("Customers Served");
        CustomerServedTextField = new JTextField (5);
        CustomersLeftLabel = new JLabel ("# of Customers Who Left");
        CustomersLeftTextField = new JTextField (5);
        DurationLabel = new JLabel ("Duration of Simulation");
        DurationTextField = new JTextField (5);
        ReplaceThisWithLogo1 = new JButton ("Logo here");
        

        //add components
        GUIPanel.add (CustomersServedLabel);
        GUIPanel.add (CustomerServedTextField);
        GUIPanel.add (CustomersLeftLabel);
        GUIPanel.add (CustomersLeftTextField);
        GUIPanel.add (DurationLabel);
        GUIPanel.add (DurationTextField);
        GUIPanel.add (ReplaceThisWithLogo1);
        

        //set component bounds (only needed by Absolute Positioning)
        CustomersServedLabel.setBounds (665, 190, 110, 25);
        CustomerServedTextField.setBounds (665, 215, 105, 25);
        CustomersLeftLabel.setBounds (645, 250, 150, 25);
        CustomersLeftTextField.setBounds (665, 275, 105, 25);
        DurationLabel.setBounds (655, 130, 130, 25);
        DurationTextField.setBounds (665, 155, 105, 25);
        ReplaceThisWithLogo1.setBounds (660, 10, 115, 110);
       
        
    }
	
	public void actionPerformed(ActionEvent event) 
    {
    	
    }
    
    
    
    public static void main(String[] args)
	{
		new DinerDashGUI();
	}
    
}
What's odd is that from the tutorials I've seen, enabling a paintComponent and JPanel in the same class would automatically have the stuff 'drawn/painted' in paintComponent appear in JPanel. But it's not happening. I compile and run and only the stuff constructed in the JPanel appears. This is the most common form I see from many of the tutorials yet they don't work for me. Am I missing something?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 3 2009
Added on Dec 6 2008
10 comments
565 views