Feb 12

Il problema dell’ordinamento di un array in Java si può risolvere in molti modi, in questo articolo vediamo un procedimento efficiente (complessità media O(n log n)) indipendente dal contenuto dell’array.

Questa classe contiene al suo interno anche un array di etichette (quando si ordinano dei dati è comodo avere un riferimento testuale) del tutto opzionale. Il procedimento si può ri-utilizzare anche in altri linguaggi purché si adotti, se esiste, il costrutto equivalente al Generic Java del linguaggio di codifica (es. i template del C++).

import java.util.Comparator;
  public class QuickSort<generica><generica>&gt;  {
    String [] label;
    Generica[] array;
    private  void  swap( int i, int j) {
    Generica  tmp = array[i];
    array[i] = array[j];
    array[j] = tmp;
    String sTmp =label[i];
    label[i] = label[j];
    label[j] = sTmp;
  }
 
  private int partition(int low, int high) {
    int index = low+(int)(Math.round(Math.random()*(high - low)));
    Generica pivot = array[index];
    swap( index, high);
    for (int i = index = low; i &lt; high; ++ i) {
      if (array[i].compareTo(pivot) &lt;= 0) {
        swap( index++, i);
      }
    }
    swap( index, high);
    return (index);
  }
 
  private  void quickSort(int  low, int high) {
    if (high &gt; low) {
      int index = partition( low, high);
      quickSort( low, index - 1);
      quickSort( index + 1,  high);
    }
  }
 
  public  void quickSort(Generica [] array) {
    this.array=array;
    quickSort( 0, array.length - 1);
  }
 
  public  void quickSort(Generica [] array, String label[]) {
    this.label=label;
    this.array=array;
    quickSort( 0, array.length - 1);
  }
}</generica></generica>
Aug 20
//Created by mathieu albanese
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
 
public class SetteMezzo extends JPanel implements Runnable {
	// Variabili
	private JLabel giocatore, computer;
 
	private JPanel panel1, panel2, panel3;
 
	private JPanel[] emptyPanels1, emptyPanels2, emptyPanels3;
 
	private Carta[] carte;
 
	private int carta, counterGiocatore, counterComputer;
 
	private Thread thread;
 
	private boolean active;
 
	// Costruttore
	public SetteMezzo() {
		thread = new Thread(this);
		thread.start();
		thread.suspend();
 
		panel1 = new JPanel(new GridLayout(1, 11, 1, 1));
		panel1.setBackground(new Color(0, 128, 0));
 
		panel2 = new JPanel(new GridLayout(1, 11, 1, 1));
		panel2.setBackground(new Color(0, 128, 0));
 
		panel3 = new JPanel(new GridLayout(1, 11, 1, 1));
		panel3.setBackground(new Color(0, 128, 0));
 
		carte = new Carta[41];
		for (int i = 0; i < 41; i++)
			carte[i] = new Carta(i);
 
		emptyPanels1 = new JPanel[10];
		for (int i = 0; i < 10; i++) {
			emptyPanels1[i] = new JPanel();
			emptyPanels1[i].setBackground(new Color(0, 128, 0));
		}
 
		emptyPanels2 = new JPanel[10];
		for (int i = 0; i < 10; i++) {
			emptyPanels2[i] = new JPanel();
			emptyPanels2[i].setBackground(new Color(0, 128, 0));
		}
 
		emptyPanels3 = new JPanel[10];
		for (int i = 0; i < 10; i++) {
			emptyPanels3[i] = new JPanel();
			emptyPanels3[i].setBackground(new Color(0, 128, 0));
		}
 
		setBackground(new Color(0, 128, 0));
		setPreferredSize(new Dimension(1020, 600));
		setBorder(BorderFactory.createLoweredBevelBorder());
		setLayout(new GridLayout(3, 1, 1, 1));
 
		// Components
		giocatore = new JLabel("");
		giocatore.setFont(new Font("Verdana", 1, 13));
		giocatore.setHorizontalAlignment(JLabel.CENTER);
		giocatore.setForeground(Color.white);
 
		computer = new JLabel("");
		computer.setFont(new Font("Verdana", 1, 13));
		computer.setHorizontalAlignment(JLabel.CENTER);
		computer.setForeground(Color.white);
 
		panel1.add(giocatore, 0);
		for (int i = 1; i < 11; i++) {
			panel1.add(emptyPanels1[i - 1], i);
		}
 
		panel2.add(carte[0], 0);
		for (int i = 1; i < 11; i++) {
			panel2.add(emptyPanels2[i - 1], i);
		}
 
		panel3.add(computer, 0);
		for (int i = 1; i < 11; i++) {
			panel3.add(emptyPanels3[i - 1], i);
		}
 
		add(panel1);
		add(panel2);
		add(panel3);
	}
 
	public void startGame() {
		generaCarta();
		counterGiocatore++;
		giocatore.setText("Giocatore");
		panel1.remove(counterGiocatore);
		panel1.add(carte[carta], counterGiocatore);
		updateScoreGiocatore();
	}
 
	public void updateScoreGiocatore() {
		SetteMezzoMain.punteggioG.setForeground(Color.red);
		double score = Double.valueOf(SetteMezzoMain.punteggioG.getText())
				.doubleValue();
		if (carta == 30) {
			score = 7.5;
			SetteMezzoMain.punteggioG.setText("" + score);
			verifyValue(score);
			return;
		} else if (carta < 8)
			score = score + carta;
		else if (carta == 8 || carta == 9 || carta == 10)
			score = score + 0.5;
		else {
			StringBuffer s = new StringBuffer("" + carta);
			String lastNString = new String("" + s.charAt(1));
			int lastN = Integer.valueOf(lastNString).intValue();
			if (lastN > 0 && lastN < 8)
				score = score + lastN;
			else
				score = score + 0.5;
		}
		SetteMezzoMain.punteggioG.setText("" + score);
		verifyValue(score);
	}
 
	public void updateScoreComputer() {
		double score = Double.valueOf(SetteMezzoMain.punteggioC.getText())
				.doubleValue();
		if (carta == 30) {
			score = 7.5;
			SetteMezzoMain.punteggioC.setText("" + score);
			verifyValueComputer(score);
			return;
		} else if (carta < 8)
			score = score + carta;
		else if (carta == 8 || carta == 9 || carta == 10)
			score = score + 0.5;
		else {
			StringBuffer s = new StringBuffer("" + carta);
			String lastNString = new String("" + s.charAt(1));
			int lastN = Integer.valueOf(lastNString).intValue();
			if (lastN > 0 && lastN < 8)
				score = score + lastN;
			else
				score = score + 0.5;
		}
		SetteMezzoMain.punteggioC.setText("" + score);
		verifyValueComputer(score);
 
	}
 
	public void verifyValueComputer(double score) {
		if (score > 7.5) {
			active = false;
 
			SetteMezzoMain.ris.setText("Il computer ha sballato - Hai vinto!");
			SetteMezzoMain.btnGiu.setEnabled(false);
			SetteMezzoMain.btnSto.setEnabled(false);
			SetteMezzoMain.btnNew.setEnabled(true);
			int puntata = Integer.valueOf(SetteMezzoMain.puntata1.getText())
					.intValue();
			int cassa = Integer.valueOf(SetteMezzoMain.cassa1.getText())
					.intValue();
			SetteMezzoMain.cassa1.setText("" + (cassa + puntata * 2));
			SetteMezzoMain.puntata1.setText("0");
			SetteMezzoMain.giu.setText("");
			thread.suspend();
		} else if (score > 4.5) {
			active = false;
 
			SetteMezzoMain.giu.setText("Sto!");
			double scoreGiocatore = Double.valueOf(
					SetteMezzoMain.punteggioG.getText()).doubleValue();
			if (score < scoreGiocatore) {
				SetteMezzoMain.ris.setText("Hai vinto!");
				SetteMezzoMain.btnGiu.setEnabled(false);
				SetteMezzoMain.btnSto.setEnabled(false);
				SetteMezzoMain.btnNew.setEnabled(true);
				int puntata = Integer
						.valueOf(SetteMezzoMain.puntata1.getText()).intValue();
				int cassa = Integer.valueOf(SetteMezzoMain.cassa1.getText())
						.intValue();
				SetteMezzoMain.cassa1.setText("" + (cassa + puntata * 2));
				SetteMezzoMain.puntata1.setText("0");
			} else {
				SetteMezzoMain.ris.setText("Hai perso");
				SetteMezzoMain.btnGiu.setEnabled(false);
				SetteMezzoMain.btnSto.setEnabled(false);
				SetteMezzoMain.btnNew.setEnabled(true);
				SetteMezzoMain.puntata1.setText("0");
			}
			thread.suspend();
		}
	}
 
	public void verifyValue(double score) {
		if (score > 7.5) {
			SetteMezzoMain.ris.setText("Sballato!");
			SetteMezzoMain.btnGiu.setEnabled(false);
			SetteMezzoMain.btnSto.setEnabled(false);
			SetteMezzoMain.btnNew.setEnabled(true);
			SetteMezzoMain.puntata1.setText("0");
			active = false;
			thread.suspend();
		}
	}
 
	public void generaCarta() {
		carta = (int) ((Math.random() * 40) + 1);
		if (!carte[carta].state)
			carte[carta].state = true;
		else
			generaCarta();
	}
 
	public void giu() {
		if (counterGiocatore < 10) {
			generaCarta();
			counterGiocatore++;
			panel1.remove(counterGiocatore);
			panel1.add(carte[carta], counterGiocatore);
			updateScoreGiocatore();
		}
	}
 
	public void sto() {
		computer.setText("Computer");
		generaCarta();
		counterComputer++;
		panel3.remove(counterComputer);
		panel3.add(carte[carta], counterComputer);
		updateScoreComputer();
		thread.resume();
		active = true;
	}
 
	public void reset() {
		counterGiocatore = 0;
		counterComputer = 0;
		carta = 0;
		giocatore.setText("");
		computer.setText("");
 
		remove(panel1);
		remove(panel2);
		remove(panel3);
 
		panel1 = new JPanel(new GridLayout(1, 11, 1, 1));
		panel1.setBackground(new Color(0, 128, 0));
 
		panel2 = new JPanel(new GridLayout(1, 11, 1, 1));
		panel2.setBackground(new Color(0, 128, 0));
 
		panel3 = new JPanel(new GridLayout(1, 11, 1, 1));
		panel3.setBackground(new Color(0, 128, 0));
 
		panel1.add(giocatore, 0);
		for (int i = 1; i < 11; i++) {
			panel1.add(emptyPanels1[i - 1], i);
		}
 
		panel2.add(carte[0], 0);
		for (int i = 1; i < 11; i++) {
			panel2.add(emptyPanels2[i - 1], i);
		}
 
		panel3.add(computer, 0);
		for (int i = 1; i < 11; i++) {
			panel3.add(emptyPanels3[i - 1], i);
		}
		add(panel1);
		add(panel2);
		add(panel3);
 
		carte = new Carta[41];
		for (int i = 0; i < 41; i++)
			carte[i] = new Carta(i);
 
		updateUI();
 
	}
 
	public void run() {
		while (true) {
			if (active) {
				try {
					thread.sleep(200);
				} catch (Exception e) {
				}
				if (counterComputer < 10) {
					generaCarta();
					counterComputer++;
					panel3.remove(counterComputer);
					panel3.add(carte[carta], counterComputer);
					updateScoreComputer();
				}
			}
		}
	}
 
	public static void main(String[] args) {
		JFrame frame = new JFrame("SetteMezzo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(new SetteMezzo());
		frame.pack();
		frame.setVisible(true);
	}
}
Aug 20
//Classe Master per la lavagna
 
//classi supplementari importate
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.net.*;
import java.io.*;
import java.applet.*;
//classe gestore eventi mouse
class Mouse extends MouseInputAdapter{
	Lavagna lav;
	int x;
	int y;
	int px;
	int py;
	Color col;
	public Mouse(Lavagna lavApp,Color color){
		lav=lavApp;
		col=color;
		x=0;
		y=0;
		px=0;
		py=0;
	}
	public void mouseReleased(MouseEvent e){
		px=0;
		py=0;
		x=0;
		y=0;
	}
	public void mouseDragged(MouseEvent e){
		x=e.getX();
		y=e.getY();
		if((px!=0)&&(px!=0)){
			if(MasterGraph.sts){
				lav.erase(px,py,x,y);
			}else{
				lav.draw(px,py,x,y,col);
			}
			MasterGraph.sendPoint(""+px+"_"+py+"_"+x+"_"+y+"_*");
		}
		px=x;
		py=y;
	}
	public void setColor(Color color){
		col=color;
	}
}
// classe finta per catturare l' oggetto Image
class Finta extends Applet{
	public void init(){
		setBackground(Color.white);
	}
 
}
// classe lavagna elettronica
class Lavagna extends Canvas{
	int px;
	int py;
	int x;
	int y;
	Color col;
	Graphics g;
	Image im;
	public Lavagna(){
		px=0;
		py=0;
		x=0;
		y=0;
		col=Color.black;
	}
	public void initG(Finta applet,int x,int y){
		im = applet.createImage(x,y);
		g = im.getGraphics();
	}
	public void draw(int px,int py,int x,int y,Color col){
		this.px=px;
		this.py=py;
		this.x=x;
		this.y=y;
		this.col=col;
		g.setColor(col);
		g.drawLine(px,py,x,y);
		repaint();
	}
	public void erase(int px,int py,int x,int y){
		g.setColor(Color.white);
		g.drawLine(px,py,x,y);
		repaint();
	}
	public void paint(Graphics g){
		g.drawImage(im,0,0,this);
	}
}
// classe principale
public class MasterGraph extends JFrame{
	static Lavagna lavagna;
	Mouse mouse;
	Color color;
	static DatagramPacket pOut;
	static MulticastSocket mcSock;
	static InetAddress ia;
	JButton bRed;
	JButton bBlack;
	JButton bBlue;
	JButton bYellow;
	JButton bMagenta;
	JButton bGreen;
	JButton bGray;
	JButton bOrange;
	JButton bPink;
	JButton bCyan;
	JButton jbuSelected;
	JToggleButton jtbStatus;
	static boolean sts;
	public MasterGraph(String title){
		super(title);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(300,300);
		setResizable(false);
		try{
			mcSock=new MulticastSocket(2000);
		}catch(IOException e){
			JOptionPane.showMessageDialog(null,"Impossibile aprire porta 2000!nIl programma verrà terminato.");
			System.exit(0);
		}
		sts=false;
		color=Color.black;
		jtbStatus=new JToggleButton("P");
		jtbStatus.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				if(jtbStatus.isSelected()){
					jtbStatus.setText("E");
					sts=true;
					sendStatus(1);
				}else{
					jtbStatus.setText("P");
					sts=false;
					sendStatus(0);
				}
			}
		});
		JPanel jpaStatus=new JPanel();
		jpaStatus.add(jtbStatus);
		jbuSelected=new JButton();
		jbuSelected.setBackground(color);
		JPanel jpa=new JPanel();
		jpa.add(jbuSelected);
		bRed=new JButton();
		bRed.setBackground(Color.red);
		bRed.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				color=bRed.getBackground();
				jbuSelected.setBackground(color);
				mouse.setColor(color);
				sendColor();
			}
		});
		bBlue=new JButton();
		bBlue.setBackground