Change the size of the circle structure of MVC practice

MVC frame is divided into a control (data acquired in the acquisition radius of a circle here), the model (where the radius of the circle is stored store data), the view (corresponding to a circle drawn)
code is as follows:
Model:

在这里插入代码片
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class model {
	private double r=10;
	private ArrayList<ActionListener> a;
	public double getr() {
		return r;
	}
	public void setr(double r) {
		this.r=r;
		process(new ActionEvent(this,ActionEvent.ACTION_PERFORMED,"r"));
	}
	public synchronized void addActionListener(ActionListener l){
		if(a==null)
			a=new ArrayList<ActionListener>();
		a.add(l);
	}
	public synchronized void removeActionListener(ActionListener l) {
		if(a!=null&&a.contains(l))
			a.remove(l);
	}
	public void process(ActionEvent e) {
		ArrayList list;
		synchronized(this) {
			if(a==null)
				return;
			list=(ArrayList)a.clone();
		}
		for(int i=0;i<list.size();i++)
			((ActionListener)list.get(i)).actionPerformed(e);
	}
}

View achieve listener ActionListener, to listen for notification from the magic. As it attempts to model the properties included. After setting the model in the view, trying to register to the model. View of the cover and extend JPanel paintComponent method to draw a circle according to the attributes specified in the model.

view:

在这里插入代码片
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;

public class view extends JPanel implements ActionListener{
private model model1;
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO 自动生成的方法存根
		repaint();
	}
	public void setmodel(model m) {
		this.model1=m;
		if(this.model1!=null)
			this.model1.addActionListener(this);
		repaint();
	}
	public model getmodel(){
		return this.model1;
	}
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if(model1==null)
			return;
		int x=getWidth()/2;
		int y=getHeight()/2;
		int r=(int)model1.getr();
		g.drawOval(x-r, y-r, 2*r, 2*r);
	}

}

Controls are given GUI interface, through which users can input attribute human r circle. Control model as its attribute included. You can use methods associated setModel round model and controls. Controls use a text field to obtain a new radius value.

Controls:

在这里插入代码片
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class control extends JPanel{
private model model1;
private JTextField shuru=new JTextField(8);
public control() {
	add(new JLabel("R"));
	add(shuru);
	shuru.addActionListener(new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO 自动生成的方法存根
			if(model1==null)
				return;
			model1.setr(new Double(shuru.getText()).doubleValue());
		}});
}

public void setmodel(model m) {
	model1=m;
}
public model getmodel() {
	return model1;
}
}

Test categories:

在这里插入代码片
import java.awt.BorderLayout;

import javax.swing.JApplet;

public class test extends JApplet{
model model1=new model();
public test() {
	setLayout(new BorderLayout());
	control control1=new control();
	control1.setmodel(model1);
	add(control1,BorderLayout.SOUTH);
	view view1=new view();
	view1.setmodel(model1);
	add(view1,BorderLayout.CENTER);
}
}

Models for storing and processing data, display data view is responsible. Model - The basic method is to maintain the view point and the view of the model consistency. Any change in the model should be associated with such through-view. All views on the same model should be the same number of electroluminescent display W. Data model also through controls to modify.

Published 130 original articles · won praise 16 · views 30000 +

Guess you like

Origin blog.csdn.net/feiqipengcheng/article/details/105128561