problem z czyszczeniem ekranu

0

Jest w Javie jakaś funkcja która czyści ekran. Nic nie znalazłem, ani w necie ani na rożnych forach. Jedyne co proponują to coś w stylu:

for(int i=0;i<100;i++){
System.out.println("");
}

A ja potrzebuje żeby nowy ekran był wyświetlany dokładnie w tym samym miejscu. Znacie jakąś funkcje albo sposób, który rozwiąże mój problem?

0

chodzi Ci o konsole? a co jest zlego w wypisaniu

Systm.out.println("\n........\n") 

lub

for(int i=0;i<10;i++){
System.out.println();
}

?

pozdrawiam

0
eximius napisał(a)

chodzi Ci o konsole? a co jest zlego w wypisaniu

Systm.out.println("\n........\n") 

lub

for(int i=0;i<10;i++){
System.out.println();
}

?

pozdrawiam

To, ze mój program polega na tym ze wyświetlam tablice dwuelementowa (wyspa z sygnaturami zwierząt) i elementy (symbole zwierząt) maja po zmienie (np przesuniecie o jedno pole w bok) pojawiać sie w ten sposób, że będzie sie wydawało że przemieszczają sie skokowo. Jak zrobię odstęp iluś tam linii to nic mi to nie da...

0

W Java nie ma metody która czyści ekran, ponieważ Java nie jest dedykowana do pracy w konsoli. Zostają Ci jedynie "domowe sposoby"

0

Jeżeli masz basha, to możesz użyć specjalnej sekwencji "\033[H\033[2J".

System.out.println("TEKST1");
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("TEKST2"); 

W wyniku otrzymasz tylko "TEKST2" na górze ekranu.

0

Program będę musiał odpalić na zajęciach, a jest tam zainstalowany Windows wiec o Bashu mogę pomarzyć ;) Potrzebuje wyświetlać tablice dwuwymiarowa Stringow (plansze-wyspę). Wyświetlam poprzestawiane elementy w równych odstępcach czasu. Mozecie polecić jak to najlepiej/najprościej zrobić?

Probowałem to zrobić dzisiaj w JOptionPane.showMessageDialog(), ale to odpada bo za każdym razem trzeba klikać OK, pyzatym "pamięta" co było w poprzednim oknie i każde następne okno jest 2x większe.

Prosze o pomoc

0

Zadeklaruj tablicę o wymiarach ekranu konsoli. Przy czyszczeniu zapisz cały ekran znaczkami "" a potem wrzuć całą tablicę na konsolę i po sprawie.

0
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.text.NumberFormatter;

import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.Queue;
import java.util.LinkedList;
import java.util.HashMap;
import java.awt.Graphics;

import java.text.NumberFormat;

public class Main extends JFrame {
	private Thread draw;
	private AsciiPanel asciiPanel ;
	private JButton start;
	private JButton stop;
	private JButton add;
	private JButton reset;
	private JButton clear;
	private JFormattedTextField delay;
	private JLabel delay_lab;
	private JTextArea sequence;
	private JScrollPane sequence_sp;

	JComboBox box;
	private HashMap map;

	private ActionListener aListener;
	private MainRunnuble mainRunnuble;

	public Main() {
		super("Matrix Drawer");
		this.setSize(255,255);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.getContentPane().setLayout(null);

		initComponents();
		addComponents();

		initValues();

		this.setVisible(true);
		System.runFinalizersOnExit(true);
	}

	private void initComponents() {
		aListener = new MainActionListener(this);
		mainRunnuble = new MainRunnuble();

		map = new HashMap();

		asciiPanel = new AsciiPanel();
		asciiPanel.setBorder( BorderFactory.createLineBorder(Color.BLACK, 1) );
		asciiPanel.setBounds(10, 5, 160, 160);

		start = new JButton("Start");
		start.setActionCommand("start");
		start.addActionListener(aListener);
		start.setBounds(175, 5, 70, 20);

		stop = new JButton("Stop");
		stop.setActionCommand("stop");
		stop.addActionListener(aListener);
		stop.setBounds(175, 30, 70, 20);

		reset = new JButton("Reset");
		reset.setActionCommand("reset");
		reset.addActionListener(aListener);
		reset.setBounds(175, 55, 70, 20);

		clear = new JButton("Clear");
		clear.setActionCommand("clear");
		clear.addActionListener(aListener);
		clear.setBounds(175, 80, 70, 20);

		add = new JButton("Dodaj");
		add.setActionCommand("add");
		add.addActionListener(aListener);
		add.setBounds(105, 175, add.getPreferredSize().width, 20);

		box =new JComboBox();
		box.setBackground(Color.WHITE);
		box.addItem("A");
		map.put("A", getA());
		box.addItem("B");
		map.put("B", getB());
		box.setBounds(10, 175, 90, 20);

		delay_lab = new JLabel("Opóźnienie");
		delay_lab.setBounds(10, 200, 70, 20);

		NumberFormat formatter = NumberFormat.getIntegerInstance();
		formatter.setGroupingUsed(false);
		delay = new JFormattedTextField(formatter);
		delay.setBorder( BorderFactory.createLineBorder(Color.BLACK, 1) );
		delay.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		delay.setText("1000");
		delay.setBounds(80, 200, 90, 20);

		sequence = new JTextArea("");
		sequence.setEditable(false);
		sequence.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		sequence.setBorder( BorderFactory.createLineBorder(Color.BLACK, 1) );

		sequence_sp = new JScrollPane(sequence);
		sequence_sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		sequence_sp.setBounds(175, 105, 70, 110);
	}
	private void addComponents() {
		this.getContentPane().add(asciiPanel);
		this.getContentPane().add(start);
		this.getContentPane().add(stop);
		this.getContentPane().add(reset);
		this.getContentPane().add(clear);
		this.getContentPane().add(add);
		this.getContentPane().add(box);
		this.getContentPane().add(delay_lab);
		this.getContentPane().add(delay);
		this.getContentPane().add(sequence_sp);
	}

	private void initValues() {
		if(mainRunnuble == null || sequence == null) return;
		for(int i=0; i<6; i++) {
			mainRunnuble.add(((i % 2 ==0) ? getA() : getB() ), 1000);
			if(i % 2 == 0) {
				if(i == 0) {
					sequence.append("\tA<<<\n");
				} else {
					sequence.append("\tA\n");
				}

			}else {
				if(i == 0) {
					sequence.append("\tB<<<\n");
				} else {
					sequence.append("\tB\n");
				}
			}
		}
		sequence.select(0,0);

	}

	private String[][] getA() {
		String[][] a = {
			{"-","-","-","-","-","-","#","#","-","-","-","-","-","-"},
			{"-","-","-","-","-","#","#","#","#","-","-","-","-","-"},
			{"-","-","-","-","#","#","-","-","#","#","-","-","-","-"},
			{"-","-","-","#","#","-","-","-","-","#","#","-","-","-"},
			{"-","-","#","#","-","-","-","-","-","-","#","#","-","-"},
			{"-","-","#","#","#","#","#","#","#","#","#","#","-","-"},
			{"-","-","#","#","-","-","-","-","-","-","#","#","-","-"},
			{"-","-","#","#","-","-","-","-","-","-","#","#","-","-"},
			{"-","-","#","#","-","-","-","-","-","-","#","#","-","-"}
		};
		return a;
	}

	private String[][] getB() {
		String[][] b = {
			{"-","-","#","#","#","#","#","#","-","-","-","-","-","-"},
			{"-","-","#","#","-","-","-","-","#","#","-","-","-","-"},
			{"-","-","#","#","-","-","-","-","#","#","-","-","-","-"},
			{"-","-","#","#","-","-","-","#","#","-","-","-","-","-"},
			{"-","-","#","#","#","#","#","#","#","#","-","-","-","-"},
			{"-","-","#","#","-","-","-","-","#","#","-","-","-","-"},
			{"-","-","#","#","-","-","-","-","#","#","-","-","-","-"},
			{"-","-","#","#","-","-","-","-","#","#","-","-","-","-"},
			{"-","-","#","#","#","#","#","#","#","-","-","-","-","-"}
		};
		return b;
	}

	private class MainActionListener implements ActionListener {
		private JFrame frame;
		public MainActionListener(JFrame frame) {
			this.frame = frame;
		}
		public void actionPerformed(ActionEvent e) {
			if(e.getActionCommand().equalsIgnoreCase("start")) {
				try {
					draw.stop();
				}catch(Exception ex) {}

				draw = new Thread(mainRunnuble);
				draw.start();
			} else if(e.getActionCommand().equalsIgnoreCase("stop")) {
				try {
					draw.stop();
				}catch(Exception ex) {}
			} else if(e.getActionCommand().equalsIgnoreCase("add")) {
				Object selected = box.getSelectedItem();
				String[][] matrix = null;
				try {
					matrix = (String[][])map.get(selected);
				}catch(ClassCastException ex) {
					return;
				}
				String s = delay.getText();
				long delay = 0;
				try {
					delay = Long.parseLong(s);
				}catch(Exception ex) {}

				mainRunnuble.add(matrix, delay);

				if(sequence.getText().split("\n")[0].equals("")) {
					sequence.append("\t"+box.getSelectedItem()+"<<<\n");
				} else {
					sequence.append("\t"+box.getSelectedItem()+"  \n");
				}
				sequence.select(0,0);

			} else if(e.getActionCommand().equalsIgnoreCase("reset")) {
				try {
					draw.stop();
				}catch(Exception ex) {}
				mainRunnuble.clear();
				asciiPanel.setMatrix(null);
				sequence.setText("");
				initValues();
			} else if(e.getActionCommand().equalsIgnoreCase("clear")) {
				try {
					draw.stop();
				}catch(Exception ex) {}
				mainRunnuble.clear();
				asciiPanel.setMatrix(null);
				sequence.setText("");
			}
		}
	}//koniec klasy MainActionListener

	private class MainRunnuble implements Runnable {
		private Queue<Object[]> tablice;
		public MainRunnuble() {
			tablice = new LinkedList<Object[]>();
		}

		public boolean add(String[][] matrix, long delay) {
			if(matrix == null) return false;
			if(delay<=0) delay = 0;

			Object[] data = {
				matrix,
				new Long(delay)
			};
			return tablice.offer(data);
		}
		public void clear() {
			while(tablice.poll() != null);
		}
		/*
		 *
		 */
		public void run() {
			while(true) {
				while(tablice.peek() != null) {
					Object o = tablice.poll();
					if(o == null) continue;
					if(!(o instanceof Object[])) continue;
					Object[] data = (Object[])o;
					if(data.length != 2) continue;

					String[][] matrix = null;
					long delay = 0;
					try {
						matrix = (String[][]) data[0];
						delay = (Long) data[1];
					}catch(ClassCastException e) {
						continue;
					}

					try {
						drawMatrix(matrix, delay);
					}catch(InterruptedException e) {
						return ;
					}
				}
			}
		}
		/*
		 *
		 */
		private synchronized void drawMatrix(String[][] matrix, long delay) throws InterruptedException {
			asciiPanel.setMatrix(matrix);

			if(sequence.getText() != null && sequence.getText() != "") {
				String[] text = sequence.getText().split("\n");
				StringBuffer s = new StringBuffer(text.length -1);
				for(int i=1 ;i<text.length; i++) {
					if(i == 1) {
						s.append(text[i]+"<<<\n");
					} else {
						s.append(text[i]+"\n");
					}

				}
				sequence.setText(s.toString());
				sequence.select(0,0);
			}

			Thread.sleep(delay);
		}

	}//koniec klasy AsciiDrawer


	@Override
	protected void finalize() throws Throwable {
		try {
			draw.stop();
		}finally {
			super.finalize();
		}
	}

	public static void main(String args[]) {
		try {
			new Main();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
}

class AsciiPanel extends JPanel {
	private String[][] matrix = null;
	public AsciiPanel() {
		super();
		this.setOpaque(false);
	}
	public void setMatrix(String[][] matrix) {
		this.matrix = matrix;
		repaint();
	}
	private void drawMatrix(Graphics g) {
		if(matrix == null) return;
		g.setColor(Color.BLACK);

		int y = 20;
		int x = 10;
		for(int i=0; i<matrix.length; i++) {
			x = 10;
			for(int j=0; j<matrix[i].length; j++) {
				g.drawString(matrix[i][j], x, y);
				x+=10;
			}
			y+=10;
		}
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, this.getSize().width, this.getSize().height);
		drawMatrix(g);
	}

}
0

OK. znalazlem o co mi chodzilo. Mozna usunac posta.

1 użytkowników online, w tym zalogowanych: 0, gości: 1