2016-02-14 67 views
-1

找到這個示例代碼,它應該在點擊後產生一條畫線,但不顯示任何內容或工作。假設所有的導入語句是正確的,代碼沒有錯誤,我不知道爲什麼它不起作用。線條的顏色是紅色的,而背景是白色的,所以它應該清楚地顯示它是否工作。鼠標監聽器似乎也是正確的。任何爲什麼此代碼不起作用的原因?用MouseAdapter繪製線條不顯示Java

public class PathPanel extends JPanel { 

/** 
* The panel width. 
*/ 
public static final int WIDTH = 400; 

/** 
* The panel height. 
*/ 
public static final int HEIGHT = 400; 

/** 
* The background color of the panel. 
*/ 
public static final Color BACKGROUND_COLOR = Color.WHITE; 

/** 
* The color to paint with. 
*/ 
public static final Color FOREGROUND_COLOR = Color.RED; 

/** 
* The line width. 
*/ 
public static final int LINE_WIDTH = 8; 

// Instance Fields 

/** 
* 
*/ 
private static final long serialVersionUID = -3644129903653409515L; 

/** 
* The path being created. 
*/ 
private final Path2D myPath; 

// OR you could use Path2D.Double instead of GeneralPath 

// Constructor 

/** 
* Constructs a new general path panel. 
*/ 
public PathPanel() { 
    super(); 
    myPath = new GeneralPath(); 
    myPath.setWindingRule(GeneralPath.WIND_EVEN_ODD); 

    //myPath = new Path2D.Double(); 

    setPreferredSize(new Dimension(WIDTH, HEIGHT)); 
    setBackground(BACKGROUND_COLOR); 
    addMouseListener(new MyMouseListener()); 
} 

/** 
* Paints the current path. 
* 
* @param theGraphics The graphics context to use for painting. 
*/ 
@Override 
public void paintComponent(final Graphics theGraphics) { 
    super.paintComponent(theGraphics); 
    final Graphics2D g2d = (Graphics2D) theGraphics; 

    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
         RenderingHints.VALUE_ANTIALIAS_ON); 
    g2d.setPaint(FOREGROUND_COLOR); 
    g2d.setStroke(new BasicStroke(LINE_WIDTH)); 
    g2d.draw(myPath); 
} 

// Main Method 

/** 
* Creates and displays a GeneralPathPanel. 
* 
* @param theArgs Command line arguments (ignored). 
*/ 
public static void main(final String... theArgs) { 
    final PathPanel panel = new PathPanel(); 
    final JFrame frame = new JFrame("GeneralPathPanel Demo"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.add(panel); 
    frame.pack(); 
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true); 
} 

// Inner Class 

/** 
* Listens for mouse clicks, to draw on our panel. 
*/ 
private class MyMouseListener extends MouseAdapter { 
    /** 
    * Handles a click event. 
    * 
    * @param theEvent The event. 
    */ 
    @Override 
    public void mouseClicked(final MouseEvent theEvent) { 

     if (myPath.getCurrentPoint() == null) { 
      myPath.moveTo(theEvent.getX(), theEvent.getY()); 
     } else if (theEvent.getClickCount() == 2) { 
      myPath.closePath(); 
     } else { 
      myPath.lineTo(theEvent.getX(), theEvent.getY()); 
     } 
     repaint(); 
    } 
} 

}

回答

0

示例代碼,您張貼優秀作品對我來說。您是否嘗試在mouseClicked(final MouseEvent theEvent)方法中添加System.out.println()以檢查它是否實際被調用?如果未被調用,您可以嘗試將其更改爲mouseReleased

進口我用:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.geom.GeneralPath; 
import java.awt.geom.Path2D;