2015-05-15 19 views
6

Tôi đang làm việc trên một máy đánh bạc trong Java và cho đến nay tôi đã tạo ra một nút ngẫu nhiên sẽ tạo ra hai hình ảnh. Mã của tôi biên dịch nhưng khi tôi chạy nó, tất cả những thứ tôi đưa vào phương pháp vẽ không hiển thị. Có điều gì tôi đang thiếu? Cảm ơn sự giúp đỡ của bạn và đây là một số mã của tôi.Cách sử dụng phương pháp sơn trong JPanel

public void paint(Graphics g) { 
     super.paintComponents(g); 
     g.drawString("Int 1 is" + int1,30,30); 
     g.drawString("Int 2 is" + int2,30,80); 
     switch (int1) { 
      case 0: 
       g.setColor(Color.white); 
       g.fillRect(300,300,300,500); 
       g.drawImage(img, 300, 500, this); 
       break; 
      case 1: 
       g.setColor(Color.white); 
       g.fillRect(300,300,300,500); 
       g.drawImage(img2,300,500,this); 
       break; 
      case 2: 
       g.setColor(Color.white); 
       g.fillRect(300,300,300,500); 
       g.drawImage(img3,300,500,this); 
       break; 
      case 3: 
       g.setColor(Color.white); 
       g.fillRect(300,300,300,500); 
       g.drawImage(img4,300,500,this); 
       break; 
      case 4: 
       g.setColor(Color.white); 
       g.fillRect(300,300,300,500); 
       g.drawImage(img5,300,500,this); 
       break; 
      case 5: 
       g.setColor(Color.white); 
       g.fillRect(300,300,300,500); 
       g.drawImage(img6,300,500,this); 
       break; 
      case 6: 
       g.setColor(Color.white); 
       g.fillRect(300,300,300,500); 
       g.drawImage(img7,300,500,this); 
       break; 
      case 7: 
       g.setColor(Color.white); 
       g.fillRect(300,300,300,500); 
       g.drawImage(img8,300,500,this); 
       break; 
      case 8: 
       g.setColor(Color.white); 
       g.fillRect(300,300,300,500); 
       g.drawImage(img9,300,500,this); 
       break; 
      case 9: 
       g.setColor(Color.white); 
       g.fillRect(300,300,300,500); 
       g.drawImage(img10,300,500,this); 
       break; 
     } 
     switch (int2) { 
      case 0: 
       g.setColor(Color.white); 
       g.fillRect(300,300,800,500); 
       g.drawImage(img, 800, 500, this); 
       break; 
      case 1: 
       g.setColor(Color.white); 
       g.fillRect(300,300,800,500); 
       g.drawImage(img2,800,500,this); 
       break; 
      case 2: 
       g.setColor(Color.white); 
       g.fillRect(300,300,800,500); 
       g.drawImage(img3,800,500,this); 
       break; 
      case 3: 
       g.setColor(Color.white); 
       g.fillRect(300,300,800,500); 
       g.drawImage(img4,800,500,this); 
       break; 
      case 4: 
       g.setColor(Color.white); 
       g.fillRect(300,300,800,500); 
       g.drawImage(img5,800,500,this); 
       break; 
      case 5: 
\    g.setColor(Color.white); 
       g.fillRect(300,300,800,500); 
       g.drawImage(img6,800,500,this); 
       break; 
      case 6: 
       g.setColor(Color.white); 
       g.fillRect(300,300,800,500); 
       g.drawImage(img7,800,500,this); 
       break; 
      case 7: 
       g.setColor(Color.white); 
       g.fillRect(300,300,800,500); 
       g.drawImage(img8,800,500,this); 
       break; 
      case 8: 
       g.setColor(Color.white); 
       g.fillRect(300,300,800,500); 
       g.drawImage(img9,800,500,this); 
       break; 
      case 9: 
       g.setColor(Color.white); 
       g.fillRect(300,300,800,500); 
       g.drawImage(img10,800,500,this); 
       break; 
     } 
     this.setVisible(true); 
    } 
+1

Khi giao dịch với 'Swing', người ta không nên chơi bằng phương pháp' paint' cho công việc như vậy. Thay vào đó, hãy ghi đè phương thức 'paintComponent (...)' của một số 'JComponent/JPanel' để làm các công cụ liên quan đến bức tranh. Thông tin thêm tại [Performing Custom Painting] (https://docs.oracle.com/javase/tutorial/uiswing/painting/). Do đọc các giải quyết vấn đề thường gặp, vấn đề đầu tiên sẽ trả lời những gì bạn làm sai. Hy vọng nó sẽ giúp :-) –

+1

Yikes, phương thức 'paint' của bạn gọi' super.paintComponents (...) 'là một điều rất nguy hiểm.Xin vui lòng đọc các hướng dẫn vẽ trên này như tất cả của nó rất tốt giải thích và đồng ý chính xác với những gì @ nIcEcOw nêu trên. Bản thân tôi, tôi chỉ đơn giản là trao đổi ImageIcons hiển thị trong một JLabel. –

+0

Vui lòng xem chỉnh sửa để trả lời. –

Trả lời

4

vấn đề:

  • Bạn đang vẽ trực tiếp trong một JFrame - không làm được điều này như bạn có thể mess lên đồ họa JFrame.
  • Ghi đè phương pháp vẽ của bạn và gọi phương thức super.paintComponents(...), một điều nguy hiểm nữa cần thực hiện và điều gì đó sẽ không bao giờ được thực hiện.

Thay vào đó bạn có thể làm bản vẽ của bạn trong phương pháp paintComponent(...) của một JPanel và gọi đúng super.paintComponent(...) phương pháp bên trong của nó, như mô tả trong Swing painting tutorials, nhưng tại sao bận tâm. Dễ dàng hơn nhiều là tạo một mảng ImageIcons và chỉ cần gọi setIcon(...) trên 3 (hoặc nhiều bạn cần) JLabels, sau khi chọn ngẫu nhiên một Biểu tượng từ mảng hoặc ArrayList.

Ngoài ra, không bao giờ làm điều này:

try { 

    // .... some code here 

} catch (IOException e) { 

} 

Ít nhất in ra stack trace trong khối catch để bạn có thể xác định bất kỳ trường hợp ngoại lệ IO nếu chúng xảy ra:

try { 

    // .... some code here 

} catch (IOException e) { 
    e.printStackTrace(); // ****** added ********  
} 

Đối ví dụ: mã sau sẽ tạo GUI này:

enter image description here

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.image.BufferedImage; 
import java.io.IOException; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Random; 

import javax.imageio.ImageIO; 
import javax.swing.*; 

@SuppressWarnings("serial") 
// extend JPanel, not JFrame as it gives the class more flexibility as to where to use 
public class ShowRandomImages extends JPanel { 
    // images from Andrew Thompson's example image page, 
    // http://stackoverflow.com/questions/19209650/example-images-for-code-and-mark-up-qas 
    private static final String IMAGE_SHEET_PATH = "http://i.stack.imgur.com/memI0.png"; 
    // how many JLabels to show in a row 
    private static final int LABEL_COUNT = 3; 
    // need to get subimages from image sheet. There are 6 columns in the sheet 
    private static final int IMAGE_COLUMNS = 6; 
    // array of JLabel 
    private JLabel[] labels = new JLabel[LABEL_COUNT]; 
    // hold all the images as ImageIcons read in 
    private List<Icon> imageIconList = new ArrayList<>(); 
    // to randomize the images 
    private Random random = new Random(); 

    // pass the ImageIcon List into this class 
    public ShowRandomImages(List<Icon> iconList) { 
     this.imageIconList = iconList; 
     // jpanel hold row of image-displaying JLabels 
     JPanel labelHolderPanel = new JPanel(new GridLayout(1, 0, 5, 0)); 
     for (int i = 0; i < labels.length; i++) { // create all JLabels in array 
     labels[i] = new JLabel(); 
     labels[i].setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY)); 
     labels[i].setHorizontalAlignment(SwingConstants.CENTER); // center the icon 
     labels[i].setIcon(getRandomIcon()); // initialize with a random image 
     labelHolderPanel.add(labels[i]); // add to holder JPanel 
     } 

     // panel to hold button at bottom 
     JPanel bottomPanel = new JPanel(); 
     // button uses an AbstractAction rather than an ActionListener 
     bottomPanel.add(new JButton(new ShowRandomIconAction("Show Random Image"))); 

     setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
     setLayout(new BorderLayout()); 
     add(labelHolderPanel, BorderLayout.CENTER); 
     add(bottomPanel, BorderLayout.PAGE_END); 
    } 

    private Icon getRandomIcon() { 
     int randomIndex = random.nextInt(imageIconList.size()); 
     return imageIconList.get(randomIndex); 
    } 

    private class ShowRandomIconAction extends AbstractAction { 
     public ShowRandomIconAction(String name) { 
     super(name); 
     int mnemonic = (int) name.charAt(0); 
     putValue(MNEMONIC_KEY, mnemonic); 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
     for (JLabel jLabel : labels) { 
      jLabel.setIcon(getRandomIcon()); 
     } 
     } 
    } 

    private static void createAndShowGui(List<Icon> imageIconList) { 
     ShowRandomImages mainPanel = new ShowRandomImages(imageIconList); 

     JFrame frame = new JFrame("ShowRandomImages"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     try { 
     final List<Icon> iconList = getImages(); 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGui(iconList); 
      } 
     }); 
     } catch (IOException e) { 
     e.printStackTrace(); 
     System.exit(-1); 
     } 
    } 

    // read in image sheet and extract sub-images from it 
    private static List<Icon> getImages() throws IOException { 
     List<Icon> iconList = new ArrayList<>(); 
     URL imageUrl = new URL(IMAGE_SHEET_PATH); 
     BufferedImage imageSheet = ImageIO.read(imageUrl); 
     for (int i = 0; i < IMAGE_COLUMNS; i++) { 
     int x = (int) ((double) i * imageSheet.getWidth()/IMAGE_COLUMNS); 
     int y = 0; 
     int w = imageSheet.getWidth()/IMAGE_COLUMNS; 
     int h = imageSheet.getHeight()/2; 

     BufferedImage subImage = imageSheet.getSubimage(x, y, w, h); 
     iconList.add(new ImageIcon(subImage)); 
     } 
     return iconList; 
    } 
} 

Nếu không, nếu bạn hoàn toàn phải hiển thị hình ảnh trong một phương pháp hội họa, tôi khuyên bạn nên:

  • Tạo một lớp mở rộng JPanel hiển thị chỉ là một hình ảnh, nói gọi ImageDisplayPanel. Bạn sẽ cung cấp cho lớp 2 của bạn nếu bạn cần hiển thị 2 hình ảnh.
  • đèo vào nó một List<BufferedImage>
  • Give it một phương pháp
  • displayRandomImage() Trong phương pháp này, chọn một hình ảnh ngẫu nhiên từ danh sách và thiết lập một trường BufferedImage đến hình ảnh này, và gọi repaint().
  • Phương thức paintComponent của DrawImagePanel sẽ vẽ hình ảnh được giữ bởi trường nếu nó không rỗng.
  • Trong GUI chính, hãy gọi phương thức này trên hình ảnh 2 hoặc 3 hình chữ nhật của bạn nếu cần.
+0

Cảm ơn sự giúp đỡ của bạn, tôi sẽ cố gắng làm theo lời khuyên của bạn tốt nhất có thể! –

Các vấn đề liên quan