……首先,這個明顯不是拋物線,而是sin/cos曲線

10年的海倫網站建設經驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。成都全網營銷推廣的優(yōu)勢是能夠根據用戶設備顯示端的尺寸不同,自動調整海倫建站的顯示方式,使網站能夠適用不同顯示終端,在瀏覽器中調整網站的寬度,無論在任何一種瀏覽器上瀏覽網站,都能展現優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯建站從事“海倫網站設計”,“海倫網站推廣”以來,每個客戶項目都認真落實執(zhí)行。
我習慣用輕量組件
取兩個數,x1和y1,x從最左邊到最右邊循環(huán)賦值,y1=f(x1)
再取兩個數,x2和y2,x2就是下一個x1的值,y2=f(x2)
其中f(x)是一個函數,可以是sin(x),也可以是x的平方。
創(chuàng)建一個JPanel,但是別直接定義JPanel類,你需要這樣創(chuàng)建:
ClassName variable = new ClassName(parameters);
其中這個ClassName,需要你繼承JPanel,并覆蓋里邊的paintComponent(Graphics g)方法,不這樣創(chuàng)建是畫不出來的。
接下來就開始畫,g.drawLine(x1, y1, x2, y2),精度可能不高,但是效果是如圖的。
哎呀我靠逗比了,答完了才看見是在控制臺輸出的
前面也不用刪,但是把
g.drawLine(x1, y1, x2, y2)
換成
g.drawRect(x1, y1, 1, 1)
比較好,x2和y2就扔了吧。
要在控制臺輸出,先定義一下每行長度和寬度,也就是橫坐標和縱坐標。
越多越精細,但是太多了也不行,一行打不出來,會很……
然后用下面的兩個句子:
BufferedImage b=new BufferedImage(剛才那個面板的長度、寬度、1是三個需要傳遞的參數);
某個面板.paint(b.createGraphics());
這樣把面板上顯示的內容輸入在一個名字叫b的圖像里
這時候就可以用兩個循環(huán)嵌套,來挨個檢查b上的每一個點的顏色,用這個句子:
int color=b.getRGB(x, y);其中x和y分別是橫縱坐標。color就是一個16進制的數字
轉成紅綠藍三色,就用下面這個:
int r=(color0xff0000)16, g=(color0xff00)8, b=color0xff;
(重名什么的去死吧!)
然后我們一般都是用黑筆來畫函數圖像對吧,就用if語句判斷紅綠藍是否都為0,如果是則系統(tǒng)打印一個*號,如果不是則系統(tǒng)打印一個空格。
最后再加一行,當橫坐標超出時,系統(tǒng)打印一個轉行符。
1、首先描一個坐標軸
2、確定方程式
3、打點
4、連線
5、取出打點的坐標,按照順序依次變更顏色(做出運動效果)
6、簡單的一元二次方程舉例【步驟5留給題主思考】
public class View extends JFrame {
public View() {
JFrame frame = new JFrame("Equation");
frame.getContentPane().setLayout(new BorderLayout());
JPanel panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(50, 50));
JLabel labelA = new JLabel();
labelA.setText("a");
JTextField textA = new JTextField("0",3);
JLabel labelB = new JLabel();
labelB.setText("b");
JTextField textB = new JTextField("0",3);
JLabel labelC = new JLabel();
labelC.setText("c");
JTextField textC = new JTextField("0",3);
JButton draw = new JButton();
draw.setText("Draw");
draw.addActionListener( new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
Controller.a = Double.parseDouble(textA.getText());
Controller.b = Double.parseDouble(textB.getText());
Controller.c = Double.parseDouble(textC.getText());
repaint();
frame.pack();
frame.setSize(420,490);
}
});
panel1.add(labelA);
panel1.add(textA);
panel1.add(labelB);
panel1.add(textB);
panel1.add(labelC);
panel1.add(textC);
panel1.add(draw);
JPanel panel2 = new JPanel(){
public void paint(Graphics g){
super.paint(g);
Controller.grid(g);
Controller.Graphic1(g);
}
};
panel2.setBackground(Color.WHITE);
frame.getContentPane().add(panel1, BorderLayout.PAGE_START);
frame.getContentPane().add(panel2, BorderLayout.CENTER);
frame.setVisible(true);
frame.setSize(420,490);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
View frame = new View();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
public class Controller {
static double a=2, b=1, c=0;
public static void grid (Graphics g){
g.setColor(Color.blue);
g.drawLine(200,0,200,400);
g.drawLine(0,200,400,200);
for (int x=0; x=400; x= x +40){
g.drawLine(x,195,x,205);
}
for (int y=0; y=400; y=y+40){
g.drawLine(195,y,205,y);
}
}
public static void Graphic1(Graphics g) {
g.setColor(Color.red);
for (double x=-100;x=100;x = x+0.1){
double y = a * x * x + b * x + c;
int X = (int)Math.round(200 + x*20);
int Y = (int)Math.round(200 - y*20);
g.fillOval(X-2,Y-2,4,4);
}
}
}
看了這套題目感覺很有興趣,就花了一個中午親手給你寫了一個類似的例子,相信可以幫助你對這個游戲有很好的理解,從右向左那個是僵尸,點一下鼠標就出現植物,我只是起到一個拋磚引玉的作用。代碼如下(絕對可以用的代碼):
import?java.awt.Dimension;
import?java.awt.Graphics;
import?java.awt.event.MouseEvent;
import?java.util.Vector;
import?javax.swing.JFrame;
import?javax.swing.event.MouseInputAdapter;
public?class?PlantsAndZombies?extends?JFrame?{
private?static?final?long?serialVersionUID?=?1L;
public?static?final?int?screenWidth=800;
public?static?final?int?screenHeight=600;
Printer?printer;
Zombies?zombies=new?Zombies();
Thread?T_Zombies;
Bullet?bullet=new?Bullet();
Thread?T_Bullet;
public?PlantsAndZombies(){
this.setSize(new?Dimension(screenWidth,screenHeight));
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.addMouseListener(new?Shoot(this));
this.setVisible(true);
printer=new?Printer(?this.getGraphics());
printer.Add(zombies);
printer.Add(bullet);
T_Zombies=new?Thread(zombies);
T_Zombies.start();
T_Bullet=new?Thread(bullet);
T_Bullet.start();
}
public?void?Shoot(){
bullet.getTarget(zombies);
}
public?static?void?main(String[]?args){
PlantsAndZombies?game=new?PlantsAndZombies();
}
public?void?run()?{
while(true){
}
}
}
interface?Drawable{
void?drawMe(Graphics?g);
}
class?Zombies?implements?Drawable,Runnable{
public?boolean?isLive=true;
public?int?x=PlantsAndZombies.screenWidth;
public?int?y=500;
public?void?run()?{
while(true){
if(x10){
x-=20;
}else?x=PlantsAndZombies.screenWidth;
try?{
Thread.sleep(500);
}?catch?(InterruptedException?e)?{
e.printStackTrace();
}
}
}
public?void?drawMe(Graphics?g){
g.drawRect(x,y,20,50);
}
}
class?Bullet?implements?Drawable,Runnable{
private?int?x=0;
private?int?y=500;
private?Zombies?_z;
private?float?a,b,c;
private?float?step;
public?void?getTarget(Zombies?z){
_z=z;
//?用三點確定一個拋物線的方法,計算彈道
int?x1=0;
int?y1=500;
int?x2=(z.x-6*20)/2;
int?y2=300;??//?拋物線高度200個像素
int?x3=z.x-6*20;?//?假設擊中僵尸用3秒鐘,在這3秒鐘內僵尸向前移動了6*20個像素
int?y3=500;
a=(float)((y2-y1)*(x3-x2)-(y3-y2)*(x2-x1))/(float)((x2*x2-x1*x1)*(x3-x2)-(x3*x3-x2*x2)*(x2-x1));
b=(float)((y2-y1)-a*(x2*x2-x1*x1))/(float)(x2-x1);
c=y1-a*x1*x1-b*x1;
step=(float)(x3-x1)/(float)(3*20);
}
public?void?run()?{
while(true){
try?{
x+=step;
y=(int)(a*x*x+b*x+c);
if(y500){
_z.isLive=false;
}
Thread.sleep(50);
}?catch?(InterruptedException?e)?{
e.printStackTrace();
}
}
}
public?void?drawMe(Graphics?g)?{
g.drawRect(x,y,20,20);
}
}
class?Printer?extends?Thread{
private?VectorDrawable?v=new?VectorDrawable();
private?Graphics?_g;
public?Printer(Graphics?g){
_g=g;
this.start();
}
public?void?Add(Drawable?o){
v.add(o);
}
public?void?run(){
while(true){
_g.clearRect(0,0,PlantsAndZombies.screenWidth,PlantsAndZombies.screenHeight);
for(Drawable?o:v){
o.drawMe(_g);
}
try?{
Thread.sleep(500);
}?catch?(InterruptedException?e)?{
e.printStackTrace();
}
}
}
}
class?Shoot?extends?MouseInputAdapter{
private?PlantsAndZombies?_adaptee;
public?Shoot(PlantsAndZombies?adaptee){
_adaptee=adaptee;
}
public?void?mouseClicked(MouseEvent?e)?{
_adaptee.Shoot();
}
}
拋物線是怎么樣的? y= a * x *x +b? 這個你比我懂。。
知道方程了,畫線就是若干點之間的連線,也就是畫 poly
至于用java 畫還是c來畫 或者是 C#來畫,完全就是各種API的調用問題。
我估計你之所以要問,主要是不知道java 圖形界面(窗體)如何生成? 那需要用 swing
角度問題 可能是最麻煩的。。。如果我來想,我會: 找到拋物線中最頂的點 P1和兩邊的等高點 P2,P3 用 p2-p1 得到向量 V1 用p3-p1得到向量 V2 用向量的點積來計算出角度。 當然思考是這樣,畫的時候就得跟據這種考慮來定點的位置了。。。 僅供參考。
標題名稱:java寫出拋物線代碼,編程拋物線
轉載來于:http://chinadenli.net/article32/dsgghsc.html
成都網站建設公司_創(chuàng)新互聯,為您提供品牌網站建設、微信公眾號、外貿建站、企業(yè)建站、建站公司、搜索引擎優(yōu)化
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯