Steve
Steve

Reputation: 11

Passing variables from method to draw a line - Java

I want to call a method from a button click and from that method I want to pass variables to the class DrawPanel which extends Jpanel to draw a line. I have 4 classes, MainFem, DrawPanel, DrawLine and Callmethod1.

The problem is that the new values don't get added in the LinkedList.

Main class: MainFem

package paintpack;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;



public class MainFem extends JFrame {

JPanel buttonsPanel = null;
JButton dcomp = null;
DrawPanel drawPanel = null;

  public MainFem(){

  add(getDrawPanel(),BorderLayout.CENTER);

  add(getButtonPanel(), BorderLayout.SOUTH);
  getButtonPanel().add(getDcomp());
  }

  private JPanel getButtonPanel() {
  if(buttonsPanel == null){
      buttonsPanel = new JPanel();
       buttonsPanel.setBorder(BorderFactory.createLineBorder(Color.black));
      buttonsPanel.setPreferredSize(new Dimension(500, 100));
  }

  return buttonsPanel;
  }



  private DrawPanel getDrawPanel(){
  if(drawPanel == null){
      drawPanel = new DrawPanel();
      drawPanel.setVisible(true);
  }

  return drawPanel;
  }



  private JButton getDcomp() {
  if(dcomp == null){
      dcomp = new JButton("Click");
      dcomp.addActionListener(new ActionListener() {

          @Override
          public void actionPerformed(ActionEvent e) {
              Callmethod1 call1 = new Callmethod1();
              call1.passmethod1();
              getDrawPanel().repaint();
          }
      });
  }
  return dcomp;
  }

public static void main(String[] args) {
MainFem wnd = new MainFem();


wnd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
wnd.setPreferredSize(new Dimension(800, 600));
Container c = wnd.getContentPane();
c.setBackground(Color.red); 
wnd.pack();
wnd.setVisible(true);



}

}

DrawPanel class

package paintpack;


import java.awt.Graphics;

import javax.swing.JPanel;



public class DrawPanel extends JPanel{
/**
 * 
 */
private static final long serialVersionUID = 1L;
private DrawLines dlines; 

public DrawPanel() {
    dlines = new DrawLines();
}

public void method1(int x1, int y1, int x2, int y2) {
    dlines.addLine(x1, y1, x2, y2);
    repaint();
    //System.out.println("bingo");
}



// beging PAINTING
@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    dlines.draw(g);

}

}

DrawLines

package paintpack;


import java.awt.Graphics;
import java.util.LinkedList;

public class DrawLines{

public DrawLines() {

}

private static class Line {
    final int x1;
    final int y1;
    final int x2;
    final int y2;

    public Line(int x1, int y1, int x2, int y2) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;

    }

}

private final LinkedList<Line> lines = new LinkedList<Line>();

public void addLine(int x1, int y1, int x2, int y2) {

    lines.add(new Line(x1, y1, x2, y2));
}

public void clearLines() {
    lines.clear();
}

public void draw(Graphics g){
    for (Line line : lines) {
        g.drawLine(line.x1, line.y1, line.x2, line.y2);
    }
}

}

Callmethod1

package paintpack;


public class Callmethod1 {
private DrawPanel pass1;

public Callmethod1() {
pass1 = new DrawPanel();
}

public void passmethod1(){
pass1.method1(300, 300, 200, 200);
pass1.repaint();
}

}

Upvotes: 0

Views: 1004

Answers (1)

tenorsax
tenorsax

Reputation: 21233

The problem is that you allocate a new instance of DrawPanel in the constructor of Callmethod1. So you are drawing lines on another instance of DrawPanel which is invisible. To fix it just pass an instance of existing DrawPanel, ei:

public Callmethod1(DrawPanel drawPanel) {
    this.pass1 = drawPanel;
}

And in the action listener:

@Override
public void actionPerformed(ActionEvent e) {
    Callmethod1 call1 = new Callmethod1(drawPanel);
    call1.passmethod1();
}

As a side note, you can utilize existing Line2D class to draw lines.

Upvotes: 1

Related Questions