2015-10-21 236 views
0

Coordinate SystemJAVA GUI圖形上的座標系

我有一個名爲「GridPanel中」即在屏幕上畫出座標系類。

public class GridPanel extends JPanel 
{ 
    protected paintComponent(Graphics g){ 
      // draws the Coordinate System with grid 
    } 
} 

另一個叫做「形狀」的類在座標系上繪製自定義形狀。

public class Shape extends JComponent{ 
      protected paintComponent(Graphics g){ 
       // draws the shape on to the coordinate system 
      } 
    } 

我正在尋找一種方式做造型類裏面的畫,而不必一次又一次地繪製座標系。

+0

讓你'Shape'類某種真棒「繪製」類的,你的'GridPane'可以畫畫,這將是簡單 – MadProgrammer

+0

@MadProgrammer我如何才能控制到GridPanel中的paintComponent()方法中借鑑? – chaoqunli

+0

通常你會對你想要繪製的東西有一些參考,可能在'List'中,這取決於你的需要 – MadProgrammer

回答

2

「如何」將歸結爲「什麼」。

一般來說,如果你自己動手繪製東西,你會發現將東西繪製到GridPane上更容易,而不是依賴佈局管理器之類的東西。

例如。這創建了一個簡單的interface,使用單個方法將事物繪製到網格上。

public interface GridShape { 
    public void draw(Graphics2D g2d, JComponent parent); 
} 

然後,這是由什麼都想要畫到網格

public class WaveShape implements GridShape { 

    @Override 
    public void draw(Graphics2D g2d, JComponent parent) { 
     g2d.setColor(Color.RED); 

     int xDiff = parent.getWidth()/4; 
     int height = parent.getHeight() - 1; 

     int xPos = 0; 

     GeneralPath path = new GeneralPath(); 
     path.moveTo(0, 0); 
     path.curveTo(xPos + xDiff, 0, xPos, height, xPos + xDiff, height); 
     xPos += xDiff; 
     path.curveTo(xPos + xDiff, height, xPos, 0, xPos + xDiff, 0); 
     xPos += xDiff; 
     path.curveTo(xPos + xDiff, 0, xPos, height, xPos + xDiff, height); 
     xPos += xDiff; 
     path.curveTo(xPos + xDiff, height, xPos, 0, xPos + xDiff, 0); 
     g2d.draw(path); 
    } 

} 

GridPane然後繪製本身,然後畫什麼都「形」它(這個例子是非常基本的實現,但你可以有一個setter從而改變其如果需要塗或,「形」,有一個List它允許你同時繪製多個形狀)

WaveForm

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.geom.GeneralPath; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TestGrid { 

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

    public TestGrid() { 
     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 GridPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class GridPane extends JPanel { 

     private WaveShape waveShape; 

     public GridPane() { 
      waveShape = new WaveShape(); 
     } 

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

     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      g2d.drawLine(getWidth()/2, 0, getWidth()/2, getHeight()); 
      g2d.drawLine(0, getHeight()/2, getWidth(), getHeight()/2); 
      g2d.dispose(); 

      // I don't trust you 
      g2d = (Graphics2D) g.create(); 
      waveShape.draw(g2d, this); 
      g2d.dispose(); 
     } 

    } 

    public interface GridShape { 
     public void draw(Graphics2D g2d, JComponent parent); 
    } 

    public class WaveShape implements GridShape { 

     @Override 
     public void draw(Graphics2D g2d, JComponent parent) { 
      g2d.setColor(Color.RED); 

      int xDiff = parent.getWidth()/4; 
      int height = parent.getHeight() - 1; 

      int xPos = 0; 

      GeneralPath path = new GeneralPath(); 
      path.moveTo(0, 0); 
      path.curveTo(xPos + xDiff, 0, xPos, height, xPos + xDiff, height); 
      xPos += xDiff; 
      path.curveTo(xPos + xDiff, height, xPos, 0, xPos + xDiff, 0); 
      xPos += xDiff; 
      path.curveTo(xPos + xDiff, 0, xPos, height, xPos + xDiff, height); 
      xPos += xDiff; 
      path.curveTo(xPos + xDiff, height, xPos, 0, xPos + xDiff, 0); 
      g2d.draw(path); 
     } 

    } 

}