2016-07-10 3 views
2

私はJavaのSwingを使って簡単なobject drawingプログラムを開発しています。 私のプログラムは、クリックするとボタンに応じて図形を描き、マウスで図形を動かすだけです。私は四角形、円、四角形を画面上に描く4つのボタンを持っています。これまでは、ボタンをクリックすると形を描くことができました。しかし、私はそれがうまくいかなかった画面上の形を移動したい。Javaスイングマウスで形を動かす

問題は次のとおりです。円でクリックしてマウスでドラッグすると、すべての画面が消去され、画面に表示されます。

そして、クリアボタンをクリックすると、すべての画面を消去する方法はありますか?

ありがとうございますか?

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 


public class PaintProject extends JComponent implements ActionListener, 
    MouseMotionListener { 

    private int CircleX=0; 
    private int CircleY=0; 
    private int RectX=100; 
    private int RectY=100; 
    private int SquareX=300; 
    private int SquareY=200; 


    public static void main(String[] args) { 
     JFrame frame = new JFrame("NEW PAINT PROGRAME!"); 

     JButton CircleButton = new JButton("Circle"); 
     CircleButton.setActionCommand("Circle"); 

     JButton RectangleButton = new JButton("Rectangle"); 
     RectangleButton.setActionCommand("Rectangle"); 

     JButton SquareButton = new JButton("Square"); 
     SquareButton.setActionCommand("Square"); 

     PaintProject paint = new PaintProject(); 

     CircleButton.addActionListener(paint); 
     RectangleButton.addActionListener(paint); 
     SquareButton.addActionListener(paint); 


     frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     frame.setLayout(new FlowLayout()); 
     frame.add(paint); 
     frame.add(CircleButton); 
     frame.add(RectangleButton); 
     frame.add(SquareButton); 


     frame.addMouseMotionListener(paint); 

     frame.pack(); 
     frame.setVisible(true); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(500, 500); 

    } 

    private void drawCircle() { 
     Graphics g = this.getGraphics(); 
     g.setColor(Color.red); 
     g.fillOval(CircleX, CircleY, 100, 100); 
    } 

    private void drawRectangle() { 
     Graphics g = this.getGraphics(); 
     g.setColor(Color.green); 
     g.fillRect(RectX, RectY, 100, 300); 
    } 
    private void drawSquare() { 
     Graphics g = this.getGraphics(); 
     g.setColor(Color.blue); 
     g.fillRect(SquareX, SquareY, 100, 100); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     String command = e.getActionCommand(); 
     if (command.equals("Circle")) { 
     drawCircle(); 
    } 
    else if (command.equals("Rectangle")) { 
     drawRectangle(); 
    } 
    else if (command.equals("Square")) { 
     drawSquare(); 
    } 

    } 

    @Override 
    public void mouseDragged(MouseEvent e) { 
     CircleX=e.getX(); 
     CircleY=e.getY(); 
     repaint(); 
    } 

} 
+2

'グラフィックG = this.getGraphics();正しいため'参照[実行カスタム絵画(https://docs.oracle.com/javase/tutorial/uiswing/painting/)レッスンそれを行う方法。 –

+1

これに対する一般的なアプローチとして、私は 'ArrayList 'をユーザが(ボタンをクリックすることによって)要求したすべての形状を保存したままにします。ユーザーがボタンをクリックすると、['Shape'](http://docs.oracle.com/javase/8/docs/api/java/awt/Shape.html)を配列リストに追加し、' repaint( ) '。これは、 'paintComponent(Graphics)'を有効な 'Graphics'インスタンスと共に呼び出すことに終わります。ペイントコンポーネントメソッドでは、リストを反復してすべてを描画します。 –

+0

私はあなたのアイデアを得ていませんでした!私はこのコードで前進したい、私は間違いをどこにしているのかわからない。 –

答えて

3

としてはgetGraphics()はSwingでcustom paintingを実行する方法はありませんが、herehereを指摘しました。代わりに、paintComponent()をオーバーライドして、目的のコンテンツをレンダリングします。マウスを使用して図形をドラッグしたいように見えます。選択したオブジェクトを移動する基本的な方法は、hereです。そこに表示されているdrawString()の代わりにfillXxx()の呼び出しを使用してください。複数の図形の場合は、List<Shape>を使用します。 clearコマンドは単にList::clearになります。完全な例は、here;移動可能で、選択可能で、サイズ変更可能な色付きノードがエッジで接続されています。

image