2014-11-06 48 views
-2

我試圖製作一個程序,可以讓您選擇想要的圖像數量,然後它會詢問您要使用的文件的名稱。然後它將以適當的方式將它們打印在屏幕上。將一些圖像繪製成一個窗格

現在我知道這個代碼的很多部分,如寬度和高度,行和列,以及其他的東西都是錯誤的,我打算在我弄清楚之後修復它們。不管我如何改變它,它都不讓我使用paintComponent。我想保留在我的主課堂中,這仍然有可能嗎?很多時候我把它放在一個單獨的圖形類中,但是把我的輸入引入到這個類中是令人惱怒的。

import java.awt.Color; 
import java.awt.Container; 
import java.awt.Graphics; 
import java.awt.GridLayout; 
import java.awt.Image; 
import java.util.ArrayList; 
import java.util.Scanner; 

import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
public class Core extends JPanel{ 
    public static void main(String[] args){ 
     Scanner r = new Scanner(System.in); 
     System.out.println("How many selections will you have? 1, 2 or 4? "); 
     if(r.next().contains("1") | r.next().contains("2") | r.next().contains("4")){ 
     String selections = r.next(); 
     int number = Integer.parseInt(selections); 
     ArrayList<String> images = new ArrayList(); 
     for(int j = 1; j <= number; j++){ 
      System.out.println("Your options are boo, bae, skinny, bro..."); 
      System.out.println("Name of image " + j + "? "); 
      if(r.next().contains("boo") | r.next().contains("bae") | r.next().contains("skinny") | r.next().contains("bro")) 
      images.add(r.next()); 
      else{ 
       System.out.println("Im sorry that was an improper input..."); 
       System.out.println("Next time, remember, you can only input boo, bae, skinny, or bro."); 
       r.close(); 
      } 
     } 

     JFrame theGUI = new JFrame(); 
     theGUI.setTitle("Random Images"); 
     theGUI.setSize(number * 100, number * 100);//ratio of 1:100 
     theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     Container pane = theGUI.getContentPane(); 

     pane.setLayout(new GridLayout(number, number)); 
     for(int j = 1; j <= number; j++){ 
      Color backColor = Color.white; 
      Image image = new ImageIcon((images.get(j)) + ".jpg").getImage(); 
      public paintComponent(Graphics g){//error here(paintComponent cannot be resolved to a variable)(Illegal modifier for 'g') 
       super.paintComponent(Graphics g);//and error here (cannot use super)(Graphics cannot be resokved to a variable) 

      g.drawImage(image, 0, 0, 100, 100, null);} 
      ColorPanel panelz = new ColorPanel(backColor); 
      pane.add(panelz); 
     } 


     theGUI.setVisible(true); 
     }//end of the checker for the 1, 2 and 4. 

     else { 
      System.out.println("I'm sorry that was an improper input."); 
      System.out.println("Keep in mind your inputs may only be 1, 2 or 4."); 
     } 
    } 

} 

在評論中張貼代碼的道歉,我是新來的。但是當我喜歡你說的時候,我爲變量聲明瞭什麼ID?

public class Core extends JPanel{ 

    public static Image image; 

    public void paintComponent(g){//insert VariableDeclaratorID error 
     super.paintComponent(g); 

    g.drawImage(image, 0, 0, 100, 100, null); 
    } 

    public static void main(String[] args){ 
+1

您正在聲明主方法內部的方法,它不是合適的語法。聲明它在你的主要方法之外,但仍然在你的班級 – 2014-11-06 01:09:07

+0

@VinceEmigh好吧lemme通過真正的快速運行,並嘗試... – Kotite 2014-11-06 01:10:52

+3

事實上,你已經得到**所有內部的主要方法,並沒有任何類適當。我建議1)閱讀Swing圖形教程(或重新閱讀它們),閱讀OOP和Java,然後從頭開始重新嘗試這個東西。理解主要的方法應該是**非常短,並且應該讓程序運行,就是這樣。 – 2014-11-06 01:12:38

回答

5
  1. 你試圖覆蓋方法中的一個方法。這不僅在Java中是非法的,它不是如何在Swing中完成繪畫。
  2. 你沒有提供paintComponent方法的返回類型
  3. 當調用一個方法,你並不需要聲明的參數類型super.paintComponent(Graphics g);,只是傳遞正確類型的對象實例吧,super.paintComponent(g);
  4. 請勿將基於控制檯的輸入與GUI輸入混合使用。 GUI運行在事件驅動的環境中,這使得它們非線性,並且在UI運行時很難在命令行收集用戶的信息。

你再往前走之前,先來看看:

拿這個......

public paintComponent(Graphics g){ error here(paintComponent cannot be resolved to a variable)(Illegal modifier for 'g') 
    super.paintComponent(Graphics g); and error here (cannot use super)(Graphics cannot be resokved to a variable) 

g.drawImage(image, 0, 0, 100, 100, null);} 

從你for-loop,並讓它成爲你Core類的方法...

@Override 
public void paintComponent(Graphics g) { 
    // ^--- Need this... 
    super.paintComponent(g); 
         // ^--- Don't need Graphics, just an instance of it... 
} 

main不是一個類,它是一個方法,一個方法Core;)

例如...

public class Core extends JPanel{ 
    public static void main(String[] args){ 
     //.. 
     pane.setLayout(new GridLayout(number, number)); 
     for(int j = 1; j <= number; j++){ 
      Color backColor = Color.white; 
      Image image = new ImageIcon((images.get(j)) + ".jpg").getImage(); 
      // This be bad... 
      //public paintComponent(Graphics g){//error here(paintComponent cannot be resolved to a variable)(Illegal modifier for 'g') 
      // super.paintComponent(Graphics g);//and error here (cannot use super)(Graphics cannot be resokved to a variable) 
      // 
      //g.drawImage(image, 0, 0, 100, 100, null);} 
      ColorPanel panelz = new ColorPanel(backColor); 
      pane.add(panelz); 
     } 


     theGUI.setVisible(true); 
     //... 
    } 

    // This be better... 
    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     // Draw the List of images here how ever you want... 
    } 

} 

更新...

好吧,如果我理解你的意見,你想要做的是創造的JPanel一個實例,但提供了一個自定義實現什麼是paintComponent方法爲每個圖像...

for(int j = 1; j <= number; j++){ 
    Color backColor = Color.white; 
    final Image image = new ImageIcon((images.get(j)) + ".jpg").getImage(); 
// ^--- This is important 
    panel.add(new JPanel() { 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(image.getWidth(), image.getHeight()); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      g.drawImage(image, 0, 0, this); 
     } 
    }); 
} 
+0

哦,好的,是的,我得到你在說什麼,這是我需要這些圖像被繪製1圖像1面板,然後打印在窗格中。由於該程序依賴於你想要的圖像和數量。那仍然可以在主體之外使用這種方法嗎? (我正在看你的括號,以確定主要方法的結束位置。) – Kotite 2014-11-06 01:49:34

+0

啊好吧,你的要求是不同於你的代碼示例,掛在... – MadProgrammer 2014-11-06 02:02:50

+1

Okey Dokey。我敢肯定我的代碼是混亂和XD搞砸了。正如我所說的寬度列等很多不正確的,它的東西混亂。基本上它需要輸入的圖像數量:1,2或4.並且可以讓你選擇你想要的圖像。然後按照1x1 2x2或1x2的順序繪製它們。 – Kotite 2014-11-06 02:05:06

相關問題