2016-05-22 7 views
-2

私は、ボールをターゲットにバウンスさせるために、ユーザーが線を引く必要があるゲームを作っています。ボールとラインの両方を同時に表示するのに問題があり、どちらか一方しか表示されない場合があります。私はそれらを透明にしたとしても、パネルは互いにブロックしているように見えます。私はそれらの両方が同じフレームに現れるようにしたいと思います。このポストの時点で、ラインパネルはボールパネルを覆っています。透明パネル

import javax.swing.Timer; 
import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.awt.Graphics; 
import java.awt.Color; 
import javax.swing.JPanel; 
import javax.swing.JFrame; 

public class Game 
{ 
    public static void main(String args[]) throws Exception 
    { 
     JFrame f = new JFrame("Let's Play"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setSize(1280, 720); 
     f.setLocation(300, 300); 
     f.setResizable(false); 

     //this part draws a ball that bounces around the screen 
     BallPanel ballPanel = new BallPanel() 
     { 
      // draw rectangles and arcs 

      public void paintComponent(Graphics g) 
      { 
      super.paintComponent(g); // call superclass's paintComponent 
      g.setColor(Color.red); 

      // check for boundaries 
      if (x < radius)   dx = Math.abs(dx); 
      if (x > getWidth() - radius) dx = -Math.abs(dx); 
      if (y < radius)   dy = Math.abs(dy); 
      if (y > getHeight() - radius) dy = -Math.abs(dy); 

      // adjust ball position 
      x += dx; 
      y += dy; 
      g.fillOval(x - radius, y - radius, radius*2, radius*2); 
      } 

     }; 
     ballPanel.setOpaque(false); 
     f.add(ballPanel); 

     //this part allows you to draw lines on the frame with your mouse 
     JPanel lineP = new JPanel() 
     { 
      Point pointStart = null; 
      Point pointEnd = null; 

      { 
       addMouseListener(new MouseAdapter() 
       { 
        public void mousePressed(MouseEvent me) 
        { 
         pointStart = me.getPoint(); 
        } 

        public void mouseReleased(MouseEvent me) 
        { 
         pointStart = null; 
        } 
       }); 
       addMouseMotionListener(new MouseMotionAdapter() 
       { 
        public void mouseMoved(MouseEvent me) 
        { 
         pointEnd = me.getPoint(); 
        } 

        public void mouseDragged(MouseEvent me) 
        { 
         pointEnd = me.getPoint(); 
         repaint(); 
        } 
       }); 
      } 
      public void paint(Graphics dline) 
      { 
       super.paint(dline); 
       if (pointStart != null) 
       { 
        dline.setColor(Color.RED); 
        dline.drawLine(pointStart.x, pointStart.y, pointEnd.x, pointEnd.y); 

       } 
      } 
     }; 
     lineP.setOpaque(false); //attempted to enable to see ball panel here 
     f.add(lineP); 
     f.setVisible(true); 
    } 
} 

class BallPanel extends JPanel implements ActionListener 
{ 
private int delay = 10; 
protected Timer timer; 

public int x = 30;  // x position 
public int y = 30;  // y position 
public int radius = 15; // ball radius 

public int dx = 10;  // increment amount (x coord) 
public int dy = 10;  // increment amount (y coord) 

public BallPanel() 
{ 
    timer = new Timer(delay, this); 
    timer.start();  // start the timer 
} 

public void actionPerformed(ActionEvent e) 
// will run when the timer fires 
{ 
    repaint(); 
    } 


} 
+0

この[URL](http://stackoverflow.com/help)をチェックすると、コンテンツの品質を上げることができます –

答えて

2

あなたはいくつかの問題を持っているが、主なものは、あなたが過剰に使用してGUIコンポーネントをしているということです。描画を行うコンポーネントJPanel、ボールパネルとラインパネルではなく、DrawingPanelを1つだけ用意する必要があります。むしろBallとLineはGUIクラスではなく論理クラスでなければならず、その表示は同じ単一のDrawingPanelになければなりません。

その他の問題が含まれます:

  • あまりにも多くのコードを持っている主な方法。そのコードの大半は、それが属するOOP世界にオフロードされるべきです。
  • リスナインタフェースも実装するGUIコンポーネントクラス。これは、クラスにあまりにも多くの責任を与えて、デバッグやアップグレードを困難にしています。これらの懸念を分けてください。
  • クラスの1つがpaintメソッドをオーバーライドします。これは避けるべきです。 paintComponentをオーバーライドします。
  • paintComponentをオーバーライドする他のクラスは、paintComponent内にプログラムロジックを持っています。このメソッドが呼び出されるタイミングを制限しているので、これは避けてください。そのクラスからロジックを取り出し、マウスリスナーコードまたはゲームループコード(Swing Timer)に入れます。