Cách repaint một hình chữ nhật ở vị trí khác trong java swing

mình muốn vẽ lại chữ nhật màu đỏ ở ví trí khác bằng nút Start sau đó xóa chữ nhật ở vị trí cũ đi thì làm thế nào . Mấy bác giúp mình với .

package Project;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.ImageObserver;
import java.sql.Time;
import java.text.AttributedCharacterIterator;

import javax.swing.*;
import javax.swing.border.EmptyBorder;



public class GiaoDien extends JFrame  implements ActionListener,Runnable{
	private static final Color[] COLOR_ARRAY = {Color.BLUE, Color.CYAN, Color.YELLOW, Color.GREEN, Color.ORANGE, Color.RED};
	private static final int X_START = 100;
	private static final int X_END = 900;
	private static final int Y_END = 500;
	private static int fWidth = 1000;
	private static int fHeight = 600;

	//	private JLabel lbDiskSelect;
	private JComboBox cbDiskSelect;
	private JButton startBtn;
	private static int thap1,thap2,thap3;
	int t = 300;
	private Thread thread ;
	
	public GiaoDien() {
		//========================CREATE GUI===========================
		super.setSize(fWidth,fHeight);
		super.setLocationRelativeTo(null);
		super.setTitle("Towers Of Hanoi ");

		super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		super.setLayout(null);
		super.setBounds(500, 100, fWidth, fHeight);

		//============================================================

		/* --- DISK CHOOSE --- */

		//-----------------------
		/* --- LABEL DISK CHOOSE--- */

		//-----------------------
		/* --- START--- */
		startBtn = new JButton("START");
		startBtn.setBounds(200, 0, 100, 25);
		//----------------

		thap1 = 4;
		thap2 = thap3 = 0 ;
		
		thread = new Thread(this);
		thread.start();

	

		//========================Add========================
		super.add(startBtn);
		startBtn.addActionListener(this);
		
		//===================================================
		super.setVisible(true);
	}


	public void paint(Graphics g)
	{
		Graphics2D g2D = (Graphics2D) g;
		g2D.setStroke(new BasicStroke(10));
		g.setColor(Color.DARK_GRAY);        
//		//Draw the platform
//		//Ve duong chan thap ha noi
//		g.drawLine(X_START, Y_END, X_END, Y_END);
//		//
//		//  Ve thap ben trai
//		g.drawLine(250,200 ,250 ,500);
//		//
//		//	Ve thap o giua
//		g.drawLine(500, 200,500, 500);
//		//
//		// Ve thap ben phai
//		g.drawLine(750, 200,750, 500);
		// Draw the rods
		g.setColor(Color.red);
		g.drawRect(t,t,150-2*15,10);
	
	}


	public void paintDisk(int x,int y,int thap3,Graphics g)
	{
		Graphics2D g2D = (Graphics2D) g;
		g2D.setStroke(new BasicStroke(20));
		//
		//ve dia
		
//		for(int i = 0 ; i < thap1 ; i++)
//		{
//			g.setColor(COLOR_ARRAY[0]);
//			g.drawRect(175+15*i,500-25-i*30,150-2*15*i,10);
//		}
//
//		for(int i = 0 ; i < thap2 ; i++)
//		{
//			g.setColor(COLOR_ARRAY[1]);
//			g.drawRect(400+15*i,500-25-i*30,150-2*15*i,10);
//		}
//
//		for(int i = 0 ; i < thap3 ; i++)
//		{
//			g.setColor(COLOR_ARRAY[2]);
//			g.drawRect(600+15*i,500-25-i*30,150-2*15*i,10);
//		}
	}


	public void dequi(int n, int thap1, int thap2, int thap3) 
	{

		if (n == 1) {
			thap1--;
			thap3++;
			update();
		} else {
			//chuyển n-1 đĩa từ cọc nguồn sang cọc trung gian,
			//lấy cọc đích làm cọc phụ
			dequi(n - 1,thap1, thap2, thap3);

			//chuyển còn lại từ cọc nguồn sang cọc đích
			thap1--;
			thap3++;
			update();
			//chuyễn n-1 từ cọc trung gian về cọc đích,
			//lấy cọc nguồn làm cọc trung gian
			dequi(n - 1,thap2,thap3,thap1);
		}
	}


	public void update() {
		
		revalidate();
		repaint();
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
	
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource() == startBtn);
		{
			System.out.println("dfdf");
			t+= 100;
			update();
		}
	}


	public static void main(String[] args) {
		GiaoDien a = new GiaoDien();
	}


}

Việc bạn cần làm là xóa hình cũ đi.
Tk: java graphics clear.

3 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?