Jump to content
  • Advertisement
Sign in to follow this  
monp

[Java] Cannot find symbol.

This topic is 2822 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!