Wednesday, April 15, 2015

Cannot Link ActionListener with ActionEvent


I need to crerate the interface for the input iof data for rectangle and then to display it by coordinates (and color fill).

Here is rectangl class, rectanglview class, mainview (input) class, Controller, Aplication classes.

1)

package recct;

import java.awt.Color;

import java.awt.EventQueue;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.geom.Ellipse2D;

import java.awt.geom.Rectangle2D;

import javax.swing.JComponent;

import javax.swing.JFrame;

import java.lang.Object;

import java.awt.geom.RectangularShape;

import java.awt.geom.Rectangle2D.Double;


public class Pkutnyk extends Rectangle2D.Double {

private double x1;

private double y1;

private double w;

private double h;

private String color;

public Pkutnyk(double x1, double y1, double w, double h) {

this.x1=x1;

this.y1=y1;

this.w=w;

this.h=h;

}


public double getWidth(){ return w;}

public double getHeight(){ return h;}

public double getX1(){return x1;}

public double getY1(){return y1;}


public String getColor() {

return color;

}


public void setColor(String color) {

this.color = color;

}

}

2)

package recct;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.Insets;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class MainView extends JFrame {

private JLabel firstPointX = new JLabel(

"Enter X coordinate of first point");

private JTextField firstPointXt = new JTextField(10);

private JLabel firstPointY = new JLabel(

"Enter Y coordinate of first point");

private JTextField firstPointYt = new JTextField(10);

private JLabel secondPointX = new JLabel(

"Enter X coordinate of second point");

private JTextField secondPointXt = new JTextField(10);

private JLabel secondPointY = new JLabel(

"Enter Y coordinate of second point");

private JTextField secondPointYt = new JTextField(10);

private JLabel rectangleColor = new JLabel("Enter Color");

private JTextField rectangleColort = new JTextField(10);

private JButton button = new JButton("Draw");

public MainView() {

JPanel panel = new JPanel();

this.setTitle("Main");

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

this.setSize(200, 500);

this.setLocationRelativeTo(null);

this.setLayout(new GridBagLayout());

this.setVisible(true);

panel.add(firstPointX);

panel.add(firstPointXt);

panel.add(firstPointY);

panel.add(firstPointYt);

panel.add(secondPointX);

panel.add(secondPointXt);

panel.add(secondPointY);

panel.add(secondPointYt);

panel.add(rectangleColor);

panel.add(rectangleColort);

panel.add(button);

this.add(panel, new GridBagConstraints(0, 0, 1, 1, 1, 1,

GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(

2, 2, 2, 2), 0, 0));

}

public double getFirstPointX() {

return Double.parseDouble(firstPointXt.getText());

}

public double getFirstPointY() {

return Double.parseDouble(firstPointYt.getText());

}

public double getWidtht() {

return Double.parseDouble(secondPointXt.getText())-getFirstPointX();

}

public double getHeightt() {

return Double.parseDouble(secondPointYt.getText())-getFirstPointY();

}

public String getrectangleColor() {

return rectangleColort.getText();

}

public void addButtonListener(ActionListener listenForButton) {

button.addActionListener(listenForButton);

}

void displayErrorMessage(String errorMessage){

JOptionPane.showMessageDialog(this, errorMessage);

}

}


3)

package recct;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import javax.swing.JFrame;

import javax.swing.JPanel;

import java.awt.geom.Rectangle2D;

public class RectangleView extends JPanel {

double x;

double y;

double w;

double h;


public void paintComponent(Graphics g) {

Graphics2D g2 = (Graphics2D) g;

Pkutnyk rect = new Pkutnyk(x, y, w, h);

g2.setPaint(Color.RED);

g2.fill(rect);


}

public void main() {

RectangleView rects = new RectangleView();

JFrame frame = new JFrame("Pryamokutnyk");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

frame.add(rects);

frame.setSize(360, 300);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

}

4)

package recct;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;


public class Controller {


private Pkutnyk pkut;

private MainView mainView;

private RectangleView rview;


public Controller(Pkutnyk pkut, MainView mainView) {

this.pkut = pkut;

this.mainView = mainView;

this.mainView.addButtonListener(new ButtonListener());

}


class ButtonListener implements ActionListener {


public void actionPerformed(ActionEvent arg0) {

try {

rview.x = mainView.getFirstPointX();

rview.y = mainView.getFirstPointY();

rview.w = mainView.getWidtht();

rview.h = mainView.getHeightt();

rview.main();

mainView.setVisible(false);

rview.setVisible(true);


} catch (NumberFormatException ex) {

System.out.println(ex);


}

}

}

}


5)

package recct;

public class Appl {

public static void main(String[] args) {

MainView mainView = new MainView();

RectangleView rectView=new RectangleView();

mainView.setVisible(true);

rectView.setVisible(false);

}

}



No comments:

Post a Comment