473,322 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

why isnt repaint() working?

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
0 1929

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

Similar topics

11
by: Ike | last post by:
I changing the size of frames dynamically in a page (I am restricted in that I cannot perform a reload of the content of any of the frames). In MSIE, when I resize the frames, thinks repaint...
2
by: Joe A | last post by:
I'm using Access 2002 on Windows XP PC, 500 megs ram, Front end/back end app. I have a simple form that draws a thermometer to indicate progress of code that is running. The thermometer form...
4
by: Bob | last post by:
Trying to repaint a form named fcon using a new filter: 'Repaint the form with the new data Forms!fcon.FilterOn = False Forms!fcon.Filter = "RecNo = " & currecno & " AND Currentvalue = -1"...
4
by: CroDude | last post by:
I've made a custom groupbox control. Inside, as one of it's members is CSimpleGradient object. CSimpleGradient is a wrapper class for gradient usage. Basically it looks like that: public class...
0
by: rodchar | last post by:
Hey all, I was wondering if there is a repaint counterpart in asp.net. I have a literal that i want to show before going into a procedure but it's not working. How do I refresh the screen before...
4
by: MLH | last post by:
Setting form's Picture property to another file from within VBA doesn't repaint the form with new bitmap - for instance... Me.Picture = "c:\pics\MyNewPic.jpg" I tried Me.Repaint afterward,...
2
by: aaron.kempf | last post by:
hey guys i have an ETL winform in VB 2005 and it's like-- off in la-la land. i know it's doing things-- it's happily crunching away. but i'm confused about what i need to do to make the main...
7
by: Paul | last post by:
I have a VB.NET form with a DataGrid. When I toggle to Excel (for example) and then back to my application the repaint of the DataGrid is really slow. You can see the repainting happening. When...
4
by: drexlin | last post by:
Hi, I hope this is the correct place to post questions about visual c++ 6.0. If not, sorry. Anyway, the program I am working on takes messages from another computer and prints them on the screen....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.