質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

1回答

1801閲覧

javaの関数が反応しない

someone190190

総合スコア3

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2020/07/20 04:39

ボタンを押すと決まった関数が呼び出されるようなプログラムを作っています。ですがSample.javaのvxup()やvxdown()など4つの間巣が反応しません。なぜなのかわからず教えていただきたいです。

//Sample.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Sample extends JPanel implements ActionListener{ static int xPanelSize; static int yPanelSize; double x,y,r,vx,vy,ax,ay; BallPropagator b1; Color c1; static JButton btn1 = new JButton("↑"); static JButton btn2 = new JButton("↓"); static JButton btn3 = new JButton("→"); static JButton btn4 = new JButton("←"); Sample(int xPanelSize, int yPanelSize){ this.xPanelSize = xPanelSize; this.yPanelSize = yPanelSize; b1 = new BallPropagator(10, 100, 100, 30, 40, 0, 9.8); btn1.addActionListener(this); btn1.setBounds(100,100,50,100); btn2.addActionListener(this); btn2.setBounds(100,50,50,100); btn3.addActionListener(this); btn3.setBounds(0,100,100,100); btn4.addActionListener(this); btn4.setBounds(200,100,100,100); } //イベント(タイマーによる呼び出し)が発生したときの処理 public void actionPerformed(ActionEvent e){ JButton selectedButton = (JButton)e.getSource(); if(selectedButton==btn1){ System.out.println("up"); b1.upvy(); }else if(selectedButton==btn2){ System.out.println("down"); b1.downvy(); }else if(selectedButton==btn3){ System.out.println("right"); b1.leftvx(); }else{ System.out.println("left"); b1.rightvx(); } } public void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(c1); g.fillOval((int)(b1.a(x) - b1.c(r)), (int)(b1.b(y) - b1.c(r)), (int)(2*b1.c(r)), (int)(2*b1.c(r))); } public static void main(String[] args) { BallMain panel = new BallMain(640,480); Sample sample=new Sample(640,480); BallPropagator.dt=0.1; BallPropagator.o=5.0; JFrame frame = new JFrame(); panel.setBackground(Color.white); panel.setPreferredSize(new Dimension(xPanelSize,yPanelSize)); panel.add(btn1); panel.add(btn2); panel.add(btn3); panel.add(btn4); BallPropagator.xmin = 0; BallPropagator.xmax = panel.xPanelSize; BallPropagator.ymin = 0; BallPropagator.ymax = panel.yPanelSize; frame.add(panel); frame.pack(); // frame.setTitle("反射するボール"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //frame.setResizable(true); frame.setVisible(true); Timer timer = new Timer(10, panel); timer.start(); } }
//BallMain.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class BallMain extends JPanel implements ActionListener{ static int xPanelSize; static int yPanelSize; double x,y,r,vx,vy,ax,ay; BallPropagator b1; Color c1; BallMain(int xPanelSize, int yPanelSize){ this.xPanelSize = xPanelSize; this.yPanelSize = yPanelSize; b1 = new BallPropagator(10, 100, 100, 30, 40, 0, 9.8); ///////////////////////////////////////////// c1=Color.black; ///////////////////////////////////////////// } public void actionPerformed(ActionEvent e){ b1.xUpdate(); b1.yUpdate(); b1.vxUpdate(); b1.vyUpdate(); repaint(); } public void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(c1); g.fillOval((int)(b1.a(x) - b1.c(r)), (int)(b1.b(y) - b1.c(r)), (int)(2*b1.c(r)), (int)(2*b1.c(r))); } }
//BallPropagator.java public class BallPropagator{ // クラスフィールド public static double dt;//プロパゲータの微小時間間隔 public static int xmin; //境界座標 public static int xmax; public static int ymin; public static int ymax; // インスタンスフィールド private double r; //ボールの半径 private double x, y; //ボールの位置(中心座標) private double vx, vy;//ボールの速度 private double ax, ay;//ボールの加速度 public static double o=5.0; double p=0.8;// 反発係数 // ???????? /////////////////////////////////////////////////////////// BallPropagator(double r,double x,double y,double vx,double vy,double ax,double ay){ this.r=r; this.x=x; this.y=y; this.vx=vx; this.vy=vy; this.ax=ax; this.ay=ay; } /////////////////////////////////////////////////////////// //x 軸方向の位置の更新 public void xUpdate() { this.x = this.x + this.vx * dt; //x 方向の反射(cf. 反発係数) if(this.x < (double)xmin+this.r ){ // this.vx = ?????????? this.vx *= -p; this.x = (double)xmin+this.r; }else if((double)xmax-this.r < x ){ // this.vx = ?????????? this.vx *= -p; this.x = (double)xmax-this.r; } } //y 軸方向の位置の更新 public void yUpdate() { this.y = this.y + this.vy * dt; //y 方向の反射(cf. 反発係数) if(this.y < (double)ymin + this.r ){ // this.vy = ?????????? this.vy *= -p; this.y = (double)ymin+this.r; }else if((double)ymax-this.r < this.y ){ // this.vy = ?????????? this.vy *= -p; this.y = (double)ymax-this.r; } } //x 軸方向の速度の更新 public void vxUpdate(){ this.vx=this.ax*dt+this.vx; } //y 軸方向の速度の更新 public void vyUpdate(){ this.vy=this.ay*dt+this.vy; // ???????? // ???????? } public void upvy(){ this.vy=this.vy+this.o; } public void downvy(){ this.vy=this.vy-this.o;} public void leftvx(){ this.vx=this.vx-this.o;} public void rightvx(){ this.vx=this.vx+this.o;} public double a(double x){ return x=this.x; } public double b(double x){ return x=this.y; } public double c(double x){ return x=this.r; } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

TN8001

2020/07/20 08:44

[Java - ボタンを押しても反応しないです|teratail](https://teratail.com/questions/278736 [Java - ボタンを押すと速度が加算されるものを作りたい|teratail](https://teratail.com/questions/278819 [Java - javaの関数が反応しない|teratail](https://teratail.com/questions/279029 3つとも同じコードに見えます。 これは3人とも同じ課題に取り組んでいるということでしょうか?? (申し上げにくいのですがあまりにもひどいコードなので)とても課題とは思えず、同一人物の投稿かと思ったのですがそうではないのですね?
guest

回答1

0

ベストアンサー

ボタンのイベント自体は動作してますね。
x,y,vx,vyUpdateメソッド系と、up,down,left,rightvx,yメソッド系を
デバッグで動作させてvx,vyの値の変化を見ればわかると思いますが、
SampleクラスとBallMainクラスのコンストラクタで
それぞれ別にBallPropagatorクラスを初期化しているため、
参照しているオブジェクトが別な物となっています。

実際にボールが描画されているのはBallMainクラスで生成されているオブジェクトなので、
Sampleクラス側で生成したBallMainクラスのb1オブジェクトからup,down,left,rightvx,yメソッドを呼び出せば、
ボタンの動作をボールに反映させられます。

一応修正したSampleクラスのソースを貼っておきますが、
とりあえず修正しただけで綺麗な構成ができてるとは言い難いので参考程度でお願いします。

java

1//Sample.java 2import java.awt.Color; 3import java.awt.Dimension; 4import java.awt.Graphics; 5import java.awt.event.ActionEvent; 6import java.awt.event.ActionListener; 7 8import javax.swing.JButton; 9import javax.swing.JFrame; 10import javax.swing.JPanel; 11import javax.swing.Timer; 12 13public class Sample extends JPanel implements ActionListener{ 14 15 static int xPanelSize; 16 static int yPanelSize; 17 18 double x,y,r,vx,vy,ax,ay; 19 static BallMain panel; 20 Color c1; 21 22 static JButton btn1 = new JButton("↑"); 23 static JButton btn2 = new JButton("↓"); 24 static JButton btn3 = new JButton("→"); 25 static JButton btn4 = new JButton("←"); 26 27 Sample(int xPanelSize, int yPanelSize){ 28 this.xPanelSize = xPanelSize; 29 this.yPanelSize = yPanelSize; 30 31 btn1.addActionListener(this); 32 btn1.setBounds(100,100,50,100); 33 34 btn2.addActionListener(this); 35 btn2.setBounds(100,50,50,100); 36 37 btn3.addActionListener(this); 38 btn3.setBounds(0,100,100,100); 39 40 btn4.addActionListener(this); 41 btn4.setBounds(200,100,100,100); 42 } 43 //イベント(タイマーによる呼び出し)が発生したときの処理 44 45 public void actionPerformed(ActionEvent e){ 46 JButton selectedButton = (JButton)e.getSource(); 47 if(selectedButton==btn1) { 48 System.out.println("up"); 49 panel.b1.upvy(); 50 } else if(selectedButton==btn2) { 51 System.out.println("down"); 52 panel.b1.downvy(); 53 }else if(selectedButton==btn3) { 54 System.out.println("right"); 55 panel.b1.leftvx(); 56 }else{ 57 System.out.println("left"); 58 panel.b1.rightvx(); 59 } 60 } 61 62 public void paintComponent(Graphics g){ 63 super.paintComponent(g); 64 65 g.setColor(c1); 66 67 g.fillOval((int)(panel.b1.a(x) - panel.b1.c(r)), (int)(panel.b1.b(y) - panel.b1.c(r)), 68 (int)(2*panel.b1.c(r)), (int)(2*panel.b1.c(r))); 69 } 70 71 public static void main(String[] args) { 72 panel = new BallMain(640,480); 73 Sample sample=new Sample(640,480); 74 75 BallPropagator.dt=0.1; 76 BallPropagator.o=5.0; 77 78 JFrame frame = new JFrame(); 79 80 panel.setBackground(Color.white); 81 panel.setPreferredSize(new Dimension(xPanelSize,yPanelSize)); 82 panel.add(btn1); 83 panel.add(btn2); 84 panel.add(btn3); 85 panel.add(btn4); 86 87 BallPropagator.xmin = 0; 88 BallPropagator.xmax = panel.xPanelSize; 89 BallPropagator.ymin = 0; 90 BallPropagator.ymax = panel.yPanelSize; 91 92 frame.add(panel); 93 frame.pack(); 94// 95 frame.setTitle("反射するボール"); 96 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 97 //frame.setResizable(true); 98 frame.setVisible(true); 99 100 Timer timer = new Timer(10, panel); 101 timer.start(); 102 } 103}

投稿2020/07/20 06:19

yureighost

総合スコア2183

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

someone190190

2020/07/20 08:10

回答、誠にありがとうございます。SampleとBallMainという違うクラスで生成されていたから反応しなかったんですね。困っていたので本当にありがとうございます。これからも頑張っていきます!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問