Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: JLabel meant to move around is not moving

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post JLabel meant to move around is not moving

    Hello!

    I am making a program where JLabels containing numbers are to move to their correct position in a binary heap, but I cannot get the label to move. I have the x-coordinates and y-coordinates correct, and you can see the shadow of the JLabel moving, but in the end it stays where it used to be. Can somebody help?
    The moving method is the moveTo method. (Node extends JLabel, and has no significant changes)

    This is my code:

    import java.awt.Color;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.ArrayList;
    import java.util.Scanner;
     
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
     
    public class HeapDraw extends JFrame
    {
    	private static final long serialVersionUID = (240)-678-7912;
    	private static ArrayList<Integer> heap = new ArrayList<Integer>();
    	private static ArrayList<Node> heapNode = new ArrayList<Node>();
    	private static int numRows;
    	private static ArrayList<Double> xCoords;
    	private static ArrayList<Double> yCoords;
    	private static JFrame drawing;
    	private static JPanel panel;
     
    	public static void main(String [] args)
    	{
    		doNothing();
    	}
     
    	public static void doNothing()
    	{
    		readHeap();
    		drawing = new JFrame("Heap Tree");
    		for(int i = 0; i < heap.size(); i++)
    			heapNode.add(new Node(heap.get(i)));
    		panel = new JPanel();
    		drawing.setSize(800, 600);
    		panel.setBackground(Color.WHITE);
    		drawing.setVisible(true);
    		drawing.add(panel);
    		try
    		{
    			Thread.sleep(200);
    		}
    		catch (InterruptedException e1)
    		{
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
    		for(int i = 0; i < heap.size(); i++)
    		{
    			panel.add(heapNode.get(i));
    			try
    			{
    				Thread.sleep(200);
    				panel.updateUI();
    				drawing.repaint();
    			}
    			catch (InterruptedException e)
    			{
    				JOptionPane.showMessageDialog(null, "Error: Interrupted", "Error", JOptionPane.ERROR_MESSAGE);
    			}
    		}
    		drawing.repaint();
    		numRows = getRows(heap);
    		xCoords = xCoords(heap, numRows, panel.getWidth());
    		yCoords = yCoords(heap, numRows, panel.getHeight());
    		System.out.println(heapNode.get(0));
    		System.out.println(xCoords.get(0));
    		System.out.println(yCoords.get(0));
    		moveTo(heapNode.get(0), (int)((double)xCoords.get(0)), (int)((double)yCoords.get(0)));
    	}
     
    	public static void readHeap()
    	{
    		try{
    			Scanner reader = new Scanner(new FileReader("numbers.txt"));
    			while(reader.hasNextInt())
    				heap.add(reader.nextInt());
    		}//end try clause for reading numbers and inserting them into a heap, and building it
    		catch(FileNotFoundException e)
    		{
    			JOptionPane.showMessageDialog(null, "404 Error: \nFile not Found", "404 Internal Error", JOptionPane.ERROR_MESSAGE);
    			System.exit(0);
    		}//end catch clause for when input file is not found
    	}//end method readHeap
     
    	public static void moveTo(Node t, int xCoord, int yCoord)
    	{
    		int x = t.getX(), y=t.getY();
    		double xInt = xCoord>x?xCoord-x:x-xCoord; double yInt = yCoord>y?yCoord-y:y-yCoord;
    		xInt = xInt/100; yInt = yInt/100;
    		double newX = t.getX()+xInt, newY = t.getY()+yInt;
    		System.out.println(xInt);
    		System.out.println(yInt);
    		for(int i = 0; i<100; i++)
    		{
    			try
    			{
    				Thread.sleep(10);
    				t.setLocation((int)newX, (int)newY);
    				panel.updateUI();
    				drawing.repaint();
    				//System.out.println("X: " + t.getX() + "\nY: " + t.getY());
    			}
    			catch (InterruptedException e)
    			{
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			newX = newX+xInt; newY = newY+yInt;
    		}
    		System.out.println("here");
    		System.exit(0);
    	}
     
    	public static int getRows(ArrayList<Integer> a)
    	{
    		ArrayList<Integer> ref = new ArrayList<Integer>();
    		for(int i = 0; i < a.size(); i++)
    			ref.add((int)((.5*i)*(i+1)));
    		int i = 0;
    		while(ref.get(i) < a.size())
    			i++;
    		return i-1;
    	}
     
    	public static ArrayList<Double> xCoords(ArrayList<Integer> a, int numRows, int width)
    	{
    		ArrayList<Double> xCoords = new ArrayList<Double>();
    		double i = 2.0;
    		int power = 1;
    		while(i<=2*a.size())
    		{
    			double j = 1.0;
    			while(j <= i-1)
    			{
    				xCoords.add((j*width)/i);
    				j++;
    			}
    			i = Math.pow(2, power)+1;
    			power++;
    		}
    		return xCoords;
    	}
     
    	public static ArrayList<Double> yCoords(ArrayList<Integer> a, int numRows, int height)
    	{
    		ArrayList<Double> yCoords = new ArrayList<Double>();
    		double interval = height / numRows;
    		double i = 1;
    		int power = 0;
    		int multiplier = 0;
    		while(i<=2*a.size())
    		{
    			double j = 0;
    			while(j<i)
    			{
    				yCoords.add(interval*multiplier);
    				j++;
    			}
    			power++;
    			i = Math.pow(2, power);
    			multiplier++;
    		}
    		for(int k = 0; k < yCoords.size(); k++)
    			yCoords.set(k, yCoords.get(k) + 20);
    		return yCoords;
     
    	}
     
     
    }


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: JLabel meant to move around is not moving

    You need an SSCCE ( sscce.org ) - that's just too much code *and* with a missing component, you'll be lucky to find anyone willing to attempt to debug it for you. A JLabel that moves depending on some values from somewhere else doesn't sound particularly difficult - I suspect you'll work out for yourself where you're going wrong (or a better way to do it) while you're constructing the SSCCE.

Similar Threads

  1. JLabel image?
    By frozen java in forum AWT / Java Swing
    Replies: 2
    Last Post: September 28th, 2011, 07:44 AM
  2. Everything but JLabel is displaying
    By Jacksontbh in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 3rd, 2011, 10:30 PM
  3. A problem with JLabel!
    By niklas in forum AWT / Java Swing
    Replies: 5
    Last Post: December 19th, 2010, 05:51 AM
  4. A problem with JLabel!
    By niklas in forum Member Introductions
    Replies: 1
    Last Post: December 14th, 2010, 06:03 PM
  5. how to output using JLabel?
    By qaromi in forum AWT / Java Swing
    Replies: 1
    Last Post: August 30th, 2009, 02:09 PM

Tags for this Thread