2014-04-07 77 views
0

我目前正在使用Swing庫和Graphics2D來繪製Java繪畫程序。我的主要GUI類擴展了JComponent,並且我試圖將它放在JPanel中,並將JPanel放在JFrame中,以便在屏幕上顯示它。當啓動程序時,JComponent看起來只是一條黑線(邊框,它被設置爲組件周圍的黑線)。我不明白爲什麼會發生這種情況,而且我已經調試了好幾個小時。如果有人可以在這個程序中發現錯誤,我會非常高興。提前致謝。Swing應用程序不在JPanel內部繪製JComponent

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

public class PaintGUI extends JComponent { 

    private static final long serialVersionUID = 1L; 

    JButton red, green, blue, clear; 
    Image img; 
    Graphics2D gfx; 
    JFrame drawFrame; 
    JPanel btnPan, drawPan; 
    MyListener ml; 
    Action act; 
    int x, y, prevX, prevY; 

    public PaintGUI(){ 
     //Initialisering av panel, frame og content 
     drawFrame = new JFrame("IFIPaint"); 
     drawFrame.setLayout(new BorderLayout()); 
     btnPan = new JPanel(); 
     drawPan = new JPanel(); 
     btnPan.setLayout(new FlowLayout()); 
     drawPan.setLayout(new BorderLayout()); 
     this.setEnabled(true); 

     //Setter størrelser 
     btnPan.setPreferredSize(new Dimension(30, 60)); 
     btnPan.setMinimumSize(new Dimension(30, 60)); 
     btnPan.setMaximumSize(new Dimension(30, 60)); 

     //Ordner knappene 
     red = new JButton("Rød"); 
     green = new JButton("Grønn"); 
     blue = new JButton("Blå"); 
     clear = new JButton("Slett alt"); 

     //Putter knappene på panelet 
     btnPan.add(red); 
     btnPan.add(green); 
     btnPan.add(blue); 
     btnPan.add(clear); 

     //Legger på action listeners 
     act = new Action(); 
     red.addActionListener(act); 
     green.addActionListener(act); 
     blue.addActionListener(act); 
     clear.addActionListener(act); 

     //Fullfører vindu og setter synlighet 
     drawFrame.setSize(500, 500); 
     drawPan.setBounds(0, 0, 400, 400); 
     this.setBounds(0, 0, 400, 400); 
     drawPan.add(this); 
     this.setBackground(Color.RED); 
     drawFrame.add(drawPan, BorderLayout.NORTH); 
     drawFrame.add(btnPan, BorderLayout.SOUTH); 

     this.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 

     this.setVisible(true); 
     drawPan.setVisible(true); 
     btnPan.setVisible(true); 
     drawFrame.setVisible(true); 

     this.paintComponent(gfx); 

     drawFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     draw(); 
    } 

    public void draw() { 
     ml = new MyListener(); 
     this.addMouseListener(ml); 
     this.addMouseMotionListener(ml); 
    } 

    public void paintComponent(Graphics g) { 

     super.paintComponent(g); 

     if(img == null){ 
      img = createImage(this.getWidth(),this.getHeight()); 
      gfx = (Graphics2D) img.getGraphics(); 
      gfx.setPaint(Color.RED); 
      gfx.fillRect(0, 0, this.getSize().width, this.getSize().height); 
      gfx.setPaint(Color.RED); 
      gfx.dispose(); 
     } 

     gfx.drawImage(img, 0, 0, this); 
    } 


    class Action implements ActionListener { 

     public void actionPerformed(ActionEvent e) { 
      if(e.getSource() == red){ 
       gfx.setPaint(Color.RED); 
       repaint(); 
      } else if (e.getSource() == green){ 
       gfx.setPaint(Color.GREEN); 
       repaint(); 
      } else if (e.getSource() == blue) { 
       gfx.setPaint(Color.BLUE); 
       repaint(); 
      } else if (e.getSource() == clear) { 
       gfx.clearRect(0, 0, drawFrame.getWidth(), drawFrame.getHeight()); 
       repaint(); 
      } 
     } 

    } 

    class MyListener extends MouseAdapter { 

     public void mousePressed(MouseEvent e) { 
      prevX = e.getX(); 
      prevY = e.getY(); 
      System.out.println("o ye"); 
     } 

     public void mouseDragged(MouseEvent e) { 
      x = e.getX(); 
      y = e.getY(); 
      gfx.drawLine(prevX, prevY, x, y); 
      repaint(); 
      prevX = x; 
      prevY = y; 

     } 
    } 

} 
+0

沒有,這是一個不同的問題和一個不同的問題。我考慮編輯一個適合這個,但那麼答案沒有任何意義。 –

+0

你還沒有添加PaintGUI到任何東西,因此它不會畫任何東西 – MadProgrammer

回答

2

您將PaintGUI添加到框架,但LayoutManager不知道大小並且無法設置所需的大小。

任一組優選的大小(或重寫的getPreferredSize以返回所希望的尺寸)

或具有優選大小添加到PaintGUI實例一些組分(例如按鈕)

+0

謝謝!現在它正在繪製適當大小的矩形。我仍然有一個問題 - 當拖動時,沒有任何反應。它也不會像我想的那樣用紅色油漆填充圖像。關於這些錯誤的任何提示? –