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();
}
}
} } }