Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Some Simple Advice?

  1. #1
    Junior Member Diamond Fowl's Avatar
    Join Date
    Apr 2011
    Posts
    2
    My Mood
    Inspired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Some Simple Advice?

    Hey people, i'm new to this particular forum, and i'm not sure if this would be the correct place to ask this question or not. What i'm wondering is if this would be the right place to post my code and have people judge it and tell me if it would be a pretty good project to pursue and make it better or not.
    If this isn't the correct place then could anyone point me to where i would post.
    Other wise i'll check back again later to see if anyone says if this would be the right place to post and if it is then i'll place my code and ask you all to judge it.

    Thanks :]


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Some Simple Advice?

    Depends how much code there is. If you post too much, people will likely not look through it. However, if you provide manageable chunks (SSCCE) we can take a look at it and give you pointers (no pun intended).

  3. #3
    Junior Member Diamond Fowl's Avatar
    Join Date
    Apr 2011
    Posts
    2
    My Mood
    Inspired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Some Simple Advice?

    Alright, here it is , and it consists of two classes i have in different files, not sure if it's necessary to have them in seperate files though:

    What it's suppose to do is pop up a JFrame, and from there you can select a photo aka a portion of your screen that you would like to search for on the screen (Could be useful for some games), and once it finds it it moves your mouse to the "photo".
    Currently i havn't gotten it to work quite as i would like it to, but sometimes it comes really close and other times it is far off, so if anyone could post any advice on whats wrong on that it's be helpful (errors come in RunnerProgram.locatePhoto)

    So thanks for all who both judge to see if this would be good to keep of following, and to those who give advice on how to solve errors produced.


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JWindow;
     
    public class window extends JWindow implements MouseListener,
    		MouseMotionListener {
     
    	Robot rob;
    	Toolkit tk = Toolkit.getDefaultToolkit();
    	Image img, img2;
    	int x = 0, y = 0, width = 1, height = 1;
     
    	public window() {
    		setBackground(Color.RED);
    		addMouseMotionListener(this);
    		addMouseListener(this);
    		setSize(tk.getScreenSize().width, tk.getScreenSize().height - 50);
    		try {
    			rob = new Robot();
    		} catch (AWTException e) {
    		}
    	}
     
    	public void setVisible(boolean t) {
    		img = rob.createScreenCapture(new Rectangle(tk.getScreenSize().width,
    				tk.getScreenSize().height));
    		if (t)
    			super.setVisible(true);
    		else {
    			img2 = rob.createScreenCapture(new Rectangle(x+1, y+1, width-1, height-1));
    			super.setVisible(t);
    		}
    	}
     
    	public void paint(Graphics g) {
    		g.drawImage(img, 0, 0, this);
    		g.drawRect(x, y, width, height);
    	}
     
    	public Image getImage() {
    		return img2;
    	}
     
    	public void mouseClicked(MouseEvent e) {}
     
    	public void mousePressed(MouseEvent e) {
    		x = MouseInfo.getPointerInfo().getLocation().x;
    		y = MouseInfo.getPointerInfo().getLocation().y;
    		width = 0;
    		height = 0;
    		repaint();
    	}
     
    	public void mouseReleased(MouseEvent e) {
    		repaint();
    	}
     
    	public void mouseEntered(MouseEvent e) {}
     
    	public void mouseExited(MouseEvent e) {}
     
    	public void mouseDragged(MouseEvent e) {
    		width = MouseInfo.getPointerInfo().getLocation().x - x;
    		height = MouseInfo.getPointerInfo().getLocation().y - y;
    		repaint();
    	}
     
    	public void mouseMoved(MouseEvent e) {}
    }

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.util.ArrayList;
    import javax.swing.*;
     
    public class RunnerProgram extends JFrame implements ActionListener,
    		KeyListener {
     
    	BufferedImage photo = null, i;
    	Robot rob;
    	static boolean windowisup = false, bfollow = false;
    	Point point = new Point(500, 500);
    	static JFrame frame = new JFrame("Visual/Photo recognition");
    	JWindow p = new JWindow();
    	window w = new window();
    	double prob = 0;
    	static JButton getScreen = new JButton("Get Photo"), follow = new JButton(
    			"Follow Image"), followandclick = new JButton("Follow and Click");
     
    	Toolkit tk = Toolkit.getDefaultToolkit();
    	Timer following = new Timer(500, new ActionListener() {
    		public void actionPerformed(ActionEvent e) {
    			locatePhoto();
    			rob.mouseMove(point.x, point.y);
    			following.stop();
    		}
    	});
     
    	public RunnerProgram() {
    		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		try {
    			rob = new Robot();
    		} catch (AWTException e) {
    		}
    		follow.addActionListener(this);
    		getScreen.addActionListener(this);
    		followandclick.addActionListener(this);
    		frame.setLayout(new FlowLayout());
    		frame.setSize(tk.getScreenSize().width, 50);
    		frame.getContentPane().add(getScreen);
    		frame.getContentPane().add(follow);
    		frame.getContentPane().add(followandclick);
    		frame.setLocation(0, 0);
    		frame.pack();
    		frame.setVisible(true);
    		frame.setAlwaysOnTop(true);
    	}
     
    	protected void locatePhoto() {
    		photo = (BufferedImage) w.getImage();
    		if (photo == null)
    			return;
     
    		for (int q = 0; q < 1024; q++) {
    			for (int j = 0; j < 768; j++) {
    				if (areSimilar(photo, q, j)) {
    					System.out.println("Found One");
    					point = new Point(q + 5, j + 5);
    					return;
    				}
    			}
    		}
    		System.out.println("Hit edge of screen");
    		following.stop();
    	}
     
    	private boolean areSimilar(BufferedImage pic, int q, int j) {
    		prob = 0;
    		int c = pic.getRGB(0, 0);
    		int red = (c & 0x00ff0000) >> 16;
    		int green = (c & 0x0000ff00) >> 8;
    		int blue = c & 0x000000ff;
    		Color color = new Color(red, green, blue);
     
    		// if(q==50)
    		// System.out.println(color+" "+rob.getPixelColor(q,j));
    		if (!rob.getPixelColor(q, j).equals(color))
    			return false;
    		// System.out.println("Gets to this point");
    		BufferedImage pic2 = rob.createScreenCapture(new Rectangle(photo
    				.getHeight() + 100, photo.getWidth() + 100));
    		for (int i = 0; i < pic.getWidth(); i++)
    			for (int we = 0; we < pic.getHeight(); we++) {
    				// FIX THIS\/\/\/
    				c = pic.getRGB(i, we);
    				red = (c & 0x00ff0000) >> 16;
    				green = (c & 0x0000ff00) >> 8;
    				blue = c & 0x000000ff;
    				color = new Color(red, green, blue);
    				int a, b, g, d;
    				a = pic2.getRGB(i, we);
    				g = (a & 0x00ff0000) >> 16;
    				b = (a & 0x0000ff00) >> 8;
    				d = c & 0x000000ff;
    				if (!color.equals(new Color(g, b, d))) {
    					// System.out.println("Kicks Here");
    					return false;
    				} /*else {
    					// System.out.println("This looks the same");
    					prob += (pic.getHeight() * pic.getWidth() / 100);
    					if (prob >= 90)
    						return true;
    				}*/
     
    			}
    		System.out.println("Does");
    		return true;
    	}
     
    	public static void main(String args[]) {
    		new RunnerProgram();
    		window w = new window();
    	}
     
    	public void actionPerformed(ActionEvent e) {
    		if (e.getSource() == getScreen) {
    			windowisup = !windowisup;
    			if (windowisup) {
    				getScreen.setLabel("OK");
    				w.setVisible(true);
    			} else {
    				getScreen.setLabel("Get Photo");
    				w.setVisible(false);
    			}
    		}
    		if (e.getSource() == follow) {
    			bfollow = !bfollow;
    			if (bfollow)
    				following.start();
    			else
    				following.stop();
    		}
    	}
     
    	@Override
    	public void keyTyped(KeyEvent e) {
    		if (e.getSource().equals(KeyEvent.VK_SPACE))
    			following.stop();
    	}
     
    	@Override
    	public void keyPressed(KeyEvent e) {}
     
    	@Override
    	public void keyReleased(KeyEvent e) {}
    }

Similar Threads

  1. Need some serious advice
    By Rituparna in forum The Cafe
    Replies: 4
    Last Post: September 25th, 2013, 09:26 AM
  2. need for some advice
    By kasiopi in forum AWT / Java Swing
    Replies: 6
    Last Post: January 26th, 2011, 10:34 AM
  3. A little advice please:
    By SDKC in forum Java Theory & Questions
    Replies: 1
    Last Post: December 8th, 2010, 08:15 PM
  4. Programming advice
    By silverspoon34 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 6th, 2009, 04:19 PM
  5. i need some advice ....
    By mdstrauss in forum Java Theory & Questions
    Replies: 8
    Last Post: July 24th, 2009, 03:29 PM