import java.awt.Color; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import javax.swing.JFrame; import javax.swing.JPanel; public class R4_4 extends JPanel { DoubleCircle dc2 = new DoubleCircle(Color.BLUE, Color.CYAN, 100, 100, 100); Crrt lu = new Crrt(Color.MAGENTA, Color.BLACK, Color.DARK_GRAY, Color.ORANGE, 10, 10, 10); public R4_4() { setOpaque(false); addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent evt) { dc2.moveTo(evt.getX(), evt.getY()); repaint(); } }); } public void paintComponent(Graphics g) { dc2.draw(g); lu.draw(g); } public static void main(String[] args) { JFrame app = new JFrame(); app.add(new R4_2()); app.setSize(1000, 1000); 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 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); } } static class DoubleCircle { Circle c1; Circle c2; Color col1, col2; int unit, xpos, ypos; public DoubleCircle(Color cl1, Color cl2, int x, int y, int r) { col1 = cl1; col2 = cl2; unit = r; xpos = x; ypos = y; init(col1, col2, unit, xpos, ypos); } private void init(Color cl1, Color cl2, int r, int x, int y) { c1 = new Circle(cl1, x, y, r); c2 = new Circle(cl2, x, y, r / 2); } public void moveTo(int x, int y) { xpos = x; ypos = y; init(col1, col2, unit, xpos, ypos); } public void draw(Graphics g) { c1.draw(g); c2.draw(g); } } static class Crrt { Circle c; Rect r1; Rect r2; Triangle t1; Color col1, col2, col3, col4; int xpos, ypos, unit; public Crrt(Color cl1, Color cl2, Color cl3, Color cl4, int x, int y, int r) { col1 = cl1; col2 = cl2; col3 = cl3; col4 = cl4; unit = r; xpos = x; ypos = y; init2(col1, col2, col3, col4, unit, xpos, ypos); } private void init2(Color cl1, Color cl2, Color cl3, Color cl4, int r, int x, int y) { c = new Circle(cl1, x * 80, y * 80, r * 4); r1 = new Rect(cl2, x * 30, y * 30, r * 5, r * 10); r2 = new Rect(cl3, x * 60, y * 60, r * 10, r * 5); t1 = new Triangle(cl4, x * 45, y * 43, x * 40, y * 50, x * 50, y * 50); } public void draw(Graphics g) { c.draw(g); r1.draw(g); r2.draw(g); t1.draw(g); } } }
前提・実現したいこと
JAVAで複数の図形が特定された傾きのある一次線形直線の上でそれらの図形の重心がその線上に来るようなプログラムを作っています。
発生している問題:一番上の円のみしか描画されないです。前回同様のドラッグできるプログラムを作ったのですが、そのプログラムが今回のプログラムに反映されている感じのエラーが起きました。実現したいこととして、今回作ったプログラムが正常に実行されるようにしたいです。
完成形は二重丸、丸、四角、三角、四角、丸が傾き-1の直線状に順に並ぶようにして、二重円だけがマウスでドラッグできるような形になります。
また二重円の部分がドラッグできるようになっています。
現状はドラッグできるが、そのほかの図形が描画されない状態です。
試したこと:ソースコードを見直して
DoubleCircle dc2 = new DoubleCircle(Color.BLUE, Color.CYAN, 100, 100, 100); Crrt lu = new Crrt(Color.MAGENTA, Color.BLACK, Color.DARK_GRAY, Color.ORANGE, 10, 10, 10);
の順番を変えて実行してみたり、
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
これが前回作ったプログラムです
import
1import java.awt.Graphics; 2import java.awt.event.MouseEvent; 3import java.awt.event.MouseMotionAdapter; 4 5import javax.swing.JFrame; 6import javax.swing.JPanel; 7 8public class R4_2 extends JPanel{ 9 DoubleCircle dc = new DoubleCircle(Color.BLUE,Color.BLACK,100,100,100); 10 11 public R4_2() { 12 setOpaque(false); 13 addMouseMotionListener(new MouseMotionAdapter(){ 14 public void mouseDragged(MouseEvent evt) { 15 dc.moveTo(evt.getX(),evt.getY()); 16 repaint(); 17 } 18 }); 19 } 20 public void paintComponent(Graphics g) { 21 dc.draw(g); 22 } 23 public static void main(String[] args) { 24 JFrame app = new JFrame(); 25 app.add(new R4_2()); 26 app.setSize(600, 600); 27 app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 28 app.setVisible(true); 29 } 30 static class Circle{ 31 Color col; 32 int xpos,ypos,rad; 33 34 public Circle(Color c,int x,int y,int r) { 35 col=c; 36 xpos=x; 37 ypos=y; 38 rad=r; 39 } 40 public void draw(Graphics g) { 41 g.setColor(col); 42 g.fillOval(xpos-rad, ypos-rad, rad*2, rad*2); 43 } 44 } 45 static class DoubleCircle{ 46 Circle c1; 47 Circle c2; 48 Color col1,col2; 49 int unit,xpos,ypos; 50 51 public DoubleCircle(Color cl1,Color cl2,int x, int y,int r) { 52 col1=cl1; 53 col2=cl2; 54 unit=r; 55 xpos=x; 56 ypos=y; 57 init(col1,col2,unit,xpos,ypos); 58 } 59 private void init(Color cl1,Color cl2,int r,int x,int y) { 60 c1=new Circle(cl1,x,y,r); 61 c2=new Circle(cl2,x,y,r/2); 62 } 63 public void moveTo(int x,int y) { 64 xpos=x;ypos=y;init(col1,col2,unit,xpos,ypos); 65 } 66 67 public void draw(Graphics g) { 68 c1.draw(g); 69 c2.draw(g); 70 } 71 } 72} 73 74コード
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/08 12:57