Results 1 to 6 of 6
  1. #1
    lordbob75 is offline Member
    Join Date
    Nov 2008
    Location
    Earth
    Posts
    14
    Rep Power
    0

    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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    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.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    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:
    Java 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 [i]new[/i] 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;
        }
    }

  4. #4
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    Create separate java files for each class and make the classes as public class.

  5. #5
    lordbob75 is offline Member
    Join Date
    Nov 2008
    Location
    Earth
    Posts
    14
    Rep Power
    0

    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....

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    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:
    Java Code:
    public class MultiClass extends JApplet {
        View view = new View();
        Control control = new Control(view);

Similar Threads

  1. different multiple public class and main class
    By mr idiot in forum New To Java
    Replies: 2
    Last Post: 01-01-2009, 12:10 PM
  2. Replies: 0
    Last Post: 11-15-2008, 10:09 PM
  3. Replies: 2
    Last Post: 10-06-2008, 09:06 PM
  4. multiple class files
    By nemesys571 in forum Eclipse
    Replies: 0
    Last Post: 06-26-2008, 01:58 AM
  5. Replies: 4
    Last Post: 06-08-2007, 06:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •