Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-06-2009, 11:38 PM
Member
 
Join Date: Nov 2008
Location: Earth
Posts: 14
Rep Power: 0
lordbob75 is on a distinguished road
Send a message via AIM to lordbob75
Default Multiple class applet
Hey I am still just learning Java programming, and I am working on a program for Warhammer 40K (yeah im a nerd) that will cut out a lot of the dice rolling involved.

Anyways, I am making the menu for now, and I have one class for each menu so far, and I want to link all the classes to run from one master class that runs the applet. I tried to do it all in one class, but it would have been thousands of lines, and many linking problems I just didnt want to deal with. Anyone know how to link the classes that way? My teacher suggested Object references, but i dont know how to use them. Any suggestions?
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 01-07-2009, 12:13 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,373
Rep Power: 8
Fubarable is on a distinguished road
Default
There is no difference on how you'd use multiple classes here with applets as you would with any java project that uses multiple classes. Have you created a class previously that was used by another class? I'm sorry, but I'm not quite sure exactly what your question is.

Best of luck.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-07-2009, 09:13 AM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
Default
Anyone know how to link the classes that way? My teacher suggested Object references, but i dont know how to use them. Any suggestions?
Yes. There are many ways to put things together. Here's an example to give you an idea:
Code:
// <applet code="MultiClass" width="300" height="300"></applet>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class MultiClass extends JApplet {
    // Object references can be declared:
    // ClassType variableName;
    // or declared and instantiated:
    // ClassType reference/variable = new Object();
    // Object on right hand side is created/instantiated
    // with the new operator. A reference to it,
    // saved in a member variable, is what we use to
    // access the objects fields and methods.
    // Use the "view" variable to access fields and call
    // methods in this new instance of the View class.
    View view = new View();
    // Create an instance of the Control class and save
    // a reference to it (the new object) in the member
    // variable "control".
    Control control = new Control(view);

    public void init() {
        System.out.println(getLayout().getClass().getSimpleName());
        add(view);
        add(control.getControlPanel(), "Last");
    }
}

class View extends JPanel {
    Color color = Color.blue;
    Point center = new Point(150,125);
    int side = 100;

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(color);
        g.drawRect(center.x-side/2, center.y-side/2, side, side);
    }

    public void setCenterX(int x) {
        center.x = x;
        repaint();
    }

    public void setCenterY(int y) {
        center.y = y;
        repaint();
    }

    public void setColor(Color color) {
        this.color = color;
        repaint();
    }
}

class Control implements ActionListener, ChangeListener {
    // Object reference to the instance of the View class
    // being shown in the applet. You can use this object
    // reference to access fields and call methods in this
    // instance of View.
    View view;
    JSlider xSlider;
    JSlider ySlider;

    public Control(View view) {
        this.view = view;
    }

    public void actionPerformed(ActionEvent e) {
        Color color = ((JButton)e.getSource()).getBackground();
        view.setColor(color);
    }

    public void stateChanged(ChangeEvent e) {
        JSlider slider = (JSlider)e.getSource();
        int value = slider.getValue();
        if(slider == xSlider) {
            view.setCenterX(value);
        }
        if(slider == ySlider) {
            view.setCenterY(value);
        }
    }

    public JPanel getControlPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(2,2,2,2);
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        panel.add(getSliderPanel(), gbc);
        panel.add(getButtonPanel(), gbc);
        return panel;
    }

    private JPanel getSliderPanel() {
        Point p = view.center;
        xSlider = getSlider(p.x);
        ySlider = getSlider(p.y);
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(2,2,2,2);
        addComponents("x", xSlider, panel, gbc);
        addComponents("y", ySlider, panel, gbc);
        return panel;
    }

    private JSlider getSlider(int value) {
        JSlider slider = new JSlider(50, 250, value);
        slider.addChangeListener(this);
        return slider;
    }

    private void addComponents(String s, JSlider slider, Container c,
                               GridBagConstraints gbc) {
        gbc.weightx = 0;
        gbc.fill = GridBagConstraints.NONE;
        c.add(new JLabel(s), gbc);
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        c.add(slider, gbc);
    }

    private JPanel getButtonPanel() {
        Color[] colors = {
            Color.red, Color.green.darker(), Color.blue, Color.orange
        };
        JPanel panel = new JPanel(new GridLayout(1,0));
        for(int i = 0; i < colors.length; i++) {
            JButton button = new JButton(" ");
            button.setBackground(colors[i]);
            button.addActionListener(this);
            panel.add(button);
        }
        return panel;
    }
}
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-07-2009, 09:16 AM
Senior Member
 
Join Date: Dec 2008
Posts: 164
Rep Power: 2
dswastik is on a distinguished road
Default
Create separate java files for each class and make the classes as public class.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-07-2009, 11:47 PM
Member
 
Join Date: Nov 2008
Location: Earth
Posts: 14
Rep Power: 0
lordbob75 is on a distinguished road
Send a message via AIM to lordbob75
Default
Ok, so is the slideshow class supposed to link the classes togetther? I am only a beginning programmer, that confused me a little bit....
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-08-2009, 02:22 AM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
Default
is the slideshow class supposed to link the classes togetther?
The linking of classes is usually done in the class that has the main method and builds/shows the gui (graphical user interface). This is a logical place to assemble/put everything (components) together.
In the example the linking is done in the JApplet, here:
Code:
public class MultiClass extends JApplet {
    View view = new View();
    Control control = new Control(view);
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
different multiple public class and main class mr idiot New To Java 2 01-01-2009 01:10 PM
How do I store multiple Strings with an inherited class? trojansc82 New To Java 0 11-15-2008 11:09 PM
[SOLVED] Hacking Singleton- Multiple instance of a Singleton class piyu.sha New To Java 2 10-06-2008 10:06 PM
multiple class files nemesys571 Eclipse 0 06-26-2008 02:58 AM
Applet, To center text and To open I engage in a dialog in an Applet Marcus Java Applets 4 06-08-2007 07:15 AM


All times are GMT +2. The time now is 09:46 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org