前提・実現したいこと
初歩的な質問で申し訳ありませんが、javaのプログラムについて質問です。
二重円を表示させ、インタフェースを使って、その円がマウスでドラッグできるようにしたいです。
該当のソースコードの欄にあるコードを元にインタフェースを追加したいです。
発生している問題・エラーメッセージ
そもそも書き方がわからないので
該当のソースコード
javaです
import java.awt.;
import java.awt.event.;
import javax.swing.*;
public class R4_2 extends JPanel {
DoubleCircle dc1 = new DoubleCircle(Color.magenta, Color.blue, 75, 30, 100);
public R4_2() { setOpaque(false); addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent evt) { dc1.moveTo(evt.getX(), evt.getY()); repaint(); //System.out.println(evt.getX()); System.out.println(evt.getY()); } }); } public void paintComponent(Graphics g) { dc1.draw(g); } public static void main(String[] args) { JFrame app = new JFrame(); app.add(new R4_2()); app.setSize(300, 200); 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, ypos, rad, rad); } } static class DoubleCircle { Circle c1, c2; Color col1, col2; int xpos, ypos, rad; public DoubleCircle(Color cl1, Color cl2, int x, int y, int r) { col1 = cl1; col2 = cl2; rad = r; c1 = new Circle(cl1, x, y, r); c2 = new Circle(cl2, x+r/4, y+r/4, r/2); } public void moveTo(int x, int y) { //xpos = x; ypos =y; c1 = new Circle(col1, x, y, rad); c2 = new Circle(col2, x+rad/4, y+rad/4, rad/2); } public void draw(Graphics g) { c1.draw(g); c2.draw(g); //System.out.println(c1.xpos); System.out.println(c1.ypos); } }
}
試したこと
補足情報
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。