473,545 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why isnt repaint() working?

1 New Member
<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 MouseEventHandl er 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 1945

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

Similar topics

11
5784
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 perfectly. In NS 7 there are artifacts left over from what previously occupied a given frame. If I minimize the NS browser, then mazimize it again, the...
2
5126
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 sometimes stops advancing (does not repaint) part way thru it's cycle. If you click the MS Access window title bar during the cycle, the Access title...
4
1896
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" Forms!fcon.FilterOn = True Forms!fcon.Requery Stop
4
2560
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 CSimpleGradient { private float m_GradientAngle; private Color m_ColorA; private Color m_ColorB;
0
907
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 entering the procedure? thanks in advance, rodchar
4
4447
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, to no avail. Ideas? Suggestions?
2
10133
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 form refresh... or repaint.. it's like.. it just looks hung (sorta like me lol jk) and i just want something that LOOKS AND ACTS like a real...
7
4985
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 I toggle back to Excel, it does not do that. The repaint is quick. I'm running this within a Citrix environment. The scrolling of the DataGrid is...
4
3031
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. So, whenever a new message comes in, all the previous ones move up and the new one gets added to the bottom. Now, if the operator is scrolling up to...
0
7401
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7807
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7419
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
3450
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3442
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1879
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
703
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.