Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6662351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:22:49+00:00 2026-05-26T02:22:49+00:00

package puzzle; import java.awt.Color; import javax.swing.*; import java.util.*; public class Puzzle { Integer x

  • 0
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 should 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:22:50+00:00Added an answer on May 26, 2026 at 2:22 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

package test; import java.awt.*; import java.awt.event.*; import java.awt.geom.Ellipse2D; import java.awt.image.BufferedImage; import javax.swing.*; public class
package jtextareatest; import java.io.FileInputStream; import java.io.IOException; import javax.swing.*; public class Jtextareatest { public static
package Sartre.Connect4; import javax.swing.*; public class ChatGUI extends JDialog { public ChatGUI(){ setTitle(Chat); }
package vaannila; import java.util.ArrayList; import com.opensymphony.xwork2.ActionSupport; public class RegisterAction extends ActionSupport { private String
package org.apache.wicket.examples.guestbook; import java.util.Date; import org.apache.wicket.IClusterable; public class Comment implements IClusterable { private String
package session; import java.io.FileWriter; import java.text.DateFormat; import java.text.SimpleDateFormat; import javax.swing.JOptionPane; import org.openymsg.network.FireEvent; import org.openymsg.network.Session;
package javaapplication1; import java.io.*; /** * * @author simon */ public class Main {
package pkgPeople; import java.io.Serializable; import java.text.DecimalFormat; public class Person implements Serializable{ private String name;
package bt; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
package database; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import database.Dbconnect; public class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.