⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 positiongraph.java

📁 Java applet that simullate the balistic model
💻 JAVA
字号:

package balistic;
import java.applet.*;
import java.awt.*;
import java.awt.image.*; 
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.util.*;
import java.text.ParseException;


public class PositionGraph extends Panel implements Projectile.ProjectileListener {
	Projectile dart;
        Image buffer;
        Dimension size; 
        Graphics bufferGraphics;
	ResourceBundle rb;
    String sDistance, sHeight, sVelocity, sTime, sMeters, sMPS, sSec;
    String sXAxis, sYAxis;
	DrawableAxis ordinate, abscissa;
	Point origin;
	double maxDistance = 280.0;  // -v0^2/g
	
	int graphLengthX, graphLengthY;
	
	double scaleDistance;
	
	int dartX, dartY;
	double realX, realY; 
	int newDartX, newDartY;
	
	Rectangle graphArea;
	boolean exists;
        boolean bPleaseRedraw; 
    boolean bWantTrails; 
	boolean bTrails; 
	boolean bStatistics; 
	double endDistance, maxHeight, endVelocity, endTime;
	
	static final Color[] colorList = {Color.red, Color.blue, Color.magenta, Color.orange, Color.green};
	
	int currentColor, newColor;
	Vector shotList;
	static final int maxShots = 5;

	
	public PositionGraph(Projectile vomit, ResourceBundle rBundle) {
	
		dart = vomit;
		bWantTrails = true;
		changeSize();  
		dart.addListener(this); 
		exists = false; 
		bStatistics = false; 
		currentColor = newColor = 0; 
		shotList = new Vector(maxShots);
	    rb = rBundle;
        if (rb != null) {
            sDistance = rb.getString("distance");
            sHeight = rb.getString("height");
            sVelocity = rb.getString("endvelocity");
            sTime = rb.getString("time");
            sMeters = rb.getString("meters");
            sMPS = rb.getString("meterspersecond");
            sSec = rb.getString("seconds");
            sXAxis = rb.getString("xaxis");
            sYAxis = rb.getString("yaxis");
        } else {
            sDistance = "Distanta maxima: ";
            sHeight = "Inaltimea Maxima: ";
            sVelocity = "Viteza: ";
            sTime = "Timpul total: ";
            sMeters = "m";
            sMPS = "m/s";
            sSec = "s";
            sXAxis = "Distanta [m]";
            sYAxis = "Inaltime [m]";
        }
	}

	public void paint(Graphics g) {
		if (!size.equals(this.getSize())) changeSize();
                Rectangle trect = g.getClipBounds();
		bufferGraphics.setClip(trect);
		bufferGraphics.setColor(Color.white);
		if (!graphArea.contains(trect.x,trect.y+trect.height)) {
			// To redraw the panel, first clear it, then draw the axes.
			bufferGraphics.fillRect(0,0,size.width,size.height);
			bufferGraphics.setColor(Color.black);
			ordinate.draw(bufferGraphics,origin.x,origin.y);
			abscissa.draw(bufferGraphics,origin.x, origin.y);
		} else {
			bufferGraphics.fillRect(graphArea.x,graphArea.y,graphArea.width,graphArea.height);
		}
		if (bTrails) {
			shotSummary sS;
			for (int j=0; j<shotList.size(); j++) {
				sS = (shotSummary) shotList.elementAt(j);
				bufferGraphics.setColor(sS.currentColor);
			   for (int k=0; k<sS.iPoints; k++)
    				bufferGraphics.fillOval(sS.shotPoints[k][0]-2,sS.shotPoints[k][1]-2,5,5);
			}
		}
		
		if (exists) {
			dartX = newDartX;  dartY = newDartY;
			bufferGraphics.setColor(colorList[currentColor]);
			bufferGraphics.fillOval(dartX-2,dartY-2,5,5);
		}
		
		if (bStatistics) {
		NumberFormat numform = NumberFormat.getInstance(Locale.US);
			if (numform instanceof DecimalFormat) {
				DecimalFormat decform = (DecimalFormat) numform;
				decform.applyPattern("0.##");
				String s1 = new String(sDistance+" "+decform.format(endDistance)+" "+sMeters);
				String s2 = new String(sHeight+" "+decform.format(maxHeight)+" "+sMeters);
				String s3 = new String(sVelocity+" "+decform.format(endVelocity)+" "+sMPS);
				String s4 = new String(sTime+" "+decform.format(endTime)+" "+sSec);
				Font f = new Font("SansSerif",Font.PLAIN,14);
				bufferGraphics.setFont(f);
				FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(f);
				int ascent = fm.getAscent();
				int spacing = ascent + fm.getLeading();
				int xloc = size.width-fm.stringWidth(s1)-20;
				int it = size.width-fm.stringWidth(s2)-20;
				if (it<xloc) xloc = it;
				it = size.width-fm.stringWidth(s3)-20;
				if (it<xloc) xloc = it;
				it = size.width-fm.stringWidth(s4)-20;
				if (it<xloc) xloc = it;
				int yloc = 10+spacing;
				bufferGraphics.setColor(Color.black);
				bufferGraphics.drawString(s1,xloc,yloc);
				yloc += spacing;
				bufferGraphics.drawString(s2,xloc,yloc);
				yloc += spacing;
				bufferGraphics.drawString(s3,xloc,yloc);
				yloc += spacing;
				bufferGraphics.drawString(s4,xloc,yloc);
			}
		}
		g.drawImage(buffer,0,0,this);
	}

	
	public void update(Graphics g) {
		paint(g);
	}
public void beginFiring(double velocity, double angle,double[] shotStats)
	{
		exists = true;  
		dartX = dartY = 0; 
		bStatistics = false; 
                currentColor = currentColor+1;
		
		if (currentColor>maxShots-1) currentColor = 0;

		setPosition(dartX,dartY,0,0,0,false);
        if (bWantTrails) bTrails = true; else bTrails = false;
		if (!bTrails) shotList.setSize(0);
		else {
			if (shotList.size() > maxShots-1) {
				shotList.removeElementAt(0);
			}
			shotList.addElement(new shotSummary(shotStats,colorList[currentColor]));
		}

		double firemax = velocity*velocity/9.81;
		//firemax = 1.1*firemax;
		
		if (firemax>maxDistance || firemax<0.4*maxDistance) {
			maxDistance = firemax;
			changeSize();
		} else {
			bPleaseRedraw = true;
			repaint();
		}
	}
public void setPosition(double x, double y, double vx, double vy,double time,boolean mark) {
        if (mark) {
            int tx, ty;
            if (!bTrails) return;
           System.out.println("mark at "+x+", "+y+" t "+time);
    		tx = (int) (x*scaleDistance+origin.x);
    		ty = (int) (origin.y-y*scaleDistance);
            ((shotSummary) shotList.lastElement()).addPoint(tx,ty,time);
            return;
        }
		
		Rectangle oldRect = new Rectangle(dartX-3,dartY-3,9,9);
		realX = x; realY = y; 
		                newDartX = (int) (x*scaleDistance+origin.x);
		newDartY = (int) (origin.y-y*scaleDistance);
		
		Rectangle newRect = new Rectangle(newDartX-3,newDartY-3,9,9);
		
		Rectangle theRect = oldRect.union(newRect);
		this.repaint(theRect.x,theRect.y,theRect.width,theRect.height);
	}

	
	public void endFiring(double[] endStats)
	{
		endDistance = endStats[0];
		endVelocity = endStats[1];
		maxHeight = endStats[2];
		endTime = endStats[3];
		bStatistics = true; 
		bPleaseRedraw = true;
		repaint(); 
	}

	private class shotSummary {
	public double[] shotStats;
		public Color currentColor;
		public int[][] shotPoints; 
        public double[] shotTimes;
        public int iPoints; 
        private int maxPoints; 
        
		public shotSummary(double[] sS, Color c) {
			shotStats = sS;
			currentColor = c;
			maxPoints = 10; // why not start with ten?
			shotPoints = new int[maxPoints][2];
            shotTimes = new double[maxPoints];
			iPoints = 0;
		}

      	public void addPoint(int x, int y, double t) {
		    if (iPoints>maxPoints-1) {
                maxPoints += 10;
		        int[][] tp = new int[maxPoints][2];
                double[] tt = new double[maxPoints];
		        for (int i=0; i<iPoints; i++)
		            for (int j=0; j<2; j++) {
		                tp[i][j] = shotPoints[i][j];
		                tt[i] = shotTimes[i];
		            }
		        shotPoints = tp;
		        shotTimes = tt;
		    }
		    shotPoints[iPoints][0] = x;
		    shotPoints[iPoints][1] = y;
		    shotTimes[iPoints] = t;
		    iPoints++;
		}
	}
		

	public void setTrails(boolean on)
	{
		bWantTrails = on;
	}

	public Dimension getPreferredSize() {
		return new Dimension(200,200);
	}
	
	public Dimension getMinimumSize() {
		return new Dimension(200,200);
	}

	
	private void changeSize()
	{
		
		size = this.getSize();
		
		buffer = this.createImage(size.width,size.height);
		
		if (buffer!=null) {
			bufferGraphics = buffer.getGraphics();
		} else return;

		
		origin = new Point((int) (size.width*0.2),(int) (size.height*0.8));
		graphArea = new Rectangle(origin.x+1,0,size.width-origin.x,origin.y-1);
		graphLengthX = (int) (size.width*0.5);
		graphLengthY = (int) (size.height*0.5);
		if (graphLengthX > graphLengthY*2)
			scaleDistance = 1.2*graphLengthY/maxDistance;
		else
			scaleDistance = graphLengthX/maxDistance;

		
		Font f = new Font("SansSerif",Font.PLAIN,14);
		ordinate = new DrawableAxis(graphLengthX,DrawableAxis.HORIZONTAL,
				0,graphLengthX/scaleDistance,f);
	    ordinate.setLabel(sXAxis, new Font("SansSerif",Font.BOLD,14));
		abscissa = new DrawableAxis(graphLengthY,DrawableAxis.VERTICAL,
				0,graphLengthY/scaleDistance,f);
	    makeLabelForAxis(abscissa,sYAxis,new Font("SansSerif",Font.BOLD,14));
		
		if (exists) setPosition(realX,realY,0,0,0,false);
		if (bTrails) calculateTrails();
		bPleaseRedraw = true;
		repaint(); 
	}

   
	private void makeLabelForAxis(DrawableAxis da,String sLabel,Font labelFont) {
        FontMetrics lfm = getToolkit().getFontMetrics(labelFont);
	    int width = lfm.stringWidth(sLabel);
	System.out.println("width "+width+" height "+lfm.getLeading());
      
        Image ti = createImage(width,lfm.getHeight()+5);
        Graphics tb = ti.getGraphics();
        tb.setFont(labelFont);
        tb.drawString(sLabel,0,lfm.getAscent());
       
        ImageProducer ip = ti.getSource();
        RotFilter rf = new RotFilter();
        ip = new FilteredImageSource(ip,rf);
        Image rotatedImage = Toolkit.getDefaultToolkit().createImage(ip);
        da.setLabel(rotatedImage);
	}
    private void calculateTrails()
	{
		double t,ttotal, kap,gam,vx0,vy0,t0;
		double[] retLocs;
		shotSummary tSum;
		
		for (int j=0; j<shotList.size(); j++) {
			// Pull initial value info from shotList.
			tSum = (shotSummary) shotList.elementAt(j);
                    t=0; kap = tSum.shotStats[0]; gam = tSum.shotStats[1];
			t0 = tSum.shotStats[4]; vx0 = tSum.shotStats[2];
			vy0 = tSum.shotStats[3];
           	for (int i=0; i<tSum.iPoints; i++) {
				t = tSum.shotTimes[i];
                            retLocs = dart.calcPosition(kap,gam,t,vx0,vy0,t0);
				tSum.shotPoints[i][0] = (int) (retLocs[0]*scaleDistance+origin.x);
				tSum.shotPoints[i][1] = (int) (origin.y-retLocs[1]*scaleDistance);
            System.out.println("recalc mark at x "+retLocs[0]+" y "+retLocs[1]+" t "+t);
			}
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -