2013-06-04 93 views
0

我有一個Java繪畫程序,我有兩個問題需要處理。這兩個問題都比較簡單,只是考慮如何處理鼠標輸入以及圖像如何使用顏色。這裏的應用程序的照片:關於Java繪畫程序在繪畫時出現多個問題

enter image description here

因此,這裏是我的第一個問題:

正如你所看到的,通過應用程序的外觀,有一個在油漆區點的噴霧。每個點都是鼠標點擊。該程序無法識別用戶何時按住鼠標按鈕,因此您必須單獨單擊。

這顯然會適得其反,用戶不友好且不可接受。現在,我如何解決這個問題,我不確定。我嘗試過使用永久性的while (true)循環,但這不起作用。我該如何做到這一點,而不是每次都點擊鼠標,每次按下鼠標時都會噴出點?

第二個問題是點的顏色。正如你所看到的,底部有顏色按鈕。這些功能,但有一個問題:每當我改變顏色,屏幕上當前所有的點改變顏色。顏色由稱爲currentColor的變量運行,該變量由由底部面板上的所有顏色按鈕控制的actionListeners運行。我如何確保已放置在屏幕上的顏色不再受影響?

我相信所有可以解決這兩個問題的代碼都在我自定義的JPanel中,該程序用於程序繪製。我將在下面發佈整個課程,如果您有任何其他問題,請告訴我。

int xCord, yCord; 

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

    public PaintPanel() { 
     addMouseListener(this); 
    } 

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

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     for (Point point : points) { 
      g.setColor(currentColor); 
      g.fillOval(point.x, point.y, 12, 12); 

     } 
     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())) { 
       points.add(m.getPoint()); 
       xCord = m.getX(); 
       yCord = m.getY(); 
       System.out.println("x: " + xCord + " y: " + yCord); 
     } 

    } 

    @Override 
    public void mouseReleased(MouseEvent m) { 
    } 

} 
+0

開始通過在考慮看看[如何寫的鼠標移動偵聽器(http://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener .html) – MadProgrammer

+2

閣下,千萬不要從'paintComponent(...)'裏面調用'repaint()'。永遠。我的意思是永遠。 –

+0

@HovercraftFullOfEels我以前聽說過。你能向我解釋爲什麼,或給我一個鏈接,或什麼?這會爲我節省很多時間。謝謝。 – user2398233

回答

1

Swing中的繪畫具有破壞性。

也就是說,當Swing請求在組件上發生重繪時,該組件應該清除以前繪製的內容並自行更新。

你的顏色問題的問題是,你只有一個指定的顏色。

一個可能的解決方案是繪製到後臺緩衝區(如BufferedImage)而不是依靠paintComponent

每次調用paintComponent而不是重新繪製所有點時,您只需改爲繪製BufferedImage即可。

至於用鼠標您的問題,您需要實現一個MouseMotionListener,這將讓你當鼠標在表面上拖檢測,畫點的蹤跡

更新用非常簡單的例子

enter image description here

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Point; 
import java.awt.event.ActionEvent; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.image.BufferedImage; 
import javax.swing.AbstractAction; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class SimplePaint04 { 

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

    public SimplePaint04() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private PaintPane paintPane; 

     public TestPane() { 
      setLayout(new BorderLayout()); 
      add((paintPane = new PaintPane())); 
      add(new ColorsPane(paintPane), BorderLayout.SOUTH); 
     } 
    } 

    public class ColorsPane extends JPanel { 

     public ColorsPane(PaintPane paintPane) { 
      add(new JButton(new ColorAction(paintPane, "Red", Color.RED))); 
      add(new JButton(new ColorAction(paintPane, "Green", Color.GREEN))); 
      add(new JButton(new ColorAction(paintPane, "Blue", Color.BLUE))); 
     } 

     public class ColorAction extends AbstractAction { 

      private PaintPane paintPane; 
      private Color color; 

      private ColorAction(PaintPane paintPane, String name, Color color) { 
       putValue(NAME, name); 
       this.paintPane = paintPane; 
       this.color = color; 
      } 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       paintPane.setForeground(color); 
      } 

     } 

    } 

    public class PaintPane extends JPanel { 

     private BufferedImage background; 

     public PaintPane() { 
      setBackground(Color.WHITE); 
      setForeground(Color.BLACK); 
      MouseAdapter handler = new MouseAdapter() { 

       @Override 
       public void mousePressed(MouseEvent e) { 
        drawDot(e.getPoint()); 
       } 

       @Override 
       public void mouseDragged(MouseEvent e) { 
        drawDot(e.getPoint()); 
       } 

      }; 
      addMouseListener(handler); 
      addMouseMotionListener(handler); 
     } 

     protected void drawDot(Point p) { 
      if (background == null) { 
       updateBuffer();; 
      } 

      if (background != null) { 
       Graphics2D g2d = background.createGraphics(); 
       g2d.setColor(getForeground()); 
       g2d.fillOval(p.x - 5, p.y - 5, 10, 10); 
       g2d.dispose(); 
      } 
      repaint(); 
     } 

     @Override 
     public void invalidate() { 
      super.invalidate(); 
      updateBuffer(); 
     } 

     protected void updateBuffer() { 

      if (getWidth() > 0 && getHeight() > 0) { 
       BufferedImage newBuffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB); 
       Graphics2D g2d = newBuffer.createGraphics(); 
       g2d.setColor(Color.WHITE); 
       g2d.fillRect(0, 0, getWidth(), getHeight()); 
       if (background != null) { 
        g2d.drawImage(background, 0, 0, this); 
       } 
       g2d.dispose(); 
       background = newBuffer; 
      } 

     } 

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

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      if (background == null) { 
       updateBuffer(); 
      } 
      g2d.drawImage(background, 0, 0, this); 
      g2d.dispose(); 
     } 
    } 
} 
+0

非常感謝,我很欣賞有用的答案和很好的例子。 – user2398233

+0

我希望它能幫助你更接近你的目標:D – MadProgrammer