HI I am working on a project to animate user-defined algos. I am still at the very beginning but I am practicing code in swing and here is an Array class that the user can use in his user defined algo:

import java.awt.*;
import javax.swing.*;
public class Array1 extends JPanel {
	
	int size = 0;
	int count = 0;
	int[]hold;
	
	public Array1(int[]arr)
	{
		size=arr.length;
		hold = arr;
	}
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
		for(int i=1; i<=size; i++)
		{
			g2d.drawRect((i*30), 30, 30, 50);
		}
		
		for(int i=1; i<=size; i++)
		{
			g2d.drawString(Integer.toString(hold[i-1]), (i*30)+15, 55);
		}
	}
	
	
	
	public static void main(String [] args)
	{
		int [] numArr  = {1,3,1,-1,5};
		Array1 myArray = new Array1(numArr);
		JFrame frame = new JFrame("Rectangles");
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    frame.add(myArray);
	    frame.setSize(360, 300);
	    frame.setLocationRelativeTo(null);
	    frame.setVisible(true);
	
	}

}

The main method will go in the user defined algo that will be compiled and run from my main GUI window when the user for example clicks the "compile" button...

I want to build a swap method for the Array1 class that will swap two elements and repaint the component. I don't know how to repaint, etc;can someone please give me a simple example on how to swap two elements and repaint?

Okie solved can use the repaint method with subclass of JPanel. Just used this.repaint() in my swap method and worked perfectly....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.