java代码问题,求指点。

sinat_32465411 2016-04-11 11:36:14
java初学者,最近写一个java写的mvc框架下拖动三角形点画三角形的程序,但是现在无法初始运行,不知道问题在哪里,没有报错,debug也不见问题。 因为编程经验不多,不知道该怎么解决,往经验人士zhi dian

----view 1------:

import java.util.Observer;
import java.util.Observable;
import javax.swing.JLabel;

public class AView extends JLabel implements Observer{
private TriangleModel model;

public AView(TriangleModel model) {
super();
this.model = model;

double valueA = model.getA();
double valueB = model.getB();
double valueC = (int) Math.sqrt(model.getA()*model.getA()+model.getB()*model.getB());
double valueD=model.getA()*model.getA()+model.getB()*model.getB();

setText("<html>A="+valueA+" A*A="+valueA*valueA+"<br>"+ "B=" +valueB+" B*B="+valueB*valueB+"<br>"+ "C="+ valueC+"<br>"+ "C*C="+ valueD
+"<br>"+ valueD+"="+valueA*valueA+"+"+valueB*valueB
+"</html>");
}

public void update(Observable os,Object obj){

double valueA = model.getA();
double valueB = model.getB();
double valueC = (int) Math.sqrt(model.getA()*model.getA()+model.getB()*model.getB());
double valueD=model.getA()*model.getA()+model.getB()*model.getB();

setText("<html>A="+valueA+" A*A="+valueA*valueA+"<br>"+ "B=" +valueB+" B*B="+valueB*valueB+"<br>"+ "C="+ valueC+"<br>"+ "C*C="+ valueD
+"<br>"+ valueD+"="+valueA*valueA+"+"+valueB*valueB
+"</html>");
}
}

----view2----- :

import java.util.Observer;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.Shape;

import java.awt.geom.Line2D;

import java.util.Observable;


import javax.swing.JPanel;

import triangle06.TriangleModel;


public class TriangleView extends JPanel implements Observer {



TriangleModel model;

final int originX = 300;
final int originY = 300;
public TriangleView(TriangleModel model) {
super();
this.model= model;

// setPreferredSize(new Dimension(300,300));
}



private void paintBackground(Graphics2D g2){
g2.setPaint(Color.LIGHT_GRAY);
for (int i = 0; i < getSize().width; i += 10) {
Shape line = new Line2D.Float(i, 0, i, getSize().height);
g2.draw(line);
}

for (int i = 0; i < getSize().height; i += 10) {
Shape line = new Line2D.Float(0, i, getSize().width, i);
g2.draw(line);
}


}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
paintBackground(g2);
//
float[] dash={(float) 3.0,(float) 3.0};
g2.setStroke(new BasicStroke((float)2,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER,(float)1.0,dash,(float)0.0));
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.50f));
if(model == null){
model = new TriangleModel(new Triangle(100,200),null,null,true);
}
if (model.startDrag != null && model.endDrag != null) {
int[] x={originX, originX, (int) (originX+model.getB())};
int[] y={originY,(int) (originY+model.getA()),originY};
Polygon poly = new Polygon(x,y,3);

g2.setColor(Color.red);

g2.fill(poly);

}


if (model.dragging = true) {
g2.setPaint(Color.LIGHT_GRAY);

int[] x1={originX, originX, (int) (originX+model.getB())};
int[] y1={originY,(int) (originY+model.getA()),originY};
Polygon poly1=new Polygon(x1,y1,3);;
g2.draw(poly1);
}
}



@Override
public void update(Observable o, Object arg) {
repaint();

}


}

------model------

import java.awt.Point;

import java.awt.event.MouseEvent;

import java.util.Observable;



public class TriangleModel extends Observable{
private Triangle temp;
public Point startDrag, endDrag;
public boolean dragging = false;




public TriangleModel(Triangle temp, Point startDrag, Point endDrag, boolean dragging) {
super();
this.temp = temp;
this.startDrag = startDrag;
this.endDrag = endDrag;
this.dragging = dragging;
}

public Point getStartDrag() {
return startDrag;
}

public void setStartDrag(Point startDrag) {
this.startDrag = startDrag;
}

public Point getEndDrag() {
return endDrag;
}

public void setEndDrag(Point endDrag) {
this.endDrag = endDrag;
}


public double getA(){
return temp.getA();
}

public void setA(int a){
temp.setA(a);
setChanged();
notifyObservers();
}
public double getB(){
return temp.getB();
}

public void setB(int b){
temp.setB(b);
setChanged();
notifyObservers();
}

public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub

}

public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
startDrag = new Point(e.getX(), e.getY());
if(e.getX() == 300||e.getY() == 300){
dragging=true;
}
endDrag = startDrag;
// repaint();
}


public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
startDrag = null;
endDrag = null;
dragging= false;

}


public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}


public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}

public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
endDrag = new Point(e.getX(), e.getY());



}


public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub

}


}

----- GUI------

import javax.swing.JFrame;

public class TriangleGUI {

public static void main(String[] args) {

JFrame frame = new JFrame("triangle");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Triangle temp = new Triangle(100, 200);
TriangleComponent panel = new TriangleComponent(temp);

frame.add(panel);
frame.setVisible(true);
}

}


------component------

import java.awt.BorderLayout;


import javax.swing.JPanel;


public class TriangleComponent extends JPanel {
public TriangleComponent(Triangle temp){
super();

TriangleModel model = new TriangleModel(temp, null, null, true);

AView a = new AView(model);


TriangleView triangle = new TriangleView(model);

model.addObserver(a);

model.addObserver(triangle);



add(BorderLayout.CENTER,triangle);

add(BorderLayout.SOUTH,a);


}



}


------triangle----------:



public class Triangle {
private int c;
private int b;
private int a;


public Triangle(int a, int b) {

this.a=a;
this.b=b;
this.c=(int) Math.sqrt(a*a+b*b);

}


public double getA() {
return a;
}


public void setA(int a) {
this.a = a;

}

public double getB() {
return b;
}


public void setB(int b) {
this.b = b;

}

public double getC() {
return c;
}


public void setC(double c) {
this.c=(int) Math.sqrt(a*a+b*b);

}


}



--------change---------:

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class change implements ChangeListener{
private TriangleModel model;

public change(TriangleModel model) {
super();
this.model = model;

}



public void stateChanged(ChangeEvent c) {
int value1 = (int)Math.abs(model.endDrag.x-300);
int value2 = (int)Math.abs(model.endDrag.y-300);
model.setA(value1);
model.setB(value2);
}

}





...全文
135 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
sinat_32465411 2016-04-12
  • 打赏
  • 举报
回复
引用 1 楼 qq_28224387 的回复:
你是怎么运行的? 有没有:

java -classpath=....  your.package.TriangleGUI
都是放到一个package里运行的,你是说这个吗?
  • 打赏
  • 举报
回复
你是怎么运行的? 有没有:

java -classpath=....  your.package.TriangleGUI

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧