|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Jan 2004
Città: ROMA
Messaggi: 2055
|
[JAVA] Modificare un'immagine con una "spruzzata" come quella di MSPaint
Devo aprire un'immagine jpg, o gif e modificarla.. Per farla breve, mi serve un qualcosa che mi permetta di modificare questa immagine come si fa con il paint quando si usa la "bomboletta spray" ...
Data la posizione centrale (ovviamente) devo spruzzare del nero nel suo intorno.. come faccio? grazie. |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Nov 2004
Città: Tra Verona e Mantova
Messaggi: 4553
|
Ti faccio un esOmpio: è come un esempio, solo che lascia da parte più di quanto non spieghi
![]() Tanto perchè funzioni, incollo due preliminari. Uno è un Runnable riflessivo, l'altro è un Window closer della stessa specie. Si usano nelle GUI per limitare il numero di elementi annidati. RefRunner.java Codice:
package it.spruzzo; import java.beans.*; public class RefRunner implements Runnable { private Object target; private String methodName; private Object[] args; public RefRunner(Object targetRef, String metName, Object...arguments) { target = targetRef; methodName = metName; args = arguments; } public void run() { try { new Statement(target, methodName, args).execute(); } catch(Exception ex) { throw new RuntimeException(ex); } } } Codice:
package it.spruzzo; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class RefWindowCloser extends RefRunner implements WindowListener { public RefWindowCloser(Object target, String methodName, Object...args) { super(target, methodName, args); } public void windowClosing(WindowEvent e) { if(SwingUtilities.isEventDispatchThread()) { run(); } else { SwingUtilities.invokeLater(this); } } //God bless copy-paste... public void windowActivated(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowOpened(WindowEvent e) {} } ![]() CenterLayout.java Codice:
package it.spruzzo; import java.awt.*; public class CenterLayout implements LayoutManager { public void addLayoutComponent(String name, Component comp) {} public void removeLayoutComponent(Component comp) {} public Dimension preferredLayoutSize(Container parent) { synchronized(parent.getTreeLock()) { Insets margin = parent.getInsets(); Dimension dim = new Dimension( margin.left + margin.right, margin.top + margin.bottom); Component[] cs = parent.getComponents(); if(cs.length != 0) { Dimension cd = cs[0].getPreferredSize(); dim.width += cd.width; dim.height += cd.height; } return dim; } } public Dimension minimumLayoutSize(Container parent) { return preferredLayoutSize(parent); } public void layoutContainer(Container parent) { synchronized(parent.getTreeLock()) { Component[] cs = parent.getComponents(); if(cs.length == 0) return; Component c = cs[0]; Dimension dim = c.getPreferredSize(); Insets margin = parent.getInsets(); int x = margin.left; int y = margin.top; int w = parent.getWidth() - margin.left - margin.right; int h = parent.getHeight() - margin.top - margin.bottom; int cw = dim.width > w ? w : dim.width; int ch = dim.height > h ? h : dim.height; x += w / 2 - cw / 2; y += h / 2 - ch / 2; c.setBounds(new Rectangle(x, y, cw, ch)); } } } Il resto è tutta discesa. Serve qualcuno che proietti un'immagine modificabile, qualcosa che modifichi le immagini e una bella finestrona da schiaffare sullo schermo. Questo è uno strumento da disegno: DrawTool.java Codice:
package it.spruzzo; import java.awt.event.*; public interface DrawTool extends MouseListener, MouseMotionListener { void setImagePanel(ImagePanel p); } ImagePanel.java Codice:
package it.spruzzo; import javax.imageio.*; import java.awt.*; import java.awt.image.*; import javax.swing.*; public class BasicImagePanel extends JPanel implements ImagePanel { private BufferedImage surface; private DrawTool tool; /** Rifila un'immagine su cui lavorare e ridimensiona il componente. Rilascia un'eccezione IllegalStateException se questo ImagePanel non sia visualizzabile */ public void setSurface(BufferedImage value) throws IllegalStateException { if(!isDisplayable()) throw new IllegalStateException("Cannot create compatible buffer."); if(surface != null) surface.flush(); surface = null; surface = getGraphicsConfiguration().createCompatibleImage( value.getWidth(), value.getHeight(), value.getTransparency()); surface.getGraphics().drawImage(value, 0, 0, null); adaptSize(); repaint(); } /** Restituisce l'immagine proiettata */ public BufferedImage getSurface() { return surface; } /** Imposta lo strumento di disegno, scollegando il precedente. */ public void setDrawTool(DrawTool t) { if(tool != null) { tool.setImagePanel(null); removeMouseListener(tool); removeMouseMotionListener(tool); } t.setImagePanel(this); addMouseMotionListener(t); addMouseListener(t); tool = t; } protected void paintComponent(Graphics g) { g.setColor(Color.WHITE); g.fillRect(0, 0, getWidth(), getHeight()); if(surface != null) g.drawImage(surface, 0, 0, null); } private void adaptSize() { Insets margin = getInsets(); Dimension size = new Dimension( surface.getWidth() + margin.left + margin.right, surface.getHeight() + margin.top + margin.bottom); setPreferredSize(size); for(Component parent = this; parent != null; parent = parent.getParent()) { if(parent instanceof JScrollPane) { JScrollPane sp = (JScrollPane)parent; sp.setViewport(sp.getViewport()); sp.validate(); break; } } } } SpruzzoTool.java Codice:
package it.spruzzo; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import javax.swing.event.*; import java.util.*; public class SpruzzoTool extends MouseInputAdapter implements DrawTool { private Random random = new Random(); private int[] track = new int[49]; private int trackSize = (int)Math.sqrt(track.length); private ImagePanel target; public void setImagePanel(ImagePanel value) { target = value; } public void mouseDragged(MouseEvent e) { if(target != null && target.getSurface() != null) { spruzza(target.getSurface(), e.getPoint()); } } private final void spruzza(final BufferedImage image, final Point p) { randomizeTrack(); int minx = Math.max(p.x - trackSize / 2, 0); int miny = Math.max(p.y - trackSize / 2, 0); int maxx = Math.min(p.x + trackSize / 2, image.getWidth() - 1); int maxy = Math.min(p.y + trackSize / 2, image.getHeight() - 1); for(int x = minx; x <= maxx; x++) { for(int y = miny; y <= maxy; y++) { int index = toArrayIndex(x - p.x + trackSize / 2, y - p.y + trackSize / 2); if(track[index] != 0) image.setRGB(x, y, 0); } } target.repaint(); } private final int toArrayIndex(int x, int y) { return x + y * trackSize; } private final void randomizeTrack() { for(int i = 0; i < track.length; i++) { track[i] = random.nextInt() > 0 ? 1 : 0; } } } MainWindow.java Codice:
package it.spruzzo; import java.awt.*; import javax.swing.*; public class MainWindow { private JFrame window = new JFrame("Spruzzo"); private ImagePanel imagePanel = new ImagePanel(); public MainWindow() { window.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); window.addWindowListener(new RefWindowCloser(this, "hide")); JPanel ipContainer = new JPanel(new CenterLayout()); ipContainer.add(imagePanel); JScrollPane ipScroller = new JScrollPane(ipContainer); window.add(ipScroller, BorderLayout.CENTER); } public ImagePanel getImagePanel() { return imagePanel; } public void show() { if(SwingUtilities.isEventDispatchThread()) { window.pack(); window.setVisible(true); } else { SwingUtilities.invokeLater(new RefRunner(window, "pack")); SwingUtilities.invokeLater(new RefRunner(window, "setVisible", true)); } } public void hide() { window.dispose(); } } ImageLoader.java Codice:
package it.spruzzo; import java.awt.image.*; import java.io.*; import java.net.*; import javax.swing.*; import javax.imageio.*; public class ImageLoader { /** Brutal concurrency :D */ public void concLoadImage(URL address, ImagePanel target) { RefRunner runner = new RefRunner(this, "loadImage", address, target); new Thread(runner).start(); } public void loadImage(URL address, ImagePanel target) throws IOException { BufferedImage image = ImageIO.read(address.openStream()); SwingUtilities.invokeLater(new RefRunner(target, "setSurface", image)); } } Main.java Codice:
package it.spruzzo; import java.net.*; import java.io.*; public class Main { public static void main(String...args) { MainWindow w = new MainWindow(); w.getImagePanel().setDrawTool(new SpruzzoTool()); w.show(); if(args.length != 0) { try { File file = new File(args[0]); URL address = new URL("file:///"+file.getCanonicalPath()); new ImageLoader().concLoadImage(address, w.getImagePanel()); } catch(Exception ex) { ex.printStackTrace(); } } } } |
![]() |
![]() |
![]() |
#3 |
Senior Member
Iscritto dal: Jan 2004
Città: ROMA
Messaggi: 2055
|
OK! grazie mille!
![]() Prendo nota e in caso di necessità ti faccio sapere!!! |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 17:59.