By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,723 Members | 1,147 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,723 IT Pros & Developers. It's quick & easy.

why isnt repaint() working?

P: 1
<I have code that uses 2 images>

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
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. public class Invasion01 {
  5.     static String buttonInt ="0";
  6.     static String area = "titlescreen";
  7.     static ImageIcon self = new ImageIcon("self.jpg");
  8.     static ImageIcon title = new ImageIcon("title.jpg");
  9.     static int mousePosX, mousePosY;
  10.     static JFrame main = new JFrame("Invasion");
  11.     static Ship you = new Ship(10, 10, 1, 0);
  12.     static Container pane = main.getContentPane();
  13.     static NewPanel panel1 = new NewPanel();
  14.     public static void main(String[] args) {
  15.         main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16.         main.addMouseListener(new MouseEventHandler());
  17.         main.addMouseMotionListener(new MouseMotionEventHandler());
  18.         main.setExtendedState(JFrame.MAXIMIZED_BOTH);
  19.         ButtonHandler handler = new ButtonHandler();
  20.         addComponentsToPane(pane, handler);
  21.         main.repaint();
  22.         main.setSize(500, 500);
  23.         main.setBackground(Color.black);
  24.         main.setVisible(true);
  25.  
  26.     }
  27.     public static void addComponentsToPane(Container pane, ButtonHandler handler) {
  28.         pane.setBackground(Color.black);
  29.         pane.setLayout(new BorderLayout(0,20));
  30.         JLabel titleLabel = new JLabel(title, SwingConstants.CENTER);
  31.         JPanel quitPanel = new JPanel();
  32.         quitPanel.setBackground(Color.black);
  33.         quitPanel.setLayout(new BoxLayout(quitPanel, BoxLayout.Y_AXIS));
  34.         quitPanel.add(titleLabel);
  35.         JButton play = new JButton("Play");
  36.         play.addActionListener(handler);
  37.         quitPanel.add(play);
  38.         pane.add(quitPanel);
  39.         JButton quit = new JButton("Quit");
  40.         quit.addActionListener(handler);
  41.         quitPanel.add(quit);
  42.         JPanel holderPanel = new JPanel();
  43.         holderPanel.add(quitPanel);
  44.         pane.add(holderPanel);
  45.         holderPanel.setBackground(Color.black);
  46.     }
  47.  
  48.     public static void startGame() {
  49.         main.getContentPane().removeAll();
  50.         main.repaint();
  51.         main.addMouseListener(new MouseEventHandler());
  52.         main.addMouseMotionListener(new MouseMotionEventHandler());
  53.         main.getContentPane().add(panel1, BorderLayout.CENTER);
  54.         main.repaint();
  55.     }
  56.  
  57. }
  58. class Ship {
  59.     public int health, maxHealth, shot, money, x ,y;
  60.     public Ship (int health, int maxHealth, int shot, int money) {
  61.         this.health = health;
  62.         this.maxHealth = maxHealth;
  63.         this.shot = shot;
  64.         this.money = money;
  65.     }
  66.     public int getX() {
  67.         return x;
  68.     }
  69.     public int getY() {
  70.         return y;
  71.     }
  72.     public void setXY(int x, int y) {
  73.         this.x = x;
  74.         this.y = y;
  75.     }
  76. }
  77. class EnemyShip {
  78.     public static int moveStage;
  79.     public int health, maxHealth, shot, probShot, x, y;
  80.     public EnemyShip (int health, int maxHealth, int shot, int probShot, int x, int y) {
  81.         this.health = health;
  82.         this.maxHealth = maxHealth;
  83.         this.shot = shot;
  84.         this.probShot = probShot;
  85.         this.x = x;
  86.         this.y = y;
  87.     }
  88. }
  89. class MouseEventHandler implements MouseListener {
  90.     public void mouseClicked(MouseEvent e) {
  91.     }
  92.     public void mouseEntered(MouseEvent e) {    
  93.     }
  94.     public void mouseExited(MouseEvent e) {    
  95.     }
  96.     public void mousePressed(MouseEvent e) {    
  97.     }
  98.     public void mouseReleased(MouseEvent e) {    
  99.     }
  100. }
  101. class MouseMotionEventHandler implements MouseMotionListener {
  102.     public void mouseMoved(MouseEvent e) {
  103.         Invasion01.mousePosX = e.getX()-33;
  104.         Invasion01.mousePosY = e.getY()-57;
  105.         Invasion01.you.setXY(Invasion01.mousePosX,Invasion01.mousePosY);
  106.         Invasion01.main.repaint();
  107.         Invasion01.panel1.repaint();
  108.     }
  109.     public void mouseDragged(MouseEvent e) {
  110.         Invasion01.mousePosX = e.getX()-33;
  111.         Invasion01.mousePosY = e.getY()-57;
  112.         Invasion01.you.setXY(Invasion01.mousePosX,Invasion01.mousePosY);
  113.         Invasion01.main.repaint();
  114.         Invasion01.panel1.repaint();
  115.     }
  116. }
  117. class ButtonHandler implements ActionListener {
  118.     public void actionPerformed(ActionEvent event) {
  119.         if(event.getActionCommand() == "Quit") {
  120.             System.exit(0);
  121.         }
  122.         else if (event.getActionCommand() == "Play") {
  123.             Invasion01.startGame();
  124.         }
  125.     }
  126. }
  127. class NewPanel extends JPanel {
  128.     public void update(Graphics g) { 
  129.         paintComponent(g);
  130.     }
  131.     public void paintComponent(Graphics g){
  132.         Invasion01.self.paintIcon(this, g, Invasion01.you.getX(), Invasion01.you.getY());
  133.     }
  134. }
  135.  
Mar 8 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.