Sorry i couldn't make a SSCCE because it doesnt happen with small programs. my code is pretty long but here it is if any one can help.
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import jmfdemo.*;
import java.util.*;
public class LaserControll extends Frame implements ActionListener
{
//Setings
int vk[]=new int[9];
int samplePlus=100;
boolean vis=true;
int rePaint=1000;
int time=250;
int numP=5;
//End of Settings
private BufferedImage image1;
private FrameGrabber vision1;
private int threshold = 200;
private Color on = Color.white;
private Color off = Color.black;
int x,y;
Rectangle r1,r2,r3,r4,r5,r6,r7,r8,r9;
boolean found=false;
public void paint(Graphics g) {
if (image1 != null)
g.drawImage(image1, 10, 30, this);
}
public void load()
{
try{
File f=new File("Data");
Scanner sk=new Scanner(f);
for (int y=0;y<9;y++)
{sk.nextInt();
sk.nextInt();
sk.nextInt();
sk.nextInt();
vk[y]=sk.nextInt();
}
vis=sk.nextBoolean();
samplePlus=sk.nextInt();
rePaint=sk.nextInt();
time=sk.nextInt();
numP=sk.nextInt();}
catch(Exception q)
{System.out.println(q+"");
JOptionPane.showMessageDialog(null,"Error: Data File Cannot be Found","Error",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
public void update(Graphics g) {
paint(g);
}
public void getRects()throws IOException
{File f=new File("Data");
Scanner sc=new Scanner(f);
int x,y,w,h;
x=sc.nextInt();
y=sc.nextInt();
w=sc.nextInt();
h=sc.nextInt();
sc.nextInt();
r1=new Rectangle(x,y,w,h);
x=sc.nextInt();
y=sc.nextInt();
w=sc.nextInt();
h=sc.nextInt();
sc.nextInt();
r2=new Rectangle(x,y,w,h);
x=sc.nextInt();
y=sc.nextInt();
w=sc.nextInt();
h=sc.nextInt();
sc.nextInt();
r3=new Rectangle(x,y,w,h);
x=sc.nextInt();
y=sc.nextInt();
w=sc.nextInt();
h=sc.nextInt();
sc.nextInt();
r4=new Rectangle(x,y,w,h);
x=sc.nextInt();
y=sc.nextInt();
w=sc.nextInt();
h=sc.nextInt();
sc.nextInt();
r5=new Rectangle(x,y,w,h);
x=sc.nextInt();
y=sc.nextInt();
w=sc.nextInt();
h=sc.nextInt();
sc.nextInt();
r6=new Rectangle(x,y,w,h);
x=sc.nextInt();
y=sc.nextInt();
w=sc.nextInt();
h=sc.nextInt();
sc.nextInt();
r7=new Rectangle(x,y,w,h);
x=sc.nextInt();
y=sc.nextInt();
w=sc.nextInt();
h=sc.nextInt();
sc.nextInt();
r8=new Rectangle(x,y,w,h);
x=sc.nextInt();
y=sc.nextInt();
w=sc.nextInt();
h=sc.nextInt();
sc.nextInt();
r9=new Rectangle(x,y,w,h);
}
public int sample()
{int sample=image1.getRGB(x/2,y/2);
int sr,sg,sb;
Color sc=new Color(sample);
sr=sc.getRed();
sb=sc.getBlue();
sg=sc.getGreen();
sample=((299 * sr + 587 * sg + 114 * sb) / 1000);
return sample;
}
class WindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
public LaserControll()throws Exception {
Robot r =new Robot();
load();
addWindowListener(new WindowListener());
setTitle("Laser Control");
try {
vision1 = new FrameGrabber();
vision1.start();
}
catch(FrameGrabberException fge) {
System.out.println(fge.getMessage());
}
image1 = vision1.getBufferedImage();
setResizable(false);
Color col = new Color(229,229,229);
setBackground(col);
x=image1.getWidth()-1;
y=image1.getHeight()-1;
for (int i=0;i<5000;i++)
{
image1 = vision1.getBufferedImage();
repaint();
}
int sample=sample();
try{
getRects();}
catch(Exception z)
{
}
JButton hide=new JButton("Hide");
hide.setBounds(35,183,100,20);
hide.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e)
{
setAlwaysOnTop(true);
}
});
JButton rc=new JButton("Recalibrate");
rc.setBounds(35,160,100,20);
rc.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e)
{int sam=sample();
JOptionPane.showMessageDialog(null,"New Threshold set to: "+sam+samplePlus,"Recalibrated",JOptionPane.INFORMATION_MESSAGE);
}
});
JTextField status= new JTextField("");
status.setBounds(146,184,20,19);
status.setEditable(false);
setLayout(null);
add(status);
add(hide);
add(rc);
setSize(image1.getWidth(this) + 20, image1.getHeight(this) + 95);
setVisible(vis);
repaint();
while(true) {
image1 = vision1.getBufferedImage();
repaint();
try {
Thread.sleep(1);
}
catch(Exception e) {
System.out.println("Error");
}
for (int i=0;i<x;i=i+numP)
{
for (int j=0;j<y;j=j+numP)
{
int v=image1.getRGB(i,j);
Color c =new Color(v);
int red=c.getRed();
int green=c.getGreen();
int blue=c.getBlue();
int b=(299 * red + 587 * green + 114 * blue) / 1000;
if (b>sample+samplePlus)
{
if (r1.contains(i,j))
{
r.keyPress(vk[0]);
r.keyRelease(vk[0]);
status.setText("1");
found=true;}
if (r2.contains(i,j))
{
r.keyPress(vk[1]);
r.keyRelease(vk[1]);
status.setText("2");
found=true;}
if (r3.contains(i,j))
{ r.keyPress(vk[2]);
r.keyRelease(vk[2]);
status.setText("3");
found=true;}
if (r4.contains(i,j))
{ r.keyPress(vk[3]);
r.keyRelease(vk[3]);
status.setText("4");
found=true;}
if (r5.contains(i,j))
{ r.keyPress(vk[4]);
r.keyRelease(vk[4]);
status.setText("5");
found=true;}
if (r6.contains(i,j))
{ r.keyPress(vk[5]);
r.keyRelease(vk[5]);
status.setText("6");
found=true;}
if (r7.contains(i,j))
{ r.keyPress(vk[6]);
r.keyRelease(vk[6]);
status.setText("7");
found=true;}
if (r8.contains(i,j))
{ r.keyPress(vk[7]);
r.keyRelease(vk[7]);
status.setText("8");
found=true;}
if (r9.contains(i,j))
{ r.keyPress(vk[8]);
r.keyRelease(vk[8]);
status.setText("9");
found=true;}
if (found==true)
{
try
{
Thread.sleep(time);
}
catch(Exception e)
{
System.out.println("Error!");
}
sample=sample();
for (int z=0;z<rePaint;z++)
{
image1 = vision1.getBufferedImage();
repaint();
}
found=false;
}
}
}
}
setVisible(vis);
}
}
public void actionPerformed (ActionEvent e)
{
}
public static void main(String[] args)throws Exception {
LaserControll a=new LaserControll();
}
}
>
> Sorry i couldn't make a SSCCE because it doesnt happen with small programs. my code is pretty long but here it is if any one can help.
So you've proven it works.
So you start simple. Add a few components then test your program. If it works add a few more then test again. That way when you have a problem you know what you last changed and have isolated the problem. Then you can ask a real question. Don't write your entire program and then start testing it because you have no idea where the problem might be and we an not going to read through all your code to find the one little mistake.
Some tips:
1) Don't use AWT components in a Swing application. All your GUI components should start with a "J". ie. you should be using JFrame, not Frame
2) Don't override the update() or paint() methods. This is not necessary in a Swing application.