0

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?

1
Contributor
1
Reply
2
Views
6 Years
Discussion Span
Last Post by DARK_BYTE
0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.