开发者

why is it only painting one of the components?

package puzzle;

import java.awt.Color;
import javax.swing.*;
import java.util.*;


public class Puzzle {

Integer x = 50;
Integer y = 50;
int x2 = 100;
int y2 = 100;
int vnotx = 0;
int vnoty = 0;
float t = 0;
float t2  = 0; 
JFrame frame = new JFrame();
Move m = new Move(x, y,Color.GREEN);
Move n = new Move(x,y,Color.ORANGE);
java.util.Timer timer = new java.util.Timer();
java.util.Timer timer2 = new java.util.Timer();

public Puzzle() {

    frame.setUndecorated(true);
    frame.setSize(400, 400);
    frame.add(m);
    frame.add(n);
    frame.addKeyListener(new java.awt.event.KeyAdapter() {

        public void keyPressed(java.awt.event.KeyEvent evt) {
            formKeyPressed(evt);
        }
    });

    com.sun.awt.AWTUtilities.setWindowOpacity(frame, 1f);
    timer.scheduleAtFixedRate(new TimerTask() {

        public void run() {
            rD();
        }
    }, 0, 20);

       timer2.scheduleAtFixedRate(new TimerTask() {

        public void run() {
          t2+=.01;
              x2 =(int) ((Math.cos(t2)+1)*100);
              y2 =(int) ((Math.sin(t2)+1)*100);
              System.out.println(x2+", "+y2);
        }
    }, 0, 2);
}
int z = 0; 
public void rD() {
   z++;

    m = new Move(x, y, Color.GREEN);
    n = new Move(x2, y2, Color.ORANGE);
    frame.add(n);
    frame.add(m);

    frame.validate();

}

private void formKeyPressed(java.awt.event.KeyEvent evt) {

    int id = evt.getID();
    int kC = evt.getKeyCode();
    if (kC == 39) {
        final java.util.Timer right = new java.util.Timer();
        right.scheduleAtFixedRate(new TimerTask() {

            public void run() {
                t += .01;
                int x1 = x;
                if (x + 51 < 400) {
                    x = x1 + (int) (10 * t) + (-1 * ((int) (20 * t * t)));
                }
                if (t > .5) {
                    t = 0;
                    right.cancel();
                }


            }
        }, 0, 10);


    }
    if (kC == 37) {
        final java.util.Timer left = new java.util.Timer();
        left.scheduleAtFixedRate(new TimerTask() {

            public void run() {
                t += .01;
                int x1 = x;
                if (x - 1 >= 0) {
                    x = x1 + (int) (-10 * t) - (-1 * ((int) (20 * t * t)));
                }
                if (t > .5) {
                    t = 0;
                    left.cancel();
                }


            }
        }, 0, 10);
    }
    if (kC == 40) {
        final java.util.Timer right = new java.util.Timer();
        right.scheduleAtFixedRate(new TimerTask() {

            public void run() {
                t += .01;
                int y1 = y;
                if (y + 51 < 400) {
                    y = y1 + (int) (10 * t) + (-1 * ((int) (20 * t * t)));
                }

                if (t > .5) {
                    t = 0;
                    right.cancel();
                }


            }
        }, 0, 10);
    }
    if (kC == 38) {
        final java.util.Timer left = new java.util.Timer();
        left.scheduleAtFixedRate(new TimerTask() {

            public void run() {
                t += .01;
                int y1 = y;
                if (y-1 >= 0)
                {
                y = y1 + (int) (-10 * t) - (-1 * ((int) (20 * t * t)));
                }
                if (t > .5) {
                    t = 0;
                    left.cancel();
                }


            }
        }, 0, 10);
    }
    if (kC == 32) {
    }
    if (kC == 67) {
    }
    if (kC == 77) {
    }
}

public static void main(String[] args) {
    new Puzzle().frame.setVisible(true);
}
}

why is this not adding both instances of the move class. it only adds the last one called. the class paint is below what shoul开发者_StackOverflow社区d i change in order for it paint both instances of move

 package puzzle;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class Move extends JPanel{

int x; 
int y; 
Color kk; 

public Move(int x1, int y1, Color k)
{
    x=x1;
    y=y1;
    kk = k; 

}
public void paintComponent(Graphics g)
{Graphics2D g2 = (Graphics2D)g;
        super.paintComponent(g2);


    g2.setColor(kk);

    g2.fill(new RoundRectangle2D.Float(x, y, 50, 50,10,10));

    g2.drawString(x+", "+y, 200, 200);

}


}

the class move paints a rectangle with the position and color passed from the puzzle class


Are you taking layout managers into consideration? Do you know about JFrame's contentPane using BorderLayout as its default layout manager, and if you add components to this without constants, they are added BorderLayout.CENTER, and only one of these can be visible at a time (the last added).

I think that you might do better disassociating Move from JPanel, yes making it paintable, but making it more of a logical object rather than a gui component and instead of having it extend JPanel, allow a single drawing JPanel to hold one or more Move objects and then draw the Move objects in this JPanel's paintComponent method.

Also, you seem to be using several java.util.Timers in your code, but since it is a Swing application, it would likely be better served by using javax.swing.Timers instead, in fact perhaps just one Swing Timer that functioned as a game loop.


if (kC == 39) {

Don't use magic numbers.

Instead you should be using:

KeyEvent.VK_???
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜