Java Awt & Swing



PongJava

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class Pong {
    private static final int WIDTH = 900, HEIGHT = 700;
    JFrame win = new JFrame();
    Paddle paddleOne = new Paddle(1);
    Paddle paddleTwo = new Paddle(2);
    Graphics g;
    Pong(){
        init();
    }
    void init(){
        win.setTitle("PONG");
        win.setSize(WIDTH, HEIGHT);
        win.addKeyListener(keyListener);
        win.setLocationRelativeTo(null);
        win.getContentPane().setBackground(Color.black);
        win.setVisible(true);
        win.getContentPane().validate();
        win.repaint();
    }

    public void paintComponent(Graphics g){
        g.setColor(Color.white);
        g.fillRect(paddleOne.getX(), paddleOne.getY(), paddleOne.getHEIGHT(), paddleOne.getWIDTH());
        System.out.println("drawn");
    }
    KeyListener keyListener = new KeyListener() {
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            switch(key){
            case 87:
                System.out.println("Player 1 Up");
                break;
            case 83:
                System.out.println("Player 1 Down");
                break;
            case 38:
                System.out.println("Player 2 Up");
                break;
            case 40:
                System.out.println("Player 2 Down");
                break;
            }
            win.getContentPane().validate();
            win.repaint();
        }
        public void keyReleased(KeyEvent arg0) {
            // TODO Auto-generated method stub
        }
        public void keyTyped(KeyEvent arg0) {
            // TODO Auto-generated method stub
        }
    };
    public static void main(String[] args) {
        Pong p = new Pong();
    }
}

Paddle Java

public class Paddle{
private int WIDTH = 50, HEIGHT = 150, X, Y;

int playerNum;
Paddle(int playerNum){
    if(playerNum == 1){
        X = 10;
        Y = 10;
    }else if (playerNum == 2){
        X = 500;
        Y = 10;
    }
}
public void setX(int x){
    X = x;
}
public void setY(int y){
    Y = y;
}
public int getWIDTH() {
    return WIDTH;
}
public int getHEIGHT() {
    return HEIGHT;
}
public int getX() {
    return X;
}
public int getY() {
    return Y;
}
}

我对Java编程比较陌生,或者更具体地说是Awt&Swing,我的问题是,为什么我的矩形画不出来?感谢您的帮助。非常感谢!

为了在Swing中绘制某些内容,它首先必须从Swing知道如何绘制的内容扩展,这通常意味着JComponent(或更典型的JPanel(。

然后,您可以覆盖绘制子系统调用的一种绘制方法,在这种情况下,通常首选覆盖paintComponent,但在进行任何自定义绘制之前,不要忘记调用super.paintComponent,否则您将面临一些奇怪且通常不可预测的绘制问题。

有关更多详细信息,请参阅AWT中的绘画和摆动和执行自定义绘画。

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Pongo {
    public static void main(String[] args) {
        new Pongo();
    }
    public Pongo() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }
                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new PongPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
    public class PongPane extends JPanel {
        private static final int WIDTH = 900, HEIGHT = 700;
        Paddle paddleOne = new Paddle(1);
        Paddle paddleTwo = new Paddle(2);
        public PongPane() {
            setBackground(Color.BLACK);
        }
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.white);
            g.fillRect(paddleOne.getX(), paddleOne.getY(), paddleOne.getHEIGHT(), paddleOne.getWIDTH());
            //System.out.println("drawn");   //Should not put something here which may overhead.
        }
    }
    public class Paddle {
        private int WIDTH = 50, HEIGHT = 150, X, Y;
        int playerNum;
        Paddle(int playerNum) {
            if (playerNum == 1) {
                X = 10;
                Y = 10;
            } else if (playerNum == 2) {
                X = 500;
                Y = 10;
            }
        }
        public void setX(int x) {
            X = x;
        }
        public void setY(int y) {
            Y = y;
        }
        public int getWIDTH() {
            return WIDTH;
        }
        public int getHEIGHT() {
            return HEIGHT;
        }
        public int getX() {
            return X;
        }
        public int getY() {
            return Y;
        }
    }
}

我还建议不要在这种情况下使用KeyListener,并建议使用密钥绑定API,因为它不会像KeyListener那样遇到与焦点相关的问题。有关更多详细信息,请参阅如何使用密钥绑定。

需要重写paintComponents才能绘制。这是您的Pong.java

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Pong {
    private static final int WIDTH = 900, HEIGHT = 700;
    JFrame win = new JFrame();
    Paddle paddleOne = new Paddle(1);
    Paddle paddleTwo = new Paddle(2);
    Graphics g;
    Pong() {
        init();
    }
    void init() {
        win.setTitle("PONG");
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        win.setSize(WIDTH, HEIGHT);
        win.addKeyListener(keyListener);
        win.setLocationRelativeTo(null);
        win.add(new Panel(paddleOne));
        win.getContentPane().setBackground(Color.black);
        win.setVisible(true);
        win.getContentPane().validate();
        win.repaint();
    }
    KeyListener keyListener = new KeyListener() {
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            switch (key) {
            case 87:
                System.out.println("Player 1 Up");
                break;
            case 83:
                System.out.println("Player 1 Down");
                break;
            case 38:
                System.out.println("Player 2 Up");
                break;
            case 40:
                System.out.println("Player 2 Down");
                break;
            }
            win.getContentPane().validate();
            win.repaint();
        }
        public void keyReleased(KeyEvent arg0) {
            // TODO Auto-generated method stub
        }
        public void keyTyped(KeyEvent arg0) {
            // TODO Auto-generated method stub
        }
    };
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Pong();
            }
        });
    }
}

此处为Panel.java,其中paintComponent被覆盖。

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Panel extends JPanel{
    private Paddle paddleOne;
    public Panel(Paddle pdl) {
        paddleOne = pdl;
    }
    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.white);
        g.fillRect(paddleOne.getX(), paddleOne.getY(), paddleOne.getHEIGHT(), paddleOne.getWIDTH());
        //System.out.println("drawn");   //Should not put something here which may overhead.
    }
} 

相关内容

  • 没有找到相关文章

最新更新