2014-11-22 111 views
0

我正在嘗試使用按鈕移動形狀,但是我沒有理解它。這是我畫類如何在java swing中向左,向右,向上,向下移動形狀

class MyCanvas extends JComponent { 


     public void paint(Graphics g) { 

     g.setColor(Color.RED); 
     g.fill3DRect(20, 20, 200, 200,true); 
     } 

    } 
class MyCanvas1 extends JComponent { 


     public void paint(Graphics g) { 

     g.setColor(Color.RED); 
     g.drawRect(20, 20, 200, 200); 
     } 
    } 

我做的下面的代碼成功繪製 - >

public class ButtonListener implements ActionListener 
    { 



    public void actionPerformed(ActionEvent e) { 
     if(check) 
     { 

      if(e.getSource()==btnNewButton) 
      { 
       MyCanvas can=new MyCanvas(); 
       me s=new me(); 
       can.setSize(400, 500); 
       panel_3.add(can); 
       panel_3.repaint(); 
       addComponent(can); 
       s.can; 
       choice=true; 

      } 
     } 
     else 
     { 
     if(e.getSource()==btnNewButton) 
      { 
       panel_3.removeAll(); 
       MyCanvas1 can=new MyCanvas1(); 
       can.setSize(500, 500); 
       panel_3.add(can); 
       panel_3.repaint(); 
       addComponent(can); 
      } 
     } 

它爲我工作的罰款。現在我有另一個4按鈕,我想通過點擊按鈕來移動形狀。我嘗試了一些方法,但沒有一個爲我工作。任何建議我如何做到這一點。我應該創建任何新的行動事件或類。

+0

首先,瞭解繪畫在Swing中的工作原理,參見[繪畫AWT和Swing](http://www.oracle.com/technetwork/java/painting-140037.html)和[執行自定義繪畫](http://docs.oracle.com/javase/tutorial/uiswing/painting/ )。 – MadProgrammer 2014-11-22 08:48:33

+0

我正在閱讀並仍在尋找解決方案 – 2014-11-22 09:10:03

+0

*「我嘗試了一些方法..」*向我們展示您在[MCVE](http://stackoverflow.com/help/mcve)(最小完整可驗證示例)中嘗試​​的內容。 – 2014-11-22 09:10:10

回答

0

我爲你寫了一個快速代碼,只是爲了從 得到想法你可以使用Component setLocation(int x,int y);爲了獲得當前的x和y位置,可以使用JComponent getX()和getY()方法。

例如comp.setLocation(comp.getX(),comp.getY());

public class Move extends JFrame implements ActionListener { 

    JComponent b = new JButton("Move me"); 
    JButton a = new JButton("Move that"); 
    Random rd = new Random(); 

    public Move() throws HeadlessException { 
     setLayout(new FlowLayout()); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     add(a); 
     add(b); 
     a.addActionListener(this); 
     setVisible(true); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 

     repaint(); 
    } 

    public static void main(String[] args) { 
     new Move(); 
    } 

    @Override 
    public void paint(Graphics g) { 
     super.paint(g); 
     int r = rd.nextInt(400); 
     b.setLocation(b.getX(), r); 
    } 

} 
+0

如果'a'和'b'沒有畫出框架的'paint'方法繪製的東西,你會很幸運...... – MadProgrammer 2014-11-22 09:19:38

+0

*「例如comp。 setLocation(comp.getX(),comp.getY());「* - 不是在涉及佈局管理器的時候,當容器被重新驗證時,組件的位置和大小將被重置爲佈局管理器想要的使用,我不會建議使用'null'佈局,因爲不適合真正需要做的事情只會吸引高級Swing海報的憤怒。 – MadProgrammer 2014-11-22 09:40:29

1

首先...

瞭解在Swing繪畫作品如何,看Painting in AWT and SwingPerforming Custom Painting

建議您覆蓋的paintComponent代替paint並始終確保你打電話super.paintComponent

public class MyCanvas extends JComponent { 

    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.setColor(Color.RED); 
     g.fill3DRect(20, 20, 200, 200,true); 
    } 

} 

其次...

有許多方法你可能做到這一點,我個人'd只是利用[Graphics2DShape的API](2D Graphics),但有些事告訴我對你來說不夠好,所以...

而不是試圖用基於組件的形狀(和要爭取佈局管理器),創建它描述了「繪製」元素的接口,這東西可畫...

public interface Drawable { 
    public Rectangle getBounds(); 
    public void setBounds(Rectangle bounds); 
    public Color getColor(); 
    public void setColor(Color color); 
    public void draw(Graphics2D g2d); 
} 

這描述的東西,可以繪製,地點和什麼顏色

接下來,保持DrawablesList一個S和使用paintComponent方法來畫他們......

public class MyCanvas extends JComponent { 

    private List<Drawable> drawables; 

    public MyCanvas() { 
     drawables = new ArrayList<>(25); 
    } 

    public Dimension getPreferredSize() { 
     return new Dimension(400, 400); 
    } 

    public void add(Drawable drawable) { 
     drawables.add(drawable); 
     repaint(); 
    } 

    public void remove(Drawable drawable) { 
     drawables.remove(drawable); 
     repaint(); 
    } 

    public int getDrawableCount() { 
     return drawables.size(); 
    } 

    public Drawable getDrawableAt(int index) { 
     return drawables.get(index); 
    } 

    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     Graphics2D g2d = (Graphics2D)g.create(); 
     for (Drawable d : drawables) { 
      d.draw(g2d); 
     } 
     g2d.dispose(); 
    } 

} 

現在你有一些畫和someth可以畫出他們...

第三...

做些事情來繪製。我會用一個抽象Drawable涵蓋了所有常見的東西,使生活更輕鬆的開始......

public abstract class AbstractDrawable { 
    private Rectangle bounds; 
    private Color color 

    public AbstractDrawable(Rectangle bounds, Color color) { 
     setBounds(bounds); 
     setColor(color); 
    } 

    public Rectangle getBounds() { 
     return bounds; 
    } 
    public void setBounds(Dimension bounds) { 
     this.bounds = bounds; 
    } 
    public Color getColor() { 
     return color; 
    } 
    public void setColor(Color color) { 
     this.color = color; 
    } 
    public abstract void draw(Graphics2D g2d); 
} 

的,你可以去堅果...

public class DrawableRectangle { 

    public AbstractDrawable(Rectangle bounds, Color color) { 
     super(bounds, color); 
    } 

    public abstract void draw(Graphics2D g2d) { 
     g2d.setColor(getColor()); 
     g2d.fill(getBounds()); 
    } 
} 

和/或...

public class Drawable3DRectangle { 

    public AbstractDrawable(Rectangle bounds, Color color) { 
     super(bounds, color); 
    } 

    public abstract void draw(Graphics2D g2d) { 
     g2d.setColor(getColor()); 
     Rectangle bounds = getBounds(); 
     g2d.fill3DRect(bounds.x, bounds.y, bounds.width, bounds.height); 
    } 
} 

第四...

把它放在一起......

import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class DrawableShapes { 

    public static void main(String[] args) { 
     new DrawableShapes(); 
    } 

    public DrawableShapes() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private MyCanvas canvas; 

     public TestPane() { 
      setLayout(new BorderLayout()); 
      canvas = new MyCanvas(); 
      add(canvas); 

      canvas.add(new Drawable3DRectangle(new Rectangle(20, 20, 100, 100), Color.RED)); 
      canvas.add(new DrawableRectangle(new Rectangle(20, 140, 100, 100), Color.RED)); 

     } 

    } 

} 

好了,所以這個油漆2個繪圖資源,但如何將他們?

基本上,你搶你要移動,有點像Drawable參考...

Drawable drawable = canvas.getDrawableAt(0); 

然後修改它的位置...

drawable.getBounds().x += 5; 
drawable.getBounds().y += 5; 

然後你重繪MyCanvas實例...

canvas.repaint(); 
相關問題