eclips(java)でプログラムを作っているのですが赤丸(c3)をマウスで移動できるようにした上で、タップする度に色をランダムで変えられるようにしたいのですが、移動までは出来ましたが色を変えることが出来ません。どう書き直せばいいか教えてください。
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.*;
public class s_1 extends JPanel {
private static final boolean fales = false; double a = 100*Math.cos((Math.PI)*2*1/3)+400; //変数の更新 double b = 100*Math.sin((Math.PI)*2*1/3)+200; //変数の更新 double c = 100*Math.cos((Math.PI)*2*2/3)+400; //変数の更新 double d = 100*Math.sin((Math.PI)*2*2/3)+200; //変数の更新 double e = 100*Math.cos((Math.PI)*2*3/3)+400; //変数の更新 double f = 100*Math.sin((Math.PI)*2*3/3)+200; //変数の更新 Circle c1 = new Circle(Color.MAGENTA, 100, 50, 40); Circle c2 = new Circle(Color.BLUE, 100, 50, 30); Rect r1 = new Rect(Color.YELLOW, 200, 100, 50, 50); // 正方形の生成 Triangle t1 = new Triangle(Color.BLACK, (int)a, (int)b, (int)c, (int)d, (int)e, (int)f); // 正三角形の生成 Rect r2 = new Rect(Color.GREEN, 600,300, 60, 60); // 正方形の生成 Circle c3 = new Circle(Color.RED, 800, 400, 50); public s_1() { setOpaque(fales); addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent evt) { c3.moveTo(evt.getX(), evt.getY()); repaint(); } }); } public void paintComponent(Graphics g) { c1.draw(g); c2.draw(g); r1.draw(g); // 正方形の描画 t1.draw(g); // 正三角形の描画 r2.draw(g); // 正方形の描画 c3.draw(g); } public static void main(String[] args) { JFrame app = new JFrame(); app.add(new s_1()); app.setSize(1000, 700); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.setVisible(true); } static class Circle { Color col; int xpos, ypos, rad; public Circle(Color c, int x, int y, int r) { col = c; xpos = x; ypos = y; rad = r; } public void moveTo(int x, int y) { xpos = x; ypos = y; } public void draw(Graphics g) { g.setColor(col); g.fillOval(xpos - rad, ypos - rad, rad * 2, rad * 2); } } /* 正方形クラスの定義 */ static class Rect { Color col; int xpos, ypos, width, height; public Rect(Color c, int x, int y, int w, int h) { col = c; xpos = x; ypos = y; width = w; height = h; } public void draw(Graphics g) { g.setColor(col); g.fillRect(xpos - width / 2, ypos - height / 2, width, height); } } /* 正三角形クラスの定義 */ static class Triangle { Color col; int[] xs, ys; public Triangle(Color c, int x0, int y0, int x1, int y1, int x2, int y2) { col = c; xs = new int[] { x0, x1, x2 }; ys = new int[] { y0, y1, y2 }; } public void draw(Graphics g) { g.setColor(col); g.fillPolygon(xs, ys, 3); } }
}
質問タグにある「JavaScript」は無関係な言語なので外してください。
Javaであることはタグで分かるのでタイトルに書く必要はないです。要件(起きている問題・実現したいこと)を記載してください。
コードはマークダウンの機能を利用してご提示ください。
https://teratail.com/help#about-markdown