Created
March 26, 2016 17:13
-
-
Save umuturan/66b25e408af42de3da02 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package MVC; | |
| /* | |
| *LAB03 | |
| *Circle CLASS | |
| * Extends Shape implements Selectable | |
| *Author:@Umut Can Turan | |
| *21401929 | |
| *Section 06 | |
| */ | |
| public class Circle { | |
| // properties | |
| double radius; | |
| double circumference; | |
| // constructor | |
| public Circle(double radius) { | |
| this.radius = radius; | |
| this.circumference= 2*Math.PI*radius; | |
| } | |
| // methods | |
| public double getArea() { | |
| return Math.PI * Math.pow(radius, 2); | |
| } | |
| public double getRadius(){ | |
| return radius; | |
| } | |
| public double getCircumference(){ | |
| return circumference; | |
| } | |
| public String toString() { | |
| return "Circle with radius :" + radius + '\n'; | |
| } | |
| public void setRadius(double radius){ | |
| this.radius=radius; | |
| circumference= 2*Math.PI*radius; | |
| } | |
| public void setCircumference(double circumference){ | |
| this.circumference=circumference; | |
| radius= circumference*1/Math.PI*1/2; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package MVC; | |
| import java.awt.*; | |
| import java.awt.event.ActionEvent; | |
| import java.awt.event.ActionListener; | |
| import javax.swing.*; | |
| public class CircleView extends JFrame{ | |
| static JPanel panel; | |
| static JLabel radLab, cirLab; | |
| static TextField radField, cirField; | |
| public CircleView() { | |
| super("PopQuiz"); | |
| setLayout(new FlowLayout()); | |
| panel = new JPanel(); | |
| panel.setLayout(new FlowLayout()); | |
| radField = new TextField(10); | |
| cirField = new TextField(10); | |
| radLab = new JLabel("Radius"); | |
| cirLab = new JLabel("Circumference"); | |
| panel.add(radLab); | |
| panel.add(radField); | |
| panel.add(cirLab); | |
| panel.add(cirField); | |
| add(panel); | |
| pack(); | |
| setVisible(true); | |
| } | |
| public double getRadField(){ | |
| return Double.parseDouble(radField.getText()); | |
| } | |
| public double getCircField(){ | |
| return Double.parseDouble(cirField.getText()); | |
| } | |
| public void setRadField(double d){ | |
| radField.setText(String.valueOf(d)); | |
| } | |
| public void setCirField(double d){ | |
| cirField.setText(String.valueOf(d)); | |
| } | |
| public void addCircleListener(ActionListener listenCircle){ | |
| radField.addActionListener(listenCircle); | |
| cirField.addActionListener(listenCircle); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package MVC; | |
| import java.awt.event.ActionEvent; | |
| import java.awt.event.ActionListener; | |
| public class CircleController { | |
| public CircleView cView; | |
| public Circle circle; | |
| public CircleController(CircleView cView, Circle cirle) { | |
| this.circle = circle; | |
| this.cView = cView; | |
| this.cView.addCircleListener(new CircleListener()); | |
| } | |
| public class CircleListener implements ActionListener { | |
| @Override | |
| public void actionPerformed(ActionEvent e) { | |
| if (e.getSource() == cView.radField) { | |
| System.out.println(circle); //bu TestMVC de bi circle olusturmama ragmen | |
| //her zaman null donuyor | |
| circle.setRadius(cView.getRadField()); | |
| cView.setCirField(circle.getCircumference()); | |
| } else if (e.getSource() == cView.cirField) { | |
| circle.setRadius(cView.getCircField()); | |
| cView.setRadField(circle.getRadius()); | |
| } | |
| cView.repaint(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package MVC; | |
| public class TestMVC { | |
| public static void main(String args[]){ | |
| CircleView c1 = new CircleView(); | |
| Circle circ = new Circle(7); | |
| CircleController cont = new CircleController(c1, circ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment