2012-10-21 37 views
1

我有一個簡單的程序(大部分將用於其他內容),當用戶單擊drawPanel JPanel並顯示橢圓時,它將繪製橢圓。問題是repaint()方法不調用paintComponent()。爲什麼是這樣?repaint()未在Java程序中調用

下面是代碼:

// Imports Used: 
import java.awt.*; 
import java.awt.event.*; 
import java.util.ArrayList; 
import javax.swing.*; 
import java.awt.geom.*; 
// Class DrawPolygon 
public class DrawPolygon extends JPanel implements MouseListener 
{ 
// Variables used in GUI 
static JFrame frame; 
static JPanel drawPanel; 
static JPanel labelPanel; 
static JPanel primaryPanel; 
static JButton loadButton; 
static JButton saveButton; 
static JButton clearButton; 
static JButton addButton; 
static JButton moveButton; 
static JButton exitButton; 
// Variables used for GUI interaction 
static final int SIZE = 6; 
static int numVertices; 
ArrayList<Point> vertices; 
static Color outlineColor = Color.RED; 
static int lineWidth = 10; 
static Color fillColor = Color.BLACK; 
// Constructor for new Polygon Drawing Application 
public DrawPolygon() 
{ 
    // Create Frame 
    frame = new JFrame("Vector Painter"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    // Set Location of GUI on screen 
    frame.setLocation(225,100); 
    // Primary Panel to store Draw Panel and Lable/Button Panel 
    primaryPanel = new JPanel(); 
    // Create Label Panel 
    createLabelPanel(); 
    // Create Draw Panel 
    createDrawPanel(); 
    // Add panels to Primary 
    primaryPanel.add(labelPanel); 
    primaryPanel.add(drawPanel); 
    // Add to frame 
    frame.getContentPane().add(primaryPanel); 
    frame.pack(); 
    frame.setVisible(true); 
} 
// Add Buttons to left side 
public void createLabelPanel() 
{ 
    // Label Panel to add Buttons 
    labelPanel = new JPanel(); 
    labelPanel.setBackground(Color.BLACK); 
    labelPanel.setPreferredSize(new Dimension(200,600)); 
    // Create JButtons 
    loadButton = new JButton("LOAD"); 
    loadButton.setPreferredSize(new Dimension(180,75)); 
    loadButton.setBackground(Color.BLACK); 
    loadButton.setForeground(Color.WHITE); 
    loadButton.addMouseListener(this); 
    saveButton = new JButton("SAVE"); 
    saveButton.setPreferredSize(new Dimension(180,75)); 
    saveButton.setBackground(Color.BLACK); 
    saveButton.setForeground(Color.WHITE); 
    saveButton.addMouseListener(this); 
    clearButton = new JButton("CLEAR"); 
    clearButton.setPreferredSize(new Dimension(180,75)); 
    clearButton.setBackground(Color.BLACK); 
    clearButton.setForeground(Color.WHITE); 
    clearButton.addMouseListener(this); 
    addButton = new JButton("ADD"); 
    addButton.setPreferredSize(new Dimension(180,75)); 
    addButton.setBackground(Color.BLACK); 
    addButton.setForeground(Color.WHITE); 
    addButton.addMouseListener(this); 
    moveButton = new JButton("MOVE"); 
    moveButton.setPreferredSize(new Dimension(180,75)); 
    moveButton.setBackground(Color.BLACK); 
    moveButton.setForeground(Color.WHITE); 
    moveButton.addMouseListener(this); 
    exitButton = new JButton("EXIT"); 
    exitButton.setPreferredSize(new Dimension(180,75)); 
    exitButton.setBackground(Color.BLACK); 
    exitButton.setForeground(Color.WHITE); 
    exitButton.addMouseListener(this); 
    // Add Buttons to Label Panel 
    labelPanel.add(loadButton); 
    labelPanel.add(saveButton); 
    labelPanel.add(clearButton); 
    labelPanel.add(addButton); 
    labelPanel.add(moveButton); 
    labelPanel.add(exitButton); 
} 
// Creates Draw Panel 
public void createDrawPanel() 
{ 
    // Draw Panel to Draw Polygons 
    drawPanel = new JPanel(); 
    drawPanel.setBackground(Color.BLACK); 
    drawPanel.setPreferredSize(new Dimension(600,600)); 
    drawPanel.addMouseListener(this); 
    vertices = new ArrayList<>(); 
} 

@Override 
public void paintComponent(Graphics g) 
{ 
    super.paintComponent(g); 

    g.setColor(Color.ORANGE); 

    for (Point spot : vertices) 
    { 
     g.fillOval(spot.x-SIZE, spot.y-SIZE, SIZE*2, SIZE*2); 
    } 

    g.drawString("Count: " + vertices.size(), 5, 15); 

    System.out.println("repaint is working?"); 
} 

// Execute when Load button is clicked 
public void loadButton() 
{ 
    System.out.println("Load Button CLICKED!!"); 
} 
// Execute when Save button is clicked 
public void saveButton() 
{ 
    System.out.println("Save Button CLICKED!!"); 
} 
// Execute when Clear button is clicked 
public void clearButton() 
{ 
    System.out.println("Clear Button CLICKED!!"); 
} 
// Execute when Add button is clicked 
public void addButton() 
{ 
    System.out.println("Add Button CLICKED!!"); 
} 
// Execute when Move button is clicked 
public void moveButton() 
{ 
    System.out.println("Move Button CLICKED!!"); 
} 
public void mouseClicked(MouseEvent e) 
{ 
    if (e.getSource() == loadButton) 
    { 
     loadButton(); 
    } 
    else if (e.getSource() == saveButton) 
    { 
     saveButton(); 
    } 
    else if (e.getSource() == clearButton) 
    { 
     clearButton(); 
    } 
    else if (e.getSource() == addButton) 
    { 
     addButton(); 
    } 
    else if (e.getSource() == moveButton) 
    { 
     moveButton(); 
    } 
    else if (e.getSource() == exitButton) 
    { 
     System.exit(0); 
    } 
    else if (e.getSource() == drawPanel) 
    { 
     System.out.println("TEST"); 
     vertices.add(e.getPoint()); 
     repaint(); 
    } 
} 
// These are here because program wouldn't compile without them 
public void mousePressed(MouseEvent e){} 
public void mouseReleased(MouseEvent e){} 
public void mouseExited(MouseEvent e){} 
public void mouseEntered(MouseEvent e){} 
// Main Function 
public static void main(String[] args) 
{ 
    // Create Frame 
    new DrawPolygon();   
} 
} 

回答

4

其實重繪被調用,但目前DrawPolygon對象,但因爲你似乎不被顯示這些paintComponent(...)的一個永遠不會被調用。爲了澄清,您當前的類DrawPolygon擴展了JPanel,但它似乎並未添加到GUI層次結構中的任何容器。也許你想添加你的當前對象,你的this,到某處的GUI?實際上,你可能應該考慮使用當前對象來代替drawPanel JPanel對象。我會考慮完全刪除該變量。

與您的問題無關,您幾乎從不想在JButton上使用MouseListener,而應該使用ActionListeners。

+0

我沒有關注......當用戶在drawPanel中單擊時調用重繪時,mouseListener執行add(Point),但只是跳過重繪()...我不確定你的意思關於其餘的? –

+0

@James,你在你的DrawPolygon對象中覆蓋paintComponent,但又是在哪裏添加一個DrawPolygon實例到你的GUI?你在哪裏調用'add(this)'在哪裏?再次,擺脫你的drawPanel變量,只是刪除它,並在你使用它的任何地方,用'this'替代。 –

+0

在主函數中,我創建了一個DrawPolygon對象的實例並以這種方式運行GUI。 –