Visualizzazione dei risultati da 1 a 7 su 7
  1. #1

    close() e JFileChooser.getSelectedFile() fanno capricci

    Questo il codice:

    codice:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.util.*;
    public class PaintTable extends JPanel
    {
    	private static JPanel topPanel = new JPanel();
    	private static JButton annulla = new JButton("Annulla");
    	private static JButton cancella = new JButton("Cancella");
    	private static String[] colorNames = {"Nero","Blu","Verde","Giallo","Rosso","Bianco"};
    	private static JComboBox colors = new JComboBox(colorNames);
    	private static String[] typeNames = {"Linea retta","Rettangolo","Cerchio"};
    	private static JComboBox types = new JComboBox(typeNames);
    	private static JCheckBox rov = new JCheckBox("Figura piena");
    	private Color[] color = new Color[99999];
    	private Color[] colorList = {Color.BLACK,Color.BLUE,Color.GREEN,Color.YELLOW,Color.RED,Color.WHITE};
    	private Point[] points1 = new Point[99999];
    	private Point[] points2 = new Point[99999];
    	private Point pointT;
    	private Point pointT2;
    	private boolean start = false;
    	private int countP = 0;
    	private byte[] type = new byte[99999];
    	private boolean[] filled = new boolean[99999];
    	private static JPanel bottomPanel = new JPanel();
    	private static JLabel label = new JLabel("Cursore del mouse fuori dal pannello");
    	//************************************************************************************************
    	private static JCheckBox gradient = new JCheckBox("Gradiente");
    	private static JButton firstColor = new JButton("Colore 1");
    	private static JButton secondColor = new JButton("Colore 2");
    	private static JLabel lineW = new JLabel("Spessore:");
    	private static JTextField lineWfield = new JTextField(2);
    	private static JLabel lineD = new JLabel("Lunghezza tratteggi:");
    	private static JTextField lineDfield = new JTextField(2);
    	private static JCheckBox dashed = new JCheckBox("Tratteggi");
    	private float spessore = 0f;
    	private float spessoreV[] = new float[99999];
    	private Color firstColorG = Color.BLACK;
    	private Color secondColorG = Color.BLACK;
    	private Color[] fColorV = new Color[99999];
    	private Color[] sColorV = new Color[99999];
    	private boolean[] gradientBoolean = new boolean[99999];
    	private boolean[] dashedBoolean = new boolean[99999];
    	private float[] dashedV = new float[99999];
    	private float[] dashedSingle = {0f};
    	//************************************************************************************************
    	private static JButton Load = new JButton("Apri file");
    	private static JButton Save = new JButton("Salva file");
    	private static JFileChooser chooser;
    	private static File file;
    	private static Formatter output;
    	
    	public PaintTable()
    	{
    		setBackground(Color.WHITE);
    		
    		lineWfield.setText(""+(int)spessore);
    		
    		for (int i=0;i<99999;i++)
    		{
    			filled[i] = false;
    		}
    		
    		firstColor.addActionListener
    		(
    			new ActionListener()
    			{
    				public void actionPerformed(ActionEvent event)
    				{
    					Color colorTemp = JColorChooser.showDialog(new JFrame(),"Seleziona primo colore",firstColorG);
    					if (colorTemp != null)
    					{
    						firstColorG = colorTemp;
    					}
    				}
    			}
    		);
    		
    		secondColor.addActionListener
    		(
    			new ActionListener()
    			{
    				public void actionPerformed(ActionEvent event)
    				{
    					Color colorTemp = JColorChooser.showDialog(new JFrame(),"Seleziona secondo colore",secondColorG);
    					if (colorTemp != null)
    					{
    						secondColorG = colorTemp;
    					}
    				}
    			}
    		);
    		
    		lineWfield.addActionListener
    		(
    			new ActionListener()
    			{
    				public void actionPerformed(ActionEvent event)
    				{
    					spessore = Float.valueOf(lineWfield.getText()).floatValue();
    				}
    			}
    		);
    		
    		lineDfield.addActionListener
    		(
    			new ActionListener()
    			{
    				public void actionPerformed(ActionEvent event)
    				{
    					dashedSingle[0] = Float.valueOf(lineDfield.getText()).floatValue();
    				}
    			}
    		);
    		
    		gradient.addItemListener
    		(
    			new ItemListener()
    			{
    				public void itemStateChanged(ItemEvent event)
    				{
    					if (gradient.isSelected())
    					{
    						firstColor.setEnabled(true);
    						secondColor.setEnabled(true);
    					}
    					else
    					{
    						firstColor.setEnabled(false);
    						secondColor.setEnabled(false);
    					}
    				}
    			}
    		);
    		
    		dashed.addItemListener
    		(
    			new ItemListener()
    			{
    				public void itemStateChanged(ItemEvent event)
    				{
    					if (dashed.isSelected())
    					{
    						lineDfield.setEnabled(true);
    					}
    					else
    					{
    						lineDfield.setEnabled(false);
    					}
    				}
    			}
    		);
    		
    		annulla.addActionListener
    		(
    			new ActionListener()
    			{
    				public void actionPerformed(ActionEvent event)
    				{
    					countP--;
    					points1[countP].x = 0;
    					points1[countP].y = 0;
    					points2[countP].x = 0;
    					points2[countP].y = 0;
    					pointT.x = 0;
    					pointT.y = 0;
    					pointT2.x = 0;
    					pointT2.y = 0;
    					repaint();
    				}
    			}
    		);
    		
    		cancella.addActionListener
    		(
    			new ActionListener()
    			{
    				public void actionPerformed(ActionEvent event)
    				{
    					countP = 0;
    					pointT.x = 0;
    					pointT.y = 0;
    					pointT2.x = 0;
    					pointT2.y = 0;
    					for (int i=0;i<countP;i++)
    					{
    						points1[i].x = 0;
    						points1[i].y = 0;
    						points2[i].x = 0;
    						points2[i].y = 0;
    					}
    					repaint();
    				}
    			}
    		);
    		
    		addMouseListener
    		(
    			new MouseAdapter()
    			{
    				public void mousePressed(MouseEvent event)
    				{
    					start = true;
    					points1[countP] = event.getPoint();
    					pointT = event.getPoint();
    				}
    				public void mouseReleased(MouseEvent event)
    				{
    					points2[countP] = event.getPoint();
    					repaint();
    					countP++;
    				}
    				public void mouseExited(MouseEvent event)
    				{
    					label.setText("Cursore del mouse fuori dal pannello");
    				}
    			}
    		);
    		
    		addMouseMotionListener
    		(
    			new MouseMotionAdapter()
    			{
    				public void mouseDragged(MouseEvent event)
    				{
    					points2[countP] = event.getPoint();
    					pointT2 = event.getPoint();
    					for (int i=0;i<colorNames.length;i++)
    					{
    						if (colors.getSelectedIndex() == i)
    						{
    							color[countP] = colorList[i];
    							spessoreV[countP] = spessore;
    						}
    					}
    					label.setText(""+event.getX()+", "+event.getY());
    					repaint();
    				}
    				public void mouseMoved(MouseEvent event)
    				{
    					label.setText(""+event.getX()+","+event.getY());
    				}
    			}
    		);
    		
    		/*Load.addActionListener
    		(
    			new ActionListener()
    			{
    				public void actionPerformed(ActionEvent e)
    				{
    					JFileChooser chooser = new JFileChooser();
    					chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    					chooser.setDialogTitle("Apri file");
    					
    					int c = chooser.showOpenDialog(this);
    					
    					if (c == JFileChooser.APPROVE_OPTION)
    					{
    						
    					}
    				}
    			}
    		);*/
    		
    		Save.addActionListener
    		(
    			new ActionListener()
    			{
    				public void actionPerformed(ActionEvent e)
    				{
    					file = SaveFile();					
    					String string = file.toString();
    
    					output = new Formatter(string+".txt");
    					for (byte i = 0; i < countP; i++)
    					{
    						output.format("%d %d %d %d\n",points1[i].x,points1[i].y,points2[i].x,points2[i].y);
    					}
    					file.close();
    				}
    			}
    		);
    	}
    	
    	public static File SaveFile()
    	{
    		chooser = new JFileChooser();
    		chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    		int c = chooser.showSaveDialog(new JFrame());
    		
    		if (c == JFileChooser.APPROVE_OPTION)
    		{
    			return JFileChooser.getSelectedFile();
    		}
    		else
    		{
    			return null;
    		}
    	}
    	
    	public void paintComponent(Graphics g)
    	{
    		super.paintComponent(g);
    		Graphics2D gg = (Graphics2D) g;
    		
    		gg.setStroke(new BasicStroke(spessore));
    
    		
    		if (types.getSelectedIndex() == 0)
    		{
    			type[countP] = 0;
    		}
    		else if (types.getSelectedIndex() == 1)
    		{
    			type[countP] = 1;
    		}
    		else
    		{
    			type[countP] = 2;
    		}
    		
    		dashedBoolean[countP] = dashed.isSelected() ? true : false;
    		filled[countP] = rov.isSelected() ? true : false;
    		gradientBoolean[countP] = gradient.isSelected() ? true : false;
    		fColorV[countP] = firstColorG;
    		sColorV[countP] = secondColorG;
    		for (int i=0;i<countP;i++)
    		{
    			if (dashedBoolean[i])
    			{
    				gg.setStroke(new BasicStroke(spessoreV[i],BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND,10,dashedSingle,0));
    			}
    			else
    			{
    				gg.setStroke(new BasicStroke(spessoreV[i]));
    			}
    			
    			if (gradientBoolean[i])
    			{
    				gg.setPaint(new GradientPaint(0,0,fColorV[i],10,20,sColorV[i],true));
    			}
    			else
    			{
    				g.setColor(color[i]);
    			}
    			
    			if (type[i] == 0)
    			{
    				g.drawLine(points1[i].x,points1[i].y,points2[i].x,points2[i].y);
    			}
    			else if (type[i] == 1)
    			{
    				if (!filled[i])
    				{
    					g.drawRect(points1[i].x,points1[i].y,points2[i].x-points1[i].x,points2[i].y-points1[i].y);
    				}
    				else
    				{
    					g.fillRect(points1[i].x,points1[i].y,points2[i].x-points1[i].x,points2[i].y-points1[i].y);
    				}
    			}
    			else
    			{
    				if (!filled[i])
    				{
    					g.drawOval(points1[i].x,points1[i].y,points2[i].x-points1[i].x,points2[i].y-points1[i].y);
    				}
    				else
    				{
    					g.fillOval(points1[i].x,points1[i].y,points2[i].x-points1[i].x,points2[i].y-points1[i].y);
    				}
    			}
    		}
    		if (start)
    		{
    			if (dashed.isSelected())
    			{
    				gg.setStroke(new BasicStroke(spessore,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND,10,dashedSingle,0));
    			}
    			else
    			{
    				gg.setStroke(new BasicStroke(spessore));
    			}
    			if (gradient.isSelected())
    			{
    				gg.setPaint(new GradientPaint(0,0,firstColorG,10,20,secondColorG,true));
    			}
    			else
    			{
    				g.setColor(color[countP]);
    			}
    			if (type[countP] == 0)
    			{
    				g.drawLine(pointT.x,pointT.y,pointT2.x,pointT2.y);
    			}
    			else if (type[countP] == 1)
    			{
    				if (rov.isSelected())
    				{
    					g.fillRect(pointT.x,pointT.y,pointT2.x-pointT.x,pointT2.y-pointT.y);
    				}
    				else
    				{
    					g.drawRect(pointT.x,pointT.y,pointT2.x-pointT.x,pointT2.y-pointT.y);
    				}
    			}
    			else
    			{
    				if (rov.isSelected())
    				{
    					g.fillOval(pointT.x,pointT.y,pointT2.x-pointT.x,pointT2.y-pointT.y);
    				}
    				else
    				{
    					g.drawOval(pointT.x,pointT.y,pointT2.x-pointT.x,pointT2.y-pointT.y);
    				}
    			}
    		}
    	}
    	public static void main(String[] args)
    	{
    		JFrame frame = new JFrame("Paint");
    		frame.setSize(640,480);
    		frame.setLocationRelativeTo(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		PaintTable table = new PaintTable();
    		topPanel.setLayout(new GridLayout(2,1));
    		
    		JPanel subUpPanel = new JPanel();
    		subUpPanel.add(Load);
    		subUpPanel.add(Save);
    		subUpPanel.add(annulla);
    		subUpPanel.add(cancella);
    		colors.setMaximumRowCount(4);
    		subUpPanel.add(colors);
    		subUpPanel.add(types);
    		subUpPanel.add(rov);
    		
    		topPanel.add(subUpPanel,BorderLayout.NORTH);
    		
    		JPanel subDownPanel = new JPanel();
    		subDownPanel.add(gradient);
    		firstColor.setEnabled(false);
    		subDownPanel.add(firstColor);
    		secondColor.setEnabled(false);
    		subDownPanel.add(secondColor);
    		subDownPanel.add(lineW);
    		subDownPanel.add(lineWfield);
    		subDownPanel.add(lineD);
    		lineDfield.setEnabled(false);
    		subDownPanel.add(lineDfield);
    		subDownPanel.add(dashed);
    		
    		topPanel.add(subDownPanel,BorderLayout.CENTER);
    		
    		frame.add(topPanel,BorderLayout.NORTH);
    		frame.add(table,BorderLayout.CENTER);
    		frame.add(label,BorderLayout.SOUTH);
    		
    		frame.setVisible(true);
    	}
    }
    Ci sono due errori (che ho evidenziato in grassetto) che non riesco a comprendere: il primo è che non mi riconosce il metodo close() per chiudere il Formatter, nonostante abbia importato la libreria java.io.*;. Il secondo errore è :"non-static method getSelectedFile() cannot be referenced from a static context"... non capisco quindi, dov'è che devo inserire getSelectedFile(). Qualche idea?

  2. #2
    Utente di HTML.it L'avatar di Metflar
    Registrato dal
    Apr 2007
    Messaggi
    790
    output.close() nn file... e return chooser.getSelectedFile();

    ps..quando stai salvando su file metti le istruzioni in un blocco try-catch

  3. #3
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284

    Re: close() e JFileChooser.getSelectedFile() fanno capricci

    Originariamente inviato da Dreamer89
    file.close();
    file è di tipo java.io.File. Non c'è alcun close() in File, perché rappresenta solo un "pathname". È il Formatter che dovresti chiudere.

    Originariamente inviato da Dreamer89
    return JFileChooser.getSelectedFile();
    getSelectedFile() è un metodo di istanza, non statico. Devi quindi invocarlo sulla tua variabile 'chooser'.

    Non ho controllato tutto il resto.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  4. #4
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da Metflar
    ps..quando stai salvando su file metti le istruzioni in un blocco try-catch
    Sicuramente è sempre bene catturare le eccezioni. Però Formatter è particolare, i suoi metodi non lanciano IOException. Se c'è stata una eccezione, va letta in "polling" usando ioException().
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  5. #5
    Utente di HTML.it L'avatar di Metflar
    Registrato dal
    Apr 2007
    Messaggi
    790
    Originariamente inviato da andbin
    Sicuramente è sempre bene catturare le eccezioni. Però Formatter è particolare, i suoi metodi non lanciano IOException. Se c'è stata una eccezione, va letta in "polling" usando ioException().
    ho modificato il suo codice e mi dava eccezione proprio li
    edit:
    era java.io.FileNotFoundException;

  6. #6
    Praticamente erano stupidi errori di distrazione... grazie mille ^^

  7. #7
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da Metflar
    ho modificato il suo codice e mi dava eccezione proprio li
    edit:
    era java.io.FileNotFoundException;
    Il costruttore sì, può lanciare FileNotFoundException. I metodi format(), flush() ecc.. invece possono lanciare altre eccezioni ma non IOException, per il motivo che ho detto sopra.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.