2017-10-13 66 views
1

這是我的代碼來畫多條......無法立刻

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

public class LineDrawing { 
    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       createWindow(); 
      } 
     }); 
    } 

    private static void createWindow() { 
     JFrame mainFrame = new JFrame(); 

     mainFrame.add(new myPanel()); 
     mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
     mainFrame.setBackground(Color.white); 
     mainFrame.setLocationRelativeTo(null); 
     mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     mainFrame.setVisible(true); 
    } 
} 

class myPanel extends JPanel { 
    int startX; 
    int startY; 
    int endX; 
    int endY; 

    public myPanel() { 

     addMouseListener(new MouseAdapter() { 
      @Override 
      public void mousePressed(MouseEvent e) { 
       startX = e.getX(); 
       startY = e.getY(); 
       repaint(startX, startY, endX, endY); 
      } 

      @Override 
      public void mouseReleased(MouseEvent e) { 
       endX = e.getX(); 
       endY = e.getY(); 
       repaint(startX, startY, endX, endY); 
      } 

      @Override 
      public void mouseClicked(MouseEvent e) { 
       startX = e.getX(); 
       endX = startX; 
       startY = e.getY(); 
       endY = startY; 
       repaint(startX, startY, endX, endY); 
      } 
     }); 

     addMouseMotionListener(new MouseMotionAdapter() { 
      @Override 
      public void mouseDragged(MouseEvent e) { 
       endX = e.getX(); 
       endY = e.getY(); 
       repaint(startX, startY, endX, endY); 
      } 
     }); 
    } 

    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.setColor(Color.black); 
     g.drawLine(startX, startY, endX, endY); 
     repaint(); 
    } 
} 

我想畫多條,同時保持前那些仍然存在的在屏幕上(框架)... 但是,當我再次點擊前行被擦或刪除了......

我試圖設計一個圖表分析的應用程序(它會告訴輸入圖形是否同構或不...

我的問題是,我無法繪製多條線,但只有一條線......

+0

請參閱[自定義繪畫方法](https://tips4java.wordpress.com/2009/05/08/custom-painting-approaches/),瞭解執行此操作的兩種常用方法。 – camickr

回答

2

我的問題是,我不能畫多條,但只有一條線......

它繪製1行只因爲你只在你的paintComponent吸引了1行。 請注意,圖紙將不是是累計。

protected void paintComponent(Graphics g) 
{ 
    super.paintComponent(g); 
    g.setColor(Color.black); 
    g.drawLine(startX,startY,endX,endY); //this will draw only one line.. 
    repaint();       //do not do this! 
} 

要繪製多條線(如圖),您需要一個數據結構來保存每個繪圖的數據。然後你可以迭代它並從那裏畫多條線。

因此,而不是隻保持一套陰謀(startX, startY, endX, endY)。此外

@Override 
protected void paintComponent(Graphics g){ 
    super.paintComponent(g); 
    for(Plot p : plots) 
     p.draw(g);  //Each plot will draw itself 
} 

,:您可以爲繪圖創建一個類:

public class Plot 
{ 
    private int startX; 
    private int startY; 
    private int endX; 
    private int endY; 

    public Plot(int startX, int startY, int endX, int endY){ 
     this.startX = startX; 
     this.startY = startY; 
     this.endX = endX ; 
     this.endY = endY ; 
    } 

    public void draw(Grpahics g){ 
     g.setColor(Color.black); 
     g.drawLine(startX,startY,endX,endY);   
    } 
} 
在顯示面板

然後,保持情節的列表:

class MyPanel extends JPanel 
{ 
    private ArrayList<Plot> plots; 

    public MyPanel(){ 
     plots = new ArrayList<Plot>(); 
    } 

    public void addPlot(int x1, int y1, int x2, int y2){ 
     plots.add(new Plot(x1, y1, x2, y2)) 
    } 

    //Invoke addPlot() in your mouseListener after you get startXY & endXY 
    //Invoke repaint() after addPlot() 
    //Don't have to do this: "repaint(startX,startY,endX,endY);" 
} 

於是最後,在你的paintComponent()您不應該在paintComponent()內撥打repaint()。當您需要刷新屏幕時,您可以在paintComponent()以外將其撥打。