KDM
KDM

Reputation: 93

Access one class from another

I have a class defined which publishes a method allowing access to a private object within:

    public class HexBoard {

[...]

        public HexBoard(int Width, int Height, boolean Wrap) {
            SetSize(Width, Height); // inherently calls Reset()
            SetWrap(Wrap);
        } // HexBoard constructor


        public Polygon GetHexagon(int CellIndex) {

            Polygon p = new Polygon();
            for (int i = 0; i < 6; i++) {
                p.addPoint((int) (HexCentres.X(CellIndex) + HexPoints.X(i)), (int) (HexCentres.Y(CellIndex) + HexPoints.Y(i)));
            }

            return p;
        } // GetHexagon

        public int Cells() { return CellCount; }

    } // HexBoard

You can see that the method creates a polygon and returns it. This bit works well. Now, I have another class, which publishes an extended JPanel in order to draw a hexagon-based playfield consisting of lots of hexagons.

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

public class PCinterface extends JPanel {

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        int CellCount = HexBoard.Cells();
        for (int i = 0; i < HexBoard.Cells(); i++) {
            g.drawPolygon(HexBoard.GetHexagon(i));
        }
    } // paintBoard

} // PCinterface

The problem is that PCinterface knows nothing of HexBoard, so it can't access HexBoard.Cells() or HexBoard.GetHexagon().

When the following is executed

public class Test {

    public static void main(String args[]) {

        BreadBoard Board = new BreadBoard(12,12,false);

        Board.SetCellRadius(25);

        JFrame frame = new JFrame();
        frame.setTitle("BreadBoard");
        frame.setSize(600, 600);
        frame.addWindowListener(new WindowAdapter() {
           public void windowClosing(WindowEvent e) {
              System.exit(0);
           }
        });
        Container contentPane = frame.getContentPane();
        contentPane.add(new PCinterface());
        frame.setVisible(true);
*/
    } // main

} // Test

I would hope it would open a window and draw some hexagons, but I can see that the hexagon based board created in main using HexBoard, doesn't exist in the context of PCinterface.

I can see tat I could readily include PCInterface in main and that would solve the problem: I'm trying to develop for multiple platforms and had hoped this was an appropriate way to separate platform dependant classes.

How can I employ the data held in BreadBoard within the PCInterface class, please?

Upvotes: 0

Views: 840

Answers (2)

Marcos
Marcos

Reputation: 4653

As @HunterMcMillen commented, you need to instantiate HexBoard in order to use the method Cells():

...
HexBoard hexBoard = new HexBoard(width, height, wrap);
int cellCount = hexBoard.Cells();
...

Upvotes: 0

You need an instance of a HexBoard. you might add one to you your PCinterface

public class PCinterface extends JPanel {

    public HexBoard hexBoard

    public PCinterface(HexBoard board)
    {
        this.hexBoard = board;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        int CellCount = this.hexBoard.Cells();
        for (int i = 0; i < this.hexBoard.Cells(); i++) {
            g.drawPolygon(this.hexBoard.GetHexagon(i));
        }
    } // paintBoard

} // PCinterface

assuming that the type of Board, BreadBoard extends HexBoard, you can pass it into the constructor like this

contentPane.add(new PCinterface(Board));

Upvotes: 1

Related Questions