我有几个问题,请原谅我的英语!问题:
public class JuliaController extends WindowAdapter implements ActionListener {
private JuliaView view;
private JuliaModel model;
private String linkBuffer;
public JuliaController(){
model = new JuliaModel();
System.out.println("true!");
view = new JuliaView("JuliaMenge");
view.makeView();
}
@Override
public void actionPerformed(ActionEvent arg0) {
String action = arg0.getActionCommand();
if(action.equals(view.ACTION_CLEAR))
{
//Clear Graphics
view.setBtClear();
}
if(action.equals(view.ACTION_COMPLEX))
{
String input = view.getComplex();
view.setTfComplex(); //Zurücksetzen des Felds!
model.juliaBerechnung(input);
view.getBtPaint(); //Setzt den Button auf anklickbar
//Paint Button anklickbar machen
}
if(action.equals(view.ACTION_ENDE))
{
view.release();
System.exit(0);
}
if(action.equals(view.ACTION_LINK))
{
String inLink = view.getTfLink();
view.setTfLink();
try {
model.juliaBerechnung(dataInList(inLink));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
view.getBtPaint();
}
if(action.equals(view.ACTION_PAINT))
{
//Erzeuge Graphics!
view.getBtPaint(); //Button nicht mehr anklickbarkeit
}
}
public String dataInList(String link)throws IOException
{
String temp="";
BufferedReader inBuffer = null;
try {
inBuffer = new BufferedReader(new FileReader(new File(link)));
while((temp=inBuffer.readLine())!=null)
{
//System.out.println("Buffer : "+temp);
linkBuffer += temp;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int dateiLength = linkBuffer.length();
inBuffer.close();
char c;
int index=0;
for(int i = index;index<dateiLength;index++){
c = linkBuffer.charAt(index);
if((c=='+')||(c=='-'))break;
}
temp = "";
for(int i=index;i<index+8;i++)
{
temp+=linkBuffer.charAt(i);
}
return temp; //Keine Anhang in der Datei
}
public void release() {
// TODO Auto-generated method stub
model = null;
view = null;
}
public void windowClosing( WindowEvent we)
{
view.release();
}
public class JuliaView extends JFrame implements Observer{
//:::: ACTIONS
public final String ACTION_ENDE = "Ende";
public final String ACTION_PAINT = "Paint";
public final String ACTION_COMPLEX = "+a.x+b.x";
public final String ACTION_LINK = "Link";
public final String ACTION_CLEAR = "Clear";
//:::: Components
private JButton btEnde;
private JButton btPaint;
private JButton btClear;
public JuliaPanel drawArea; //Bereich für Paint
//private JButton btAutoPaint;
private JTextField tfComplex;
private JTextField tfLink;
//:::: Observer
private JuliaModel model;
private JuliaController controller;
private JuliaBild map;
public JuliaView(String titel){
super(titel);
this.model = model;
//this.map = model.getMap(); //??????//Iterationsarray einbinden
//this.model.addObserver(this);
//controller = makeController();
initForm();
//makeView();
}
void makeView() {
resetView();
// Fenster
addWindowListener( controller);
pack();
setVisible( true);
}
/*private JuliaController makeController() { ?????????????????
return new JuliaController();
}*/
/**
* Anordnen der Komponenten im GridBag
*/
private void initForm(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new GridBagLayout());
this.setBounds(200, 200, 800, 600);
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(15,15,15,15);
//this.add(btAutoPaint,c);
// TextField für Kompleze Zahl
tfComplex = new JTextField(ACTION_COMPLEX,8);
tfComplex.addActionListener(controller);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
this.getContentPane().add(tfComplex,c);
// Link
tfLink = new JTextField(ACTION_LINK);
tfLink.addActionListener(controller);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 2;
this.getContentPane().add(tfLink,c);
// Paint Button
btPaint = new JButton(ACTION_PAINT);
btPaint.setEnabled(false);
btPaint.addActionListener(controller);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 2;
c.ipady = 40;
this.getContentPane().add(btPaint,c);
//CLS Button
btClear = new JButton(ACTION_CLEAR);
btClear.addActionListener(controller);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 4;
c.gridwidth = 2;
this.getContentPane().add(btClear,c);
//Exit Button
btEnde = new JButton(ACTION_ENDE);
btEnde.addActionListener(controller);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 5;
c.gridwidth = 1;
this.getContentPane().add(btEnde,c);
//draw area
drawArea = new JuliaPanel(); //Place for the JuliaSet
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 0;
c.gridwidth = 6;
this.getContentPane().add(drawArea,c);
setVisible( true);
pack();
}
public void resetView(){
this.tfComplex.setText("+a.x+b.x");
this.tfLink.setText("c:/...");
//drawArea.paint(g);
}
@Override
public void update(Observable m, Object o) {
// TODO Auto-generated method stub
if(model == m) setBtPaint(); //???
}
public void setBtPaint()
{
btPaint.setText(ACTION_PAINT);
btPaint.requestFocus();
btPaint.setEnabled(true);
drawArea.setKoordinaten(map);
//Zeichnen
//drawArea.repaint();
drawArea.print(getGraphics());
//drawArea.paint(getGraphics());
}
public void getBtPaint()
{
btPaint.setText(ACTION_PAINT);
drawArea.createImage();
btPaint.requestFocus();
btPaint.setEnabled(false); //anklickbarkeit
}
public void setBtClear()
{
btClear.setText(ACTION_CLEAR);
resetView();
drawArea.clearImage();
btClear.requestFocus();
}
public void setTfComplex()
{
tfComplex.setText(ACTION_COMPLEX);
tfComplex.setCaretPosition(tfComplex.getText().length());
tfComplex.requestFocus();
}
public void setTfLink()
{
tfLink.setText(ACTION_LINK);
tfLink.setCaretPosition(tfLink.getText().length());
tfLink.requestFocus();
}
public void getBtEnde()
{
btEnde.setText(ACTION_ENDE);
btEnde.requestFocus();
}
public String getComplex()
{
String temp = "";
temp = tfComplex.getText();
return temp;
}
public String getTfLink()
{
String temp = tfLink.getText();
return temp;
}
public void release(){
dispose();
// Controller
controller.release();
controller = null;
// Model
model.deleteObserver( this);
model = null;
}
}型号:
public class JuliaModel extends Observable{
//private Complex startWert;
private Complex konst;
private String pfad;
private JuliaBild map;
public JuliaModel()
{
//startWert = new Complex(re, im);
konst = new Complex(1.0,1.0);
pfad = "";
makeBild();
}
private void makeBild()
{
map = new JuliaBild(600,600);
System.out.println("bild true");
}
public JuliaModel(double reK, double imK)
{
map = new JuliaBild(600,600);
//startWert = new Complex(re, im);
konst = new Complex(reK,imK);
pfad = "";
}
private int grundAlgoIt(double x,double y)
{
Complex temp = new Complex(x,y);
double xTemp=0;
for(int n=0;n<256;n++)
{
if(temp.abs()<4){return n;}
xTemp = Math.pow(temp.getRe(), 2)-Math.pow(temp.getIm(), 2) + konst.getRe();
temp.setIm((2*temp.getIm()*temp.getRe())+konst.getIm());//y=
temp.setRe(xTemp);
}
return 0;
}
void juliaBerechnung(String input)
{
double xmin = -2.0;//fx
double ymin = -2.0;//fy
double width = 4.0;//fw //Weg im Koordinatensystem
double height = 4.0;//fh
double schrittWeite = 4.0/600;
stringToKonst(input);
//Complex temp = new Complex(xmin-schrittWeite,1);
double xTemp,yTemp;
xTemp = xmin-schrittWeite;
//durch die Pixel gehen
for(int j=0;j<600;j++)
{
xTemp = xTemp+schrittWeite;
yTemp = ymin;
for(int i=0;i<600;i++)
{
yTemp = yTemp + schrittWeite;
map.setBild(j,i,grundAlgoIt(xTemp,yTemp));
}
}
}
private void stringToKonst(String in)
{
char cBuffer;
String sBuffer="";
double im = 0;
double re = 0;
for(int a=0;a<=4;a+=4)
for(int i=0;i<4;i++)
{
cBuffer = in.charAt(i+a);
sBuffer+=cBuffer;
if(a==0&&i==4)
{
re = Double.parseDouble(sBuffer);
}
if(a==4&&i==4)
{
im = Double.parseDouble(sBuffer);
}
}
konst.setNumber(re, im);
}
public JuliaBild getMap()
{
return map;
}}
谢谢你的帮助!
我在控制器中执行的操作没有反应
这是因为您从不向能够生成ActionEvent
的任何东西注册控制器
而且我不知道如何在视图中使用update(model,object)方法。
是的,这是为您准备的GridbagLayout
不是最后一个。在视图的第41行,我得到一个NPE作为在模型中初始化的类的结果。不是为什么。我已经准备好了,如果这个类被构造好了,是的,它是
哪一个是41号线?堆栈跟踪是什么样子的?
有关该主题的更多讨论,请参阅:
我正在与AngularJS项目工作,我有以下问题。我有个密码: form.html: 使用这段代码,someVariable不会显示,但当我将ng-model更改为someVariable.SomeField时,我试图显示它...管用!有人能解释一下为什么吗?
我试图提出一种可重用的模式,用于在结合使用Spring数据和Spring MVC时更新MongoDB文档。 用例通常可以总结为: 使用存储库在Mongo中创建文档。保存() 如果我使用存储库。save()方法在步骤3中,我将丢失文档中未绑定到表单的所有数据。让表单负责整个文档是很脆弱的,因此MongoTemplate的findAndModify()方法似乎就在这里派上了用场。 要使用findAnd
问题内容: 我已经阅读了有关此问题的主题,例如:[\AngularJS中未更新视图,\但我仍然不明白如何在我的简单示例中应用它。 我有这个功能: 当代码中的其他地方更新时(用户单击,交互,发送XHR请求时),它不会更新我的视图。我知道我需要使用$apply做些事情,但我不知道在哪里以及如何做。 有人可以向我解释如何针对这个简单用例解决此问题吗? 我的模型看起来像这样(如果这个问题是必要的)-它里面
我现在迷路了。
问题内容: 我用来渲染一个。我的控制器看起来像这样: 如您所见,我正在定期更新阵列。但是,我的观点是这样构造的: 问题是,当我更新时,它不会用新数据重新渲染我的表。 我将如何解决这个问题? 问题答案: 您是否将更新包入 ?这应该可以解决问题。
问题内容: 我试图弄清楚Angular的工作原理,并在模型更改时无法更新视图。 的HTML JS http://jsfiddle.net/N2G7z/ 有任何想法吗? 问题答案: 正如上面提到的Ajay beniwal一样,您需要使用Apply来开始消化。