crazedmeph's Profile User Rating: -----

Reputation: 2 Apprentice
Group:
Active Members
Active Posts:
90 (0.07 per day)
Joined:
19-October 09
Profile Views:
1,688
Last Active:
User is offline Feb 14 2013 09:04 AM
Currently:
Offline

Previous Fields

Country:
US
OS Preference:
Windows
Favorite Browser:
Chrome
Favorite Processor:
Intel
Favorite Gaming Platform:
XBox
Your Car:
Toyota
Dream Kudos:
0

Latest Visitors

Icon   crazedmeph Live laugh code

Posts I've Made

  1. In Topic: Byte array to BufferedImage Question

    Posted 15 Nov 2012

    How do you write an Image with ImageIO? And I see the problem, I am currently using this
    imageBytes = ((DataBufferByte) image.getData().getDataBuffer()).getData();
    
    To get the array of bytes, it will give me the correct amount of bytes that I would expect with a 2x2 image i get 16 bytes which is ARGB * 4 = 16. But there is nothing to tell that it is a picture. I was using
    try{
    			ByteArrayOutputStream baos = new ByteArrayOutputStream();
    			ImageIO.write(image, "jpg", baos );
    			baos.flush();
    			imageBytes = baos.toByteArray();
    			baos.close();
    		}catch(IOException e){System.out.println(e.getMessage());}
    

    Which always gave me 800+ bytes even with a 2x2 image..... Im not sure why. And when i save it with the first one I cannot view it, because there are no bytes that tell its a picture im guessing?
  2. In Topic: Byte array to BufferedImage Question

    Posted 2 Nov 2012

    I tried both
    BufferedImage.TYPE_4BYTE_ABGR
    and
    BufferedImage.TYPE_3BYTE_BGR

    Neither of them worked :/
  3. In Topic: Generic Casting Question

    Posted 12 Oct 2012

    Thanks for the help guys :), thanks for the information! You guys are awesome, can always rely on dreamincode for answers :)
  4. In Topic: Generic Casting Question

    Posted 12 Oct 2012

    Yea i know how to do it that way, but I was wanting to see if you could cast a object or do something like that with generics. It might not be possible but I thought it might be :P
  5. In Topic: JScrollPane Question

    Posted 8 Sep 2012

    View PostSheph, on 07 September 2012 - 06:08 PM, said:

    It works for me. You have to call revalidate() to force the JScrollPane to check the size again.

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    
    @SuppressWarnings("serial")
    public class Zoom extends JPanel {
    
    	private BufferedImage img;
    	private int zoom; // screen pixels per image pixel
    
    	public Zoom(BufferedImage img) {
    		super(null);
    		this.img = img;
    
    		zoom(1);
    		
    		// draw a circle on our image
    		Graphics g = img.getGraphics();
    		g.setColor(Color.white);
    		g.fillRect(0, 0, img.getWidth(), img.getHeight());
    		g.setColor(Color.red);
    		g.fillOval(20, 20, 100, 100);
    		g.dispose();
    	}
    
    	public void zoom(int zoom) {
    		this.zoom = zoom;
    		setPreferredSize(new Dimension(img.getWidth() * zoom, img.getHeight()
    				* zoom));
    		revalidate();
    	}
    
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		g.drawImage(img, 0, 0, zoom * img.getWidth(), zoom * img.getHeight(),
    				null);
    	}
    
    	public static void main(String[] args) {
    		JFrame frame = new JFrame("Zoom");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(800, 600);
    		frame.setLocationRelativeTo(null);
    
    		Zoom z = new Zoom(new BufferedImage(800, 600,
    				BufferedImage.TYPE_INT_ARGB));
    
    		JScrollPane scroll = new JScrollPane(z);
    
    		frame.add(scroll);
    
    		frame.setVisible(true);
    
    		// wait one second to allow GUI to show, then zoom (to simulate mid draw)
    		try {
    			Thread.sleep(1000);
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    		z.zoom(2);
    	}
    }
    


    Haha I just realized what I did, I put that in where it updates so it kept setting the size bigger and bigger and it was spazing out lol. I just put it in the zoom buttons but now everything works exception 1 thing.

    public void zoomIn(){
    		if(zoom > -.99)
    			zoom -= .1;
    		setPreferredSize(new Dimension((int)(getWidth()*zoom *-1)+getWidth(), (int)(getHeight()* zoom * -1)+getHeight()));
    		System.out.println((int)((getWidth()*zoom *-1)+getWidth())+ (int)(getHeight()* zoom * -1));
    		revalidate();
    	}
    	
    	public void zoomOut(){
    		if(zoom < .99)
    			zoom += .1;
    		setPreferredSize(new Dimension((int)(getWidth()*zoom *-1)+getWidth(), (int)(getHeight()* zoom * -1)+getHeight()));
    		System.out.println((int)((getWidth()*zoom *-1)+getWidth())+ (int)(getHeight()* zoom * -1));
    		revalidate();
    	}
    
    


    when I zoom out my formula is making it zoom in even more. Until it gets to a certain point and then jumps to the correct spot. And that is because of the formula. Cant really think of another formula right now, super tired from work haha. Ill work on it after my homework i guess, but if you can think of something that would be awesome! Btw, here is all the updated code now just in case anyone wants to try it. Btw the reason I have that weird formula is because of how the paint is zooming haha. Just makes it zoom nice and neat :)

    package SpriteCreator;
    
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    import javax.swing.ScrollPaneConstants;
    import javax.swing.Scrollable;
    
    
    public class Sprites extends JFrame implements ActionListener{
    
    	private static final long serialVersionUID = 1L;
    	JMenuItem Open, Save, New, One, Two, Three, Normal;
    	DrawPane panel;
    	ColorChooser color;
    	JScrollPane pane;
    	
    	
    	Sprites(){
    		color = new ColorChooser();
    		panel = new DrawPane(color);
    		color.setPanel(panel);
    		
    		JMenuBar menuBar = new JMenuBar();
    		
    		JMenu menu = new JMenu("File");
    		JMenu size = new JMenu("Size");
    		
    		menuBar.add(menu);
    		menuBar.add(size);
    		
    		Open = new JMenuItem("Open");
    		Save = new JMenuItem("Save");
    		New = new JMenuItem("New");
    		
    		Normal = new JMenuItem("Normal");
    		One = new JMenuItem("One");
    		Two = new JMenuItem("Two");
    		Three = new JMenuItem("Three");
    		
    		New.addActionListener(this);
    		Open.addActionListener(this);
    		Save.addActionListener(this);
    		
    		Normal.addActionListener(this);
    		One.addActionListener(this);
    		Two.addActionListener(this);
    		Three.addActionListener(this);
    		
    		menu.add(New);
    		menu.add(Open);
    		menu.add(Save);
    
    		size.add(Normal);
    		size.add(One);
    		size.add(Two);
    		size.add(Three);
    		
    		setJMenuBar(menuBar);
    		
    		pane = new JScrollPane(panel, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    //		pane = new JScrollPane(panel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    		pane.getViewport().add(panel);
    		
    		setResizable(true);
    		setSize(730,582);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setLayout(new BorderLayout());
    		getContentPane().add(pane, BorderLayout.CENTER);
    		getContentPane().add(color, BorderLayout.WEST);
    		setVisible(true);
    	}
    	
    	public void actionPerformed(ActionEvent e) {
    		Object o = e.getSource();
    		
    		if(o == New)
    			newFile();
    		else if(o == Save)
    			saveFile();
    		else if(o == Open)
    			openFile();
    		else if(o == Normal)
    			panel.setSize(0);
    		else if(o == One)
    			panel.setSize(1);
    		else if(o == Two)
    			panel.setSize(2);
    		else if(o == Three)
    			panel.setSize(3);
    		
       }
    	
    	public void newFile(){
    		panel.newFile();
    	}
    	
    	public void saveFile(){
    		File file = null;
    		JFileChooser fc = null;
    		int returnVal = 0;
    		String directory, savePath, fileFormat = "";
    		
    		directory = System.getProperty("user.dir");
    
    		fc = new JFileChooser(directory);
    		fc.setApproveButtonText("Save");
    		fc.setApproveButtonToolTipText("Save the file");
    		
    		if(fc != null)
    			returnVal = fc.showOpenDialog(this);
    		
    		if (returnVal == JFileChooser.APPROVE_OPTION && fc != null){
    			if(!fc.getSelectedFile().getPath().contains(".")){ //if the file was not stated which format use default .png				
    				file = new File(fc.getSelectedFile().getPath() + ".png");
    				savePath = file.getPath();
    			}
    			else{
    				savePath = fc.getSelectedFile().getPath();
    				savePath = savePath.toLowerCase();
    			}
    			
    			for(int i = 1; i < 4; i++){
    				fileFormat = savePath.charAt(savePath.length()-i) + fileFormat;
    			}
    			fileFormat = fileFormat.toLowerCase();
    			if(fileFormat.equals("png") || fileFormat.equals("jpg") || fileFormat.equals("gif") || fileFormat.equals("bmp")){	
    				file = new File(savePath);
    				panel.saveFile(file, fileFormat);
    			}
    			else{
    				JOptionPane.showMessageDialog(this, "File format should be .png, .jpg, .gif or .bmp");
    			}
    		}
    	}
    	
    	public void openFile(){
    		File file = null;
    		JFileChooser fc = null;
    		int returnVal = 0;
    		String directory;
    		
    		directory = System.getProperty("user.dir");
    		fc = new JFileChooser(directory);
    		
    		if(fc != null)
    			returnVal = fc.showOpenDialog(this);
    		
    		
    		if (returnVal == JFileChooser.APPROVE_OPTION && fc != null){
    			file = fc.getSelectedFile();
    			panel.openFile(file);
    		}
    	}
    	
    	public static void main(String[] args){
    		new Sprites();
    	}
    }
    
    
    


    package SpriteCreator;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.JPanel;
    import javax.swing.Scrollable;
    import javax.swing.Timer;
    
    public class DrawPane extends JPanel implements MouseListener,MouseMotionListener, ActionListener{
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	ColorChooser color;
    	Color[][] colors = new Color[32][32];
    	BufferedImage img;
    	int x, y;
    	Timer timer;
    	boolean pickUp = false, fill = false;
    	double zoom = 0;
    	int width, height, brushSize, panelWidth, panelHeight;
    	
    	DrawPane(ColorChooser color){
    		this.color = color;
    		img = new BufferedImage(32,32, BufferedImage.TYPE_INT_RGB);
    		setPreferredSize(new Dimension(600, 600));
    		addMouseListener(this);
    		addMouseMotionListener(this);
    		
    		brushSize = 0;
    
    		timer = new Timer(30, this);
    		timer.setInitialDelay(30);
    		timer.start(); 
    		
    		for(int j = 0; j < 32; j++){
    			for(int i = 0; i < 32; i++){
    				colors[i][j] = Color.white;
    			}
    		}
    	}
    
    	
    	public void saveFile(File file, String fileFormat){
    		for(int j = 0; j < 32; j++){
    			for(int i = 0; i < 32; i++){
    				int rgb = colors[i][j].getRGB();
    				img.setRGB(i, j, rgb);
    			}
    		}
    		try{
    			ImageIO.write(img, fileFormat, file);
    		}catch(Exception e){
    			System.out.println(e);
    		}
    	}
    	
    	public void openFile(File file){	
    		BufferedImage image = null;
    		try {
    			image = ImageIO.read(file);
    			for(int j = 0; j < 32; j++){
    				for(int i = 0; i < 32; i++){
    					int rgb = image.getRGB(i,j);
    					Color color = new Color(rgb);
    					colors[i][j] = color;
    				}
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    	
    	public void newFile(){
    		for(int j = 0; j < 32; j++){
    			for(int i = 0; i < 32; i++){
    				colors[i][j] = Color.white;
    			}
    		}
    	}
    	
    	public void actionPerformed(ActionEvent e) {
    		repaint();
    		timer.restart();
    	}
    	
    	public void isPickUp(boolean a){
    		pickUp = a;
    	}
    	
    	public void fill(boolean fill){
    		this.fill = fill;
    	}
    	
    	/* Fills the canvas with the selected color all the way to the border
    	 * (a different color than is clicked on)
    	 */
    	public void start(Color c, int x, int y){
    		if(c == colors[x][y]){
    			colors[x][y] = color.getColor();
    			
    			if(x > 0)
    				start(c, x - 1, y);
    			if(x < 31)
    				start(c, x + 1, y);
    			if(y < 31)
    				start(c, x, y + 1);
    			if(y > 0)
    				start(c, x, y - 1);
    		}
    	}	
    	
    	public void setSize(int brushSize){
    		this.brushSize = brushSize;
    	}
    	
    	public void mousePressed(MouseEvent e){
    		try{
    			/* Check to see if it is in scope
    			 * 
    			 */
    		 	if(!fill && !pickUp && e.getX() < getWidth() && e.getX() > 0 && e.getY() < getHeight() && e.getY() > 0 && (e.getX() / 100) < 8 && (e.getY() / 80) < 7){
    		 		
    		 		/* Checks to see which brushSize to draw when the mouseDragged event is fired
        	 		 * Either a normal single cell up to 3 cells on each side
        	 		 */
    		 		if(brushSize > 0){
    		 			for(int i = -brushSize; i < brushSize; i++){
    		 				for(int j = -brushSize; j < brushSize; j++){
    		 					colors[(x / width)+i][(y / height)+j] = color.getColor();
    		 				}
    		 			}
    		 		}
    		 		else{
    		 			colors[x / width][y / height] = color.getColor();
    		 		}
    			}
    		 	else{
    		 		if(pickUp){ // Check to see if i want to get the color from the canvas or not
    		 			color.setSelectedColor(colors[x / width][y / height]);
    		 			pickUp = false;
    		 		}
    		 		else{
    		 			if(fill){ // start to fill the canvas
    		 				start(colors[x / width][y / height], x / width, y / height);
    		 				fill = false;
    		 			}
    		 		}
    		 	}
    		}catch(Exception r){System.out.println(e.getY() / (getWidth()/32));}
    		color.repaint();
        }
    
        public void mouseReleased(MouseEvent e) {
    
        }
    
        public void mouseEntered(MouseEvent e) {
    
        }
    
        public void mouseExited(MouseEvent e) {
    
        }
    
        public void mouseClicked(MouseEvent e) {
        }
    	 
    	 public void mouseMoved(MouseEvent e) {
    	 	x = e.getX();
    		y = e.getY();
        }
    
        public void mouseDragged(MouseEvent e) {
        	try{
        	 	if(!pickUp && e.getX() < getWidth() && e.getX() > 0 && e.getY() < getHeight() && e.getY() > 0 && (e.getX() / 100) < 8 && (e.getY() / 80) < 7){
        	 		/* Checks to see which brushSize to draw when the mouseDragged event is fired
        	 		 * Either a normal single cell up to 3 cells on each side
        	 		 */
        	 		if(brushSize > 0){
    		 			for(int i = -brushSize; i < brushSize; i++){
    		 				for(int j = -brushSize; j < brushSize; j++){
    		 					colors[(e.getX() / width)+i][(e.getY() / height)+j] = color.getColor();
    		 				}
    		 			}
    		 		}
    		 		else{
    		 			colors[e.getX() / width][e.getY() / height] = color.getColor();
    		 		} 		
        		}
        	}catch(Exception r){System.out.println(e.getY() / (getWidth()/32));}
        }
    	
    	public void zoomIn(){
    		if(zoom > -.99)
    			zoom -= .1;
    		setPreferredSize(new Dimension((int)(getWidth()*zoom *-1)+getWidth(), (int)(getHeight()* zoom * -1)+getHeight()));
    		System.out.println((int)((getWidth()*zoom *-1)+getWidth())+ (int)(getHeight()* zoom * -1));
    		revalidate();
    	}
    	
    	public void zoomOut(){
    		if(zoom < .99)
    			zoom += .1;
    		setPreferredSize(new Dimension((int)(getWidth()*zoom * -1)+getWidth(), (int)(getHeight()* zoom * -1)+getHeight()));
    		System.out.println((int)((getWidth()*zoom)+getWidth())+ (int)(getHeight()* zoom));
    		revalidate();
    	}
    	
    	/* Clear the canvas
    	 * 
    	 */
    	public void clear(){
    		for(int j = 0; j < 32; j++){
    			for(int i = 0; i < 32; i++){
    				colors[i][j] = Color.white;
    			}
    		}
    	}
    	
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		
    		width = (getWidth() / 32) - (int)((getWidth()/32)*zoom);
    		height = (getHeight() / 32) - (int)((getHeight()/32)*zoom);
    		/* Get the color from the picture and then draw it
    		 * 
    		 */
    		
    		for(int j = 0; j < 32; j++){
    			for(int i = 0; i < 32; i++){
    				if(colors[i][j] != null){
    					g.setColor(colors[i][j]);
    				}
    				else{
    					g.setColor(Color.WHITE);
    				}
    				g.fillRect(i * width, j * height, width, height);
    			}
    		}
    		
    		g.setColor(color.getColor());		
    		
    		/* Draw the brushSize of the brush
    		 * 
    		 */
    		if(brushSize > 0){
     			for(int i = -brushSize; i < brushSize; i++){
     				for(int j = -brushSize; j < brushSize; j++){
     					g.fillRect(((int)(x/width)*width)+(i*width), ((int)(y/height)* height)+(j*height), width, height);
     				}
     			}
     		}
     		else{
     			g.fillRect((int)(x/width)*width, (int)(y/height)* height, width, height);
     		} 	
    		
    		g.setColor(Color.blue);
    		g.drawLine(50, 50, 2000, 50);
    	}
    }
    
    
    


    package SpriteCreator;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JColorChooser;
    import javax.swing.JPanel;
    
    public class ColorChooser extends JPanel implements ActionListener{
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    
    	DrawPane panel;
    	
    	Color selectedColor = Color.red;
    	JButton colorButton, colorPicker, fill, zoomIn, zoomOut, clear, eraser;	
    	Color bgColor;
    	
    	public ColorChooser(){
    		colorButton = new JButton("Color");
    		colorPicker = new JButton("GetColor");
    		fill = new JButton("Fill");
    		zoomIn = new JButton("Zoom In");
    		zoomOut = new JButton("Zoom Out");
    		clear = new JButton("Clear");
    		eraser = new JButton("Eraser");
    		
    		colorPicker.addActionListener(this);
    		colorButton.addActionListener(this);
    		fill.addActionListener(this);
    		zoomIn.addActionListener(this);
    		zoomOut.addActionListener(this);
    		clear.addActionListener(this);
    		eraser.addActionListener(this);
    		
    		setLayout(new GridLayout(7,0, 20, 20));
    		
    		add(colorButton);
    		add(colorPicker);
    		add(fill);
    		add(zoomIn);
    		add(zoomOut);
    		add(clear);
    		add(eraser);
    	}
    	
    	public void setPanel(DrawPane panel){
    		this.panel = panel;
    	}
    	
    	public void setSelectedColor(Color select){
    		selectedColor = select;
    	}
    	
    	@Override
    	public void actionPerformed(ActionEvent e){
    	    // Args are parent component, title, initial color
    		Object o = e.getSource();
    		
    		panel.isPickUp(false);
    		panel.fill(false);
    		
    		if(o == colorButton)
    			bgColor = JColorChooser.showDialog(this,"Choose Color",selectedColor);
    	    
    	    if (bgColor != null && o == colorButton){
    	    	selectedColor = bgColor;
    	    }
    	    else if(o == colorPicker){
    	    	panel.isPickUp(true);
    	    }
    	    else if(o == fill){
    	    	panel.fill(true);
    	    }
    	    else if(o == zoomIn){
    	    	panel.zoomIn();
    	    }
    	    else if(o == zoomOut){
    	    	panel.zoomOut();
    	    }
    	    else if(o == clear){
    	    	panel.clear();
    	    }
    		else if(o == eraser){
    			selectedColor = Color.white;
    		}
    	    
    	    repaint();
    	}
    	
    	public Color getColor(){
    		return selectedColor;
    	}
    	
    	
    	/* Paint the Selected background behind the buttons
    	 * 
    	 */
    	@Override
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		g.setColor(selectedColor);
    		g.fillRect(0,0,90,600);
    	}
    }
    
    
    

My Information

Member Title:
D.I.C Head
Age:
25 years old
Birthday:
May 25, 1988
Gender:
Location:
Jacksonville, Fl
Full Name:
Justin
Programming Languages:
Java, some C

Contact Information

E-mail:
Private

Friends

crazedmeph hasn't added any friends yet.

Comments

crazedmeph has no profile comments yet. Why not say hello?