2013-06-03 144 views
0

我有一個使用自定義JPanel繪製的Java繪畫程序。當點擊JPanel畫一個小橢圓形(或者如果你願意的話),每當你點擊另一個地方時橢圓消失。座標也得到更新,但橢圓形不留,它移動到任何地方的用戶點擊一個... ... 這裏有一個自定義的JPanel代碼:爲什麼這個Java繪畫程序不能繪製多個橢圓形?

int xCord, yCord; 

    public class PaintPanel extends JPanel implements MouseListener { 
     // default serial whatever... 
     private static final long serialVersionUID = -6514297510194472060L; 

     // initial values 
     int xCord = -10; 
     int yCord = -10; 

     public PaintPanel() { 
      addMouseListener(this); 
     } 

     @Override 
     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      g.setColor(ProgramUI.currentColor); 
      g.fillOval(xCord, yCord, 8, 8); 
      repaint(); 
     } 

     @Override 
     public void mouseClicked(MouseEvent m) { 
     } 

     @Override 
     public void mouseEntered(MouseEvent m) { 
     } 

     @Override 
     public void mouseExited(MouseEvent m) { 
     } 

     @Override 
     public void mousePressed(MouseEvent m) { 
      if (paintPanel.contains(m.getPoint())) { 
       xCord = m.getX(); 
       yCord = m.getY(); 
       System.out.println("x: " + xCord + " y: " + yCord); 
      } 
     } 

     @Override 
     public void mouseReleased(MouseEvent m) { 
     } 

    } 

我需要鼠標的召開,不斷作畫一個橢圓直到放開鼠標按鈕。這裏唯一的問題是鼠標橢圓更新,但不保存它的原始位置。我該如何解決?

回答

5

只有一個橢圓形被畫成只有一個fillOval聲明繪製在paintComponent方法單一的橢圓形,因此聲明

super.paintComponent(g); 

導致任何以前的繪畫被清除一次repaint被調用。

作畫多個橢圓形,如Custom Painting Approaches

概述不要從paintComponent中調用repaint可以繪製從List<Point>組件。這會造成無限循環並降低性能。如果需要定期更新,請從ActionListener調用repaint代替Swing Timer

0

,這是因爲該組件重繪本身,以使更改永久,你應該採取的JPanel的圖像,並將其設置爲背景,一旦你完成每一次繪製...

0

你只畫最後每次點擊用戶點擊。相反,您需要收集過去的點擊並且每次都進行繪製。

該代碼會做你想要什麼:

package com.sandbox; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.util.ArrayList; 

public class SwingSandbox { 


    public static void main(String[] args) { 

     JFrame frame = buildFrame(); 
     frame.add(new PaintPanel()); 
    } 

    public static class PaintPanel extends JPanel implements MouseListener { 
     // default serial whatever... 
     private static final long serialVersionUID = -6514297510194472060L; 

     ArrayList<Point> points = new ArrayList<Point>(); 

     public PaintPanel() { 
      addMouseListener(this); 
     } 

     @Override 
     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      g.setColor(new Color(250)); 
      for (Point point : points) { 
       g.fillOval(point.x, point.y, 8, 8); 
      } 
      repaint(); 
     } 

     @Override 
     public void mouseClicked(MouseEvent m) { 
     } 

     @Override 
     public void mouseEntered(MouseEvent m) { 
     } 

     @Override 
     public void mouseExited(MouseEvent m) { 
     } 

     @Override 
     public void mousePressed(MouseEvent m) { 
      if (this.contains(m.getPoint())) { 
       points.add(m.getPoint()); 
      } 
     } 

     @Override 
     public void mouseReleased(MouseEvent m) { 
     } 

    } 


    private static JFrame buildFrame() { 
     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     frame.setSize(200, 200); 
     frame.setVisible(true); 
     return frame; 
    } 


}