Sign in to follow this  
monp

[Java] Cannot find symbol.

Recommended Posts

I am still following the book and cannot compile this into a class because I am receiving a cannot find symbol error for Thread t = new Thread(this);


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class GuiCore2 extends JComponent implements MouseMotionListener, ActionListener
{
String message;
int m_x = 125;
int m_y = 95;

JButton button;

int colorIndex;
static Color[] colors = {Color.black, Color.red, Color.blue, Color.green};
boolean blinkState;

public GuiCore2(String msg)
{
message = msg;
button = new JButton("Change Colors");
setLayout(new FlowLayout());
add(button);
button.addActionListener(this);
addMouseMotionListener(this);
Thread t = new Thread(this);
t.start();
}

public void paintComponent(Graphics g)
{
g.setColor(blinkState ? getBackground() : currentColor());
g.drawString(message, m_x, m_y);
}

public void mouseDragged(MouseEvent e)
{
m_x = e.getX();
m_y = e.getY();
}

public void mouseMoved(MouseEvent e)
{
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource() == button)
{
changeColor();
}
}

synchronized private void changeColor()
{
if(++colorIndex == colors.length)
{
colorIndex = 0;
}
setForeground(currentColor());
repaint();

}

synchronized private Color currentColor()
{
return colors[colorIndex];
}

public void run()
{
try
{
while(true)
{
blinkState = !blinkState;
repaint();
Thread.sleep(300);
}
}
catch(InterruptedException e)
{
}
}
}

Share this post


Link to post
Share on other sites
Your GuiCore2 class should implement Runnable - Thread's constructor takes an instance of Runnable as its first argument.

Runnable declares the "public void run" method which you have already implemented.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this