绘图&更改JPanel中的图像(来自另一个类)

时间:2015-10-18 16:33:29

标签: java image swing graphics jpanel

所以是的,我正在尝试用图像编写一个石头剪刀程序,但我在插入图片方面遇到了麻烦。 JPanel上似乎只有一个小的白色方块。

以下是我的主类的代码:

public class Practice extends JPanel implements ActionListener {

// Images
final ImageIcon rockPic = new ImageIcon("D:\\JOVO\\RPS\\src\\rock.png");
final ImageIcon paperPic = new ImageIcon("D:\\JOVO\\RPS\\src\\paper.png");

// variables
String results = null;
Image pic = null;

//Image Panel
ImagePanel youPic = new ImagePanel();

public Practice() {

    // FRAME
    JFrame frame = new JFrame();
    frame.setSize(400, 400);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("RPS");
    frame.setLayout(new BorderLayout());
    frame.setVisible(true);

    // SUPER JPANEL
    setLayout(new GridBagLayout());
    setBackground(Color.WHITE);
    GridBagConstraints c = new GridBagConstraints();
    frame.add(this);

    // JPANELS - RPS title, JButton, PICTURES
    JPanel rpsTitle = new JPanel();
    c.fill = GridBagConstraints.CENTER;
    c.gridy = 0;
    c.gridx = 1;
    rpsTitle.setBackground(Color.WHITE);
    add(rpsTitle, c);

    JPanel jbuts = new JPanel();
    c.gridy = 3;
    jbuts.setLayout(new BorderLayout());
    add(jbuts, c);

    // ImagePanels
    c.gridy = 2;
    c.gridx = 0;
    add(youPic,c);

    c.gridy = 0;
    c.gridx = 1;

    // JBUTTONS
    JButton rock = new JButton("  Rock  ");
    JButton paper = new JButton("  Paper  ");
    JButton scissors = new JButton("Scissors");
    jbuts.add(rock, BorderLayout.WEST);
    jbuts.add(paper, BorderLayout.CENTER);
    jbuts.add(scissors, BorderLayout.EAST);
    paper.addActionListener(this);

    JButton resultB = new JButton("RES");
    c.gridy = 2;
    c.weighty = 10;
    add(resultB, c);
    c.weighty = 0;

    // FONTS
    Font rps = new Font(null, Font.BOLD, 28);
    Font labels = new Font(null, Font.ITALIC, 18);

    // JLABELS
    JLabel rpsTit = new JLabel("Rock, Paper, Scissors");
    rpsTit.setFont(rps);
    rpsTitle.add(rpsTit);

    JLabel you = new JLabel("You: ");
    you.setFont(labels);
    JLabel com = new JLabel("Com: ");
    com.setFont(labels);

    c.gridy = 1;
    c.gridx = 0;
    add(you, c);

    c.gridx = 2;
    add(com, c);

    // PICTURES
    // comPic.setPic(rockPic);
    // youPic.setPic(pic);

}

public String test(String name) {
    // test the game

    return results;

}

public static void main(String[] args) {
    // main stuffs
    Practice prac = new Practice();

}

@Override
public void actionPerformed(ActionEvent e) {
    System.out.println("Works");
    ImageIcon pic = null;
    pic = paperPic;
    youPic.setPic(pic);

    }
}

我的另一堂课:

public class ImagePanel extends JPanel {

Image pic = null;

public ImagePanel() {
    repaint();
}

public void setPic(ImageIcon pic) {
    // set picture for painting
    this.pic = pic.getImage();
    System.out.println("Set Pic");
    repaint();
}

public void paintComponent(Graphics g) {
    // paint
    super.paintComponent(g);
    System.out.println("Painting");
    g.drawImage(pic, 0, 0, this);
    }

}

提前致谢! :)

0 个答案:

没有答案