the program creates a window and then when u press play, it removes all components in the container. then, i add a NewPanel object. i add mouse listeners and all in all, you should see an image following your mouse.
BUT. its only seen after you press play and then resize the window. this leads me to conclude that theres a problem with repaint().
can someone please help?
thanks.
heres my code: (EnemyShip and MouseEventHandler dont do anything yet)
Expand|Select|Wrap|Line Numbers
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class Invasion01 {
- static String buttonInt ="0";
- static String area = "titlescreen";
- static ImageIcon self = new ImageIcon("self.jpg");
- static ImageIcon title = new ImageIcon("title.jpg");
- static int mousePosX, mousePosY;
- static JFrame main = new JFrame("Invasion");
- static Ship you = new Ship(10, 10, 1, 0);
- static Container pane = main.getContentPane();
- static NewPanel panel1 = new NewPanel();
- public static void main(String[] args) {
- main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- main.addMouseListener(new MouseEventHandler());
- main.addMouseMotionListener(new MouseMotionEventHandler());
- main.setExtendedState(JFrame.MAXIMIZED_BOTH);
- ButtonHandler handler = new ButtonHandler();
- addComponentsToPane(pane, handler);
- main.repaint();
- main.setSize(500, 500);
- main.setBackground(Color.black);
- main.setVisible(true);
- }
- public static void addComponentsToPane(Container pane, ButtonHandler handler) {
- pane.setBackground(Color.black);
- pane.setLayout(new BorderLayout(0,20));
- JLabel titleLabel = new JLabel(title, SwingConstants.CENTER);
- JPanel quitPanel = new JPanel();
- quitPanel.setBackground(Color.black);
- quitPanel.setLayout(new BoxLayout(quitPanel, BoxLayout.Y_AXIS));
- quitPanel.add(titleLabel);
- JButton play = new JButton("Play");
- play.addActionListener(handler);
- quitPanel.add(play);
- pane.add(quitPanel);
- JButton quit = new JButton("Quit");
- quit.addActionListener(handler);
- quitPanel.add(quit);
- JPanel holderPanel = new JPanel();
- holderPanel.add(quitPanel);
- pane.add(holderPanel);
- holderPanel.setBackground(Color.black);
- }
- public static void startGame() {
- main.getContentPane().removeAll();
- main.repaint();
- main.addMouseListener(new MouseEventHandler());
- main.addMouseMotionListener(new MouseMotionEventHandler());
- main.getContentPane().add(panel1, BorderLayout.CENTER);
- main.repaint();
- }
- }
- class Ship {
- public int health, maxHealth, shot, money, x ,y;
- public Ship (int health, int maxHealth, int shot, int money) {
- this.health = health;
- this.maxHealth = maxHealth;
- this.shot = shot;
- this.money = money;
- }
- public int getX() {
- return x;
- }
- public int getY() {
- return y;
- }
- public void setXY(int x, int y) {
- this.x = x;
- this.y = y;
- }
- }
- class EnemyShip {
- public static int moveStage;
- public int health, maxHealth, shot, probShot, x, y;
- public EnemyShip (int health, int maxHealth, int shot, int probShot, int x, int y) {
- this.health = health;
- this.maxHealth = maxHealth;
- this.shot = shot;
- this.probShot = probShot;
- this.x = x;
- this.y = y;
- }
- }
- class MouseEventHandler implements MouseListener {
- public void mouseClicked(MouseEvent e) {
- }
- public void mouseEntered(MouseEvent e) {
- }
- public void mouseExited(MouseEvent e) {
- }
- public void mousePressed(MouseEvent e) {
- }
- public void mouseReleased(MouseEvent e) {
- }
- }
- class MouseMotionEventHandler implements MouseMotionListener {
- public void mouseMoved(MouseEvent e) {
- Invasion01.mousePosX = e.getX()-33;
- Invasion01.mousePosY = e.getY()-57;
- Invasion01.you.setXY(Invasion01.mousePosX,Invasion01.mousePosY);
- Invasion01.main.repaint();
- Invasion01.panel1.repaint();
- }
- public void mouseDragged(MouseEvent e) {
- Invasion01.mousePosX = e.getX()-33;
- Invasion01.mousePosY = e.getY()-57;
- Invasion01.you.setXY(Invasion01.mousePosX,Invasion01.mousePosY);
- Invasion01.main.repaint();
- Invasion01.panel1.repaint();
- }
- }
- class ButtonHandler implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- if(event.getActionCommand() == "Quit") {
- System.exit(0);
- }
- else if (event.getActionCommand() == "Play") {
- Invasion01.startGame();
- }
- }
- }
- class NewPanel extends JPanel {
- public void update(Graphics g) {
- paintComponent(g);
- }
- public void paintComponent(Graphics g){
- Invasion01.self.paintIcon(this, g, Invasion01.you.getX(), Invasion01.you.getY());
- }
- }