Welcome to Dream.In.Code
Become a Java Expert!

Join 149,533 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,528 people online right now. Registration is fast and FREE... Join Now!




Problem with repaint()

 
Reply to this topicStart new topic

Problem with repaint(), My call to repaint() is just ignored...help plz

anter
27 May, 2007 - 04:20 AM
Post #1

New D.I.C Head
*

Joined: 27 May, 2007
Posts: 11


My Contributions
Hi i was trying to make a 2D game. For this i am required to move a rectangle placed on a JFrame in the x-axis when the mouse is moved. My mouseMoved method call a user-defined moveTo method in another class, passing the x-coordinate as argument.
This moveTo function is responsible for changing the current location of the rectangle and invoking repaint().
But the problem is that the repaint method is just ignored i.e. nothing happens when it is called.
Bellow is the code of the class which is responsible for creating the rectangle object, defining paintComponent and moveTo methods, and calling repaint.
CODE


import java.awt.Rectangle;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;

public class MyBase extends JComponent
{
                private int xCord=0;
    private static final int YCORD=0;
    private static final int WIDTH=20;
    private static final int HEIGHT=30;
    
    private Rectangle box;
    
    public MyBase()
    {
    box = new Rectangle(xCord,YCORD,WIDTH,HEIGHT);
    }
    
    public void paintComponent(Graphics g)
    {
                 super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.draw(box);
                                System.out.println("paint");
    }
    
    public void moveTo(int x)
    {
        box.setLocation(x,YCORD);
        repaint();
        System.out.println(box);
    }
}


I am very new to java programming, so please help me out with this. This problem has been bugging me for quite a long. By the way i am using OpenSuSe 10.2 OS and JDK 1.6
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Problem With Repaint()
27 May, 2007 - 04:56 AM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi, is this all the code you have?
If it is then where is the main()?
you want to repaint() what? or should I say on what?
You need a window to paint something on.
If there is more code please post it.

User is offlineProfile CardPM
+Quote Post

anter
RE: Problem With Repaint()
27 May, 2007 - 09:54 AM
Post #3

New D.I.C Head
*

Joined: 27 May, 2007
Posts: 11


My Contributions
The code that i had posted before was of the class named MyBase. Object of this class is used by another class that i have, called MyFrame, it also uses an object of another class called MyEvents to keep in track with the mouse events. The MyFrame class has the main(). It contains a JFrame. Anyhow i think the code will explain more. So bellow is the two classes. The first is MyFrame and the second is MyEvents. The third class MyBase code is already posted above and it's filename will be MyBase.java.
CODE


//Filename Name: MyFrame.java

import javax.swing.JFrame;

import java.awt.event.MouseMotionListener;

public class MyFrame
{
    public static final int WIDTH=800;
    public static final int HEIGHT=600;
    
    public static void main(String args[])
    {
        JFrame frame = new JFrame();
        MyBase rect = new MyBase();
        
        frame.setSize(WIDTH, HEIGHT);
        frame.setTitle("Shooter 87");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.add(rect);
        MouseMotionListener listener = new MyEvents();
        frame.addMouseMotionListener(listener);
        
        frame.setVisible(true);
    }
} //MyFrame exits

//Filename Name: MyEvents.java

import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
//import java.awt.event.MouseListener;


public class MyEvents implements MouseMotionListener
{
    private MyBase rect=new MyBase();
    
    public void mouseMoved(MouseEvent event)
    {
        int x = event.getX();
        //System.out.println(x + " - " + y);
        rect.moveTo(x);
    }
    
    public void mouseDragged(MouseEvent event) {};
    
    /* MouseListener
    public void mouseEntered(MouseEvent event) {};
    public void mouseReleased(MouseEvent event) {};
    public void mouseClicked(MouseEvent event) {};
    public void mousePressed(MouseEvent event) {};
    public void mouseExited(MouseEvent event) {};
    */
}


Theoritically I think the program is correct. But the problem is when the moveTo function is call, the repaint() call inside moveTo is ignored.
Sort this for me please.

User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Problem With Repaint()
27 May, 2007 - 02:23 PM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
jkjhk

You should have declared a function
CODE
public void paint(Graphics g)
{

}

not paintComponent but paint
so that when you call repaint()
the program will "know" what to do.

This post has been edited by PennyBoki: 27 May, 2007 - 02:20 PM
User is offlineProfile CardPM
+Quote Post

anter
RE: Problem With Repaint()
28 May, 2007 - 07:47 AM
Post #5

New D.I.C Head
*

Joined: 27 May, 2007
Posts: 11


My Contributions
Mr. PennyBoki thanks for helping, and i did as u said. But still the program won't work. Could u plz try the code i posted and advice me on how to solve the problem.

User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Problem With Repaint()
28 May, 2007 - 09:04 AM
Post #6

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
I think you will be fine if you just make the method PennyBoki suggested as follows:
CODE

public void paint(Graphics g)
{
     super.paint(g);
}

calling super to paint, will automatically repaint all basic elements (frames, buttons, etc) with all changes.
User is offlineProfile CardPM
+Quote Post

anter
RE: Problem With Repaint()
28 May, 2007 - 10:02 AM
Post #7

New D.I.C Head
*

Joined: 27 May, 2007
Posts: 11


My Contributions
Mr. William I have tried your suggestion but still my arrogant code won't work. Anyhow thanks for suggesting.

Mr. William I have tried your suggestion but still my arrogant code won't work. Anyhow thanks for suggesting.
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Problem With Repaint()
28 May, 2007 - 10:43 AM
Post #8

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
is your other code:
CODE

Graphics2D g2 = (Graphics2D) g;
g2.draw(box);

inside this method as well?
User is offlineProfile CardPM
+Quote Post

anter
RE: Problem With Repaint()
28 May, 2007 - 10:59 AM
Post #9

New D.I.C Head
*

Joined: 27 May, 2007
Posts: 11


My Contributions
Yes it is there.
I don't understand what is the problem with the code. I have tried a lot but couldn't figure it out. Mr. William could plz copy the codes I have posted or download the attachment of the game files which i am posting now and try it out. The rectangle should move in the x-axis when the mouse is moved over the frame.

This post has been edited by anter: 28 May, 2007 - 11:13 AM


Attached File(s)
Attached File  MyGame.zip ( 1.3k ) Number of downloads: 26
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Problem With Repaint()
28 May, 2007 - 12:13 PM
Post #10

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
Your MYEvents class uses a different copy of your MyBase class than does your GUI, thus the box is updated, and painted, but it is not being painted anywhere viewable.

use a constructor instead
CODE

import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
//import java.awt.event.MouseListener;


public class MyEvents implements MouseMotionListener
{
    MyBase base;
    public MyEvents(MyBase b)
    {
        base = b;
    }
    public void mouseMoved(MouseEvent event)
    {
        int x = event.getX();
        //System.out.println(x);
        base.moveTo(x);
    }
    
    public void mouseDragged(MouseEvent event) {};
    
    /* MouseListener
    public void mouseEntered(MouseEvent event) {};
    public void mouseReleased(MouseEvent event) {};
    public void mouseClicked(MouseEvent event) {};
    public void mousePressed(MouseEvent event) {};
    public void mouseExited(MouseEvent event) {};
    */
}



and change: MouseMotionListener listener = new MyEvents();
to MouseMotionListener listener = new MyEvents(rect);

and it will work fine
User is offlineProfile CardPM
+Quote Post

anter
RE: Problem With Repaint()
28 May, 2007 - 12:33 PM
Post #11

New D.I.C Head
*

Joined: 27 May, 2007
Posts: 11


My Contributions
YES IT WORKS NOW. Thanks you very much Mr. William Wilson , for helping me out. I am very new to java programming, and this problem in my code bugged me for a very very long time. And I now I am so happy that it is finnaly solved. Thanks again man.
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Problem With Repaint()
28 May, 2007 - 12:35 PM
Post #12

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
always glad to help those who show an honest effort smile.gif
keep it up, you'll master java GUI eventually!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 09:02PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month