图形不动

时间:2013-10-24 11:57:57

标签: java swing graphics jpanel jbutton

我正在尝试制作一个可以用JButtons移动红球的代码(后来添加了键控器)。编译时没有问题,当我跑步时我看到球但JButtons不会影响他。我认为问题可能是球只被抽出一次,然后被一次又一次地调用,而不是在新位置被抽出,但我不知道如何解决这个问题。 1)有人知道如何解决这个问题吗? 2)有没有办法将JPanel的形状改为球? (这可能是一种更简单的移动方式)

package il.co.atlantis;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.*;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class KeyBinders implements ActionListener {

boolean right=true, left=false, up=false, down=false, inGame=true;
JPanel backgroundPanel, bannerPanel, scorePanel, applePanel;
JLabel currentScoreLabel, highestScoreLabel;
JButton upButton, downButton, rightButton, leftButton;
long millis =System.currentTimeMillis(), millisn =System.currentTimeMillis();
public static final int WID = 10, HEI = 10;
public static int x1 = 100, y1 = 100;
public class MyGraphics extends JComponent {

    private static final long serialVersionUID = 1L;

    MyGraphics() {
        setPreferredSize(new Dimension(700, 500));
    }

    public void moveRight(){
    ++x1;
    }

    public void moveLeft(){
    --x1;
    }

    public void moveUp(){
    --y1;
    }

    public void moveDown(){
    ++y1;
    }

    public void paintComponent(Graphics g){
        super.paintComponents(g);
        g.setColor(Color.red);
        g.fillOval(x1, y1, WID, HEI);
    }

}


public JPanel CreateContentPane (){
    JPanel totalGUI = new JPanel();
    totalGUI.setLayout(null);

    backgroundPanel = new JPanel();
    backgroundPanel.setBackground(Color.black);
    backgroundPanel.setLocation(100, 10);
    backgroundPanel.setSize(700, 500);
    totalGUI.add(backgroundPanel);

    upButton = new JButton("up");
    upButton.setLocation(0,0);
    upButton.setSize(50,50);
    totalGUI.add(upButton);

    downButton = new JButton ("down");
    downButton.setLocation(0,50);
    downButton.setSize(50,50);
    totalGUI.add(downButton);

    rightButton = new JButton("right");
    rightButton.setLocation(0,100);
    rightButton.setSize(50,50);
    totalGUI.add(rightButton);

    leftButton = new JButton("left");
    leftButton.setLocation(0,150);
    leftButton.setSize(50,50);
    totalGUI.add(leftButton);


    MyGraphics tr = new MyGraphics();
    tr.setLocation(100, 100);
    backgroundPanel.add(tr);


    return totalGUI;
}

public void ActionPerformed(ActionEvent h){
    if(h.getSource() == upButton) {
        --y1;
    }
    else if(h.getSource() == downButton){
        ++y1;
    }
    else if(h.getSource() == leftButton){
        --x1;
    }
    else if(h.getSource() == rightButton){
        ++x1;
    }
}

private static void createAndShowGUI() {

    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("[=] JButton Scores! [=]");

    //Create and set up the content pane.
    KeyBinders demo = new KeyBinders();
    frame.setContentPane(demo.CreateContentPane());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(280, 190);
    frame.setVisible(true);
}

public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            createAndShowGUI();

        }
    });
}

public KeyBinders() {
    // TODO Auto-generated constructor stub
}

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

}


}

2 个答案:

答案 0 :(得分:2)

当你调用动作事件时,会像你一样调用actionPerformed()函数。您也改变了绘图位置。你需要调用Component.repaint()告诉Swing整个组件,无论你指定哪一个重绘,都必须更新。所以在你的代码中添加这个函数调用。例如:

public void ActionPerformed(ActionEvent h){
    if(h.getSource() == upButton) {
        --y1;
    }
    else if(h.getSource() == downButton){
        ++y1;
    }
    else if(h.getSource() == leftButton){
        --x1;
    }
    else if(h.getSource() == rightButton){
        ++x1;
    }
   repaint();
}

查看教程:Performing Custom Painting.

答案 1 :(得分:1)

您应该熟悉一种名为repaint()的方法。

在组件(例如JFrame)上调用时,它将重新绘制其中的所有组件。当然,如果您希望更改在屏幕上显示,则需要调用它。

对于自定义绘画,你根本不应该使用Component,而是使用Graphics.fillRect / fillOval等方法来绘制你想要的东西。

有关自定义绘画教程,请参阅here