Reputation: 1
So basically I have a main (DrawDriver) where I create 2 objects at the top.
public class DrawDriver {
public static void main(String[] args) {
final GraphicPanel pannel1 = new GraphicPanel();
final Frame2UserInput pannel2 = new Frame2UserInput();
TitledBorder border = new TitledBorder("Input");
border.setTitleColor(Color.BLACK);
pannel2.setBorder(border);
JFrame frame = new JFrame("DrawGoGo!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(500, 500));
frame.setLayout(new GridLayout(2, 0));
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
// Creating the MENU BAR
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
JMenu file = new JMenu("File");
menubar.add(file);
JMenuItem New = new
JMenuItem("New"); // Creates the jmenu item
file.add(New); // adds
JMenuItem Load = new JMenuItem("Load");
file.add(Load);
JMenuItem Save = new JMenuItem("Save");
file.add(Save);
JMenuItem Exit = new JMenuItem("Exit");
file.add(Exit);
JMenu help = new JMenu("Help");
menubar.add(help);
JMenuItem about = new JMenuItem("About");
help.add(about);
frame.getContentPane().add(pannel1);
frame.getContentPane().add(pannel2);
}
In Frame2UserInput I want to access the GraphicPanel instance through the DrawDriver (main) class. This will be a canvas where I can type commands in & click the button & then the canvas will draw accordingly. You can see I've attempted to access it already in my ActionListener but can't because it makes a new instance of the class & not changes anything in the program (gui) I run.
So how would I access the object I created for GraphicPanel in the main but through a different class ? Any help would be appreciated thank you!
public class Frame2UserInput extends JPanel {
private static JTextArea input;
private static JButton draw;
Frame2UserInput() {
input = new JTextArea(2,35);
add(input);
draw = new JButton("Draw!");
draw.addActionListener(new DrawListener());
add(draw);
}
private static class DrawListener implements ActionListener {
private String userInput;
GraphicPanel pannel1 = new GraphicPanel();
public void actionPerformed(ActionEvent event) {
if (event.getSource()==draw) {
userInput = input.getText();
System.out.println(userInput);
if (userInput.equalsIgnoreCase("penup")) {
}
if (userInput.equalsIgnoreCase("pendown")) {
}
if (userInput.equalsIgnoreCase("turnleft")) {
}
if (userInput.equalsIgnoreCase("turnright")) {
}
if (userInput.equalsIgnoreCase("black")) {
}
if (userInput.equalsIgnoreCase("green")) {
}
if (userInput.equalsIgnoreCase("red")) {
pannel1.drawLine(Color.RED,0,0,100,100);
}
if (userInput.equalsIgnoreCase("reset")) {
pannel1.clear();
}
}
} } }
Upvotes: 0
Views: 72
Reputation: 461
You can
Upvotes: 1
Reputation: 347294
Basically, you want to start by setting an Observer Pattern, where DrawDriver
listens to events from Frame2UserInput
and makes changes to the instance of GraphicPanel
.
This is also a conceptual example of the Model-View-Controller paragdigm.
The idea here is, neither GraphicPanel
or Frame2UserInput
have any idea of each other (and shouldn't really know about DrawDriver
either). DrawDriver
is the bridge between the two, passing information between them as needed.
This will decouple your code and make it easier to modify and expand in the future, as changing one part shouldn't change how the other parts work. To this end, you should make heavy use of interface
s to define the core contractual logic between these classes.
Also, don't rely on static
, it's not helping you and you should learn to work without it
Upvotes: 2
Reputation: 3956
Take it as a parameter.
final GraphicPanel pannel1 = new GraphicPanel();
final Frame2UserInput pannel2 = new Frame2UserInput(pannel1 );
New Frame2UserInput
constructor:
Frame2UserInput(GraphicPanel pannel1) {
.
.
draw.addActionListener(new DrawListener(pannel1));
}
And new DrawListener
constructor
DrawListener(GraphicPanel pannel1) {
.
.
//Don't need this just use pannel1
//GraphicPanel pannel1 = new GraphicPanel();
}
Upvotes: 2
Reputation: 1447
The dot operator
object.objectMethod();
the method must be public, or protected if there is a level of inheritance & polymorphism going on
For your case, few pointers:
panel1
or panel2
are awful names for variables.
Next, you'd need to have an object in DrawDriver class to call an objects method from
Upvotes: 1