使用paint绘制JPanel(Graphics gr)

时间:2013-05-28 10:36:19

标签: java swing graphics jpanel paint

我试图在JFrame上的JPanel上绘制一个简单的矩形,但它没有用。有人可以给我一些建议吗?如果有人可以,我会很高兴...我真的很努力,但我无法弄清楚如何做到这一点。这段代码是我的“Snake”程序的一部分。所有其他工作,除此之外。这是我的代码:

package initialising_Package;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Initialising extends JPanel implements WindowListener,
        ItemListener, ChangeListener, Runnable {

    /**
     * @param args
     */
    private String name;
    private JFrame frame;
    private boolean fall = false;
    private JSlider slider;
    private JLabel lbl1, lbl2, lbl3, lbl4;
    private JComboBox box1, box2, box3;
    private String[] positionsx, positionsy, laenge;
    private JPanel pnl1, pnl2, pnl3, pnlpnl;
    private JButton btn;
    private int beginningLenght, posX, posY;
    private boolean start = false;

    public Initialising() {
        initStartFrame("Initialising");
    }

    public void initStartFrame(String name) {
        frame = new JFrame(name);
        frame.setLocation(300, 300);
        frame.setSize(500, 350);
        frame.setUndecorated(false);
        frame.getContentPane().setLayout(new GridLayout(5, 1));
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.setVisible(true);
        frame.addWindowListener(this);

        // GUI
        slider = new JSlider(1, 100, 50);
        slider.addChangeListener(this);
        slider.setPaintTicks(false);
        slider.setPaintTrack(true);

        positionsx = new String[50];
        positionsy = new String[50];
        for (int i = 0, j = 0; i < 50; i++, j = j + 10) {
            positionsx[i] = new String("" + j);
            positionsy[i] = new String("" + j);
        }

        laenge = new String[45];
        for (int i = 0; i < 45; i++) {
            laenge[i] = "" + (i + 1);
        }

        box1 = new JComboBox(positionsx);
        box1.addItemListener(this);
        box2 = new JComboBox(positionsy);
        box2.addItemListener(this);
        box3 = new JComboBox(laenge);
        box3.addItemListener(this);

        lbl1 = new JLabel("Geschwindigkeit: ");
        lbl2 = new JLabel("Startlaenge: ");
        lbl3 = new JLabel("Position: ");
        pnlpnl = new JPanel();
        pnlpnl.setPreferredSize(new Dimension(95, 490));
        pnlpnl.setBackground(Color.BLACK);

        pnl1 = new JPanel();
        pnl1.setLayout(new GridLayout(1, 2));
        pnl2 = new JPanel();
        pnl2.setLayout(new GridLayout(1, 2));
        pnl3 = new JPanel();
        pnl3.setLayout(new GridLayout(1, 3));

        btn = new JButton("Snake!");
        btn.setPreferredSize(new Dimension(80, 490));

        pnl1.add(lbl1);
        pnl1.add(slider);
        pnl2.add(lbl2);
        pnl2.add(box3);
        pnl3.add(lbl3);
        pnl3.add(box1);
        pnl3.add(box2);

        frame.add(pnlpnl);
        frame.add(pnl1);
        frame.add(pnl2);
        frame.add(pnl3);
        frame.add(btn);

        // GUI

        frame.validate();
        frame.repaint();

    }

    public int getSpeed() {
        return slider.getValue();
    }

    public int getBeginningLenght() {
        return beginningLenght;
    }

    public int getPosX() {
        return posX;
    }

    public int getPosY() {
        return posY;
    }

    @Override
    public void windowActivated(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void windowClosed(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void windowClosing(WindowEvent arg0) {
        int z = JOptionPane.showConfirmDialog(frame,
                "Willst du wirklich benden?", "Warnung", 0);
        if (z == JOptionPane.YES_OPTION) {
            System.exit(0);
        }

    }

    @Override
    public void windowDeactivated(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void windowDeiconified(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void windowIconified(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void windowOpened(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Initialising init = new Initialising();
        new Thread(init).start();

    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        // TODO Auto-generated method stub
        if (e.getSource().equals(box1)) {
            posX = Integer.parseInt((String) box1.getSelectedItem());
            System.out.println("PosX: " + posX);

        } else if (e.getSource().equals(box2)) {
            posY = Integer.parseInt((String) box2.getSelectedItem());
            System.out.println("PosY: " + posY);

        } else if (e.getSource().equals(box3)) {
            beginningLenght = Integer.parseInt((String) box3.getSelectedItem());
            System.out.println("Beginning Lenght: " + beginningLenght);

            System.out.println("H: " + pnlpnl.getHeight() + " B: "
                    + pnlpnl.getWidth());

        }

    }

    @Override
    public void stateChanged(ChangeEvent e) {
        // TODO Auto-generated method stub
        if (e.getSource().equals(slider)) {
            System.out.println("Speed: " + slider.getValue());
        }

    }

    @Override
    public void paintComponent(Graphics gr) {
        super.paint(gr);
        gr.setColor(Color.CYAN);
        gr.fillRect(0, 0, 200, 200);
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        while (true) {
            System.out.println("It works!!");
            try {
                Thread.sleep(800);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if (!start) {

            }
            repaint();
            validate();
        }

    }

}

1 个答案:

答案 0 :(得分:0)

从JFrame中删除paintComponent。这也称为super.paint i.o. super.paintComponent方法。

    pnlpnl = new JPanel() {

        @Override
        public void paintComponent(Graphics gr) {
            super.paintComponent(gr);
            gr.setColor(Color.CYAN);
            gr.fillRect(0, 0, 200, 200);
        }
    };

在此paintComponent中,您可以使用JFrame的字段。

在构造函数中调用initStartFrame时,它不应该是可覆盖的(然后将调用子类的版本,而不会完全初始化子类)。

//Sometimes: public final void initStartFrame(String name) {
private void initStartFrame(String name) {

按原样启动框架:

    // GUI

    frame.validate();
    //frame.repaint();

    frame.pack();
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            frame.setVisible(true);
        }
    });