将一些图像绘制成一个窗格

时间:2014-11-06 00:59:35

标签: java swing user-interface graphics

我试图制作一个可以让你选择所需图像数量的程序,然后会询问你想要使用的文件的名称。然后它将以适当的方式在屏幕上以网格打印它们。

现在我知道这段代码的很多部分,比如宽度和高度,行和列,以及其他的东西都是错的,我打算在我弄清楚这一点之后修复它们。不管我如何改变它,它都不会让我使用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 个答案:

答案 0 :(得分:5)

  1. 您正在尝试覆盖方法中的方法。这不仅在Java中是非法的,也不是在Swing中完成绘画的方式。
  2. 您尚未提供paintComponent方法的返回类型
  3. 调用方法时,您不需要声明参数类型super.paintComponent(Graphics g);,只需将正确类型的对象的实例传递给它,super.paintComponent(g);
  4. 不要将基于控制台的输入与GUI输入混合使用。 GUI在事件驱动的环境中运行,这使得它们本质上是非线性的,并且在UI运行时很难从命令行收集用户的信息。
  5. 在继续之前,请先看看:

    拿这个......

    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);
            }
        });
    }