Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

[java] I have some basic Java problems

Started by Alpha_ProgDes Mar 15, 2007 at 7:39 PM 7 replies 1.2k views
Original Post
Alpha_ProgDes
Alpha_ProgDes
I want to convert this application

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

public class Ch2app {
    
    public static void main(String[] args) {
    	
    	// TODO, add your application code
    	JFrame frame = new JFrame("I Want This in an Applet.");
    	frame.setBackground(Color.blue);
    	frame.setPreferredSize(new Dimension(300, 150));
    	frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    	
    	JPanel panel = new JPanel();
    	panel.setBackground(Color.yellow);
    	panel.setPreferredSize(new Dimension(150, 75));
    	
    	JLabel label1 = new JLabel("How Do I Do This with an Applet?");
    	
    	panel.add(label1);
    	
    	frame.getContentPane().add(panel);
    	frame.pack();
    	frame.setVisible(true);
    }
}
to an applet. Like the one below

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


public class Ch2 extends JApplet {
	
	public void init() {
		
		setBackground(Color.blue);
		JPanel one = new JPanel();
		
		one.setBackground(Color.yellow);
		one.setPreferredSize(new Dimension(150,75));
		getContentPane().add(one);
		setVisible(true);
	}

	public void paint(Graphics g) {
		
		g.drawString("Welcome to Java!!", 50, 60 );
		g.setColor(Color.gray);
		g.drawRect(50, 60, 75, 25);
		g.drawOval(50, 60, 75, 25);
		g.drawString("WtJ", 50, 70);
		
	}
}
Now some questions I have are: 1) does JApplet take the place of JFrame? 2) How do I make the Panel a certain size in the JFrame or JApplet. Is that even possible? 3) The paint() is that call separate from the start() or call from within start()?
Beginner in Game Development?  Read here. And read here.  
smc
smc
Quote:
Original post by Alpha_ProgDes
Now some questions I have are: 1) does JApplet take the place of JFrame? 2) How do I make the Panel a certain size in the JFrame or JApplet. Is that even possible? 3) The paint() is that call separate from the start() or call from within start()?


1) yes. Although you can create a JFrame from an applet.

2) I used to set the Layout to null. Then I would specify the dimensions with something like setRect() or setWidth()/setHeight(). It has been quite a while...

3) It is called separate from the start() method. The JVM will call the paint method. You may also call it directly from you code.

In your applet code... I believe you are drawing to the default graphics context. If you do not add the JPanel then you would see what you are drawing. Otherwize try something like:

(Move 'JPanel one' to member status).

Inside of paint it should be something like.

one.getGraphicsContext().drawString("Welcome to Java!!", 50, 60 );
∫Mc
CaptainJester
CaptainJester
1) All GUI applications require a top level container. That is either JFrame or JApplet(for Swing). There is also JWindow, but using that requires more experience.

2) Do not set the layout to null. It is better to try to work within the Swing framework. There are 2 tutorials that you should look at to gain a better understanding of how Swing works. http://java.sun.com/docs/books/tutorial/uiswing/index.html and http://java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/index.html. The second one is old, but still relevent. Layout management has not changed, other than to add newer layout mangers. If you go to http://java.sun.com/developer/onlineTraining/GUI/ you will find a series of 5 tutorials called "Building an Application".

3) Do not do as smc suggested. If you want to do custom painting, subclass JPanel and do it there. I will give you an example below. When you start a Java application there a multiple threads that get started. Your application is in the main thread. There is also the event dispatch thread. All paint methods are called from update. And all update methods are called from the event dispatch thread if any repainting is required. So if you override the paint method, all your custom painting code will be used. So instead of an empty JPanel, you have your own graphics. Do not mix custom painting with adding components to the JPanel until you have more experience.

Example of a custom JPanel:
public class MyPanel extends JPanel {  private Image background;  private Dimension size;  public MyPanel() {    image = //some code to load the image    size = new Dimension(image.getWidth(null), image.getHeight(null));  }  public void paint(Graphics g) {    g.drawImage(image, 0, 0, this);  }  //this will ensure that layout managers lay your component out  //properly.  public Dimension getPreferredSize() {    return size;  }}
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
Alpha_ProgDes
Alpha_ProgDes
Another question. If in an application you put what you want the code to do in main(), where are you supposed to put it in a JApplet?
Beginner in Game Development?  Read here. And read here.  
Megaman_22
Megaman_22
Quote:
Original post by Alpha_ProgDes
Another question. If in an application you put what you want the code to do in main(), where are you supposed to put it in a JApplet?


I believe you would put it in either the init() or start() functions.
Eric Richards
CaptainJester
CaptainJester
If you look at the tutorials that I linked, your answers are there.

With applets, init() is called when your applet is first launched, so all start up code should go there.

stop() is called everytime people navigate away from the page with your applet

start() is called everytime that they come back, including the first time, right after init() is called.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
Alpha_ProgDes
Alpha_ProgDes
Ok so from what I've been reading from other tutorials. Whatever code I'd normally put in main(), I can put in either init() or start(). Makes no difference right?
Beginner in Game Development?  Read here. And read here.  
Son of Cain
Son of Cain
Everything you need on pre-render time, put on init(); Things that are dependent of your applet's resources, should go on start(); Basically, it's good OOP practice: you use init() to put your 'component' in a valid state, and the code in start() assumes your 'component' is already in a valid state (i.e, no defensive programming *should* be necessary at this point, if dealing with initialized resources).

[edit: poor grammar - not that it helped much, though =]
a.k.a javabeats at yahoo.ca

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.