T
T
Timofey Lanevich2016-06-15 13:10:23
Java
Timofey Lanevich, 2016-06-15 13:10:23

currentColor() java not working?

I started learning java from the book Java Programming The Definitive Guide for Professionals by Patrick Niemeyer.
I write code like in a book:

package simpleWindowMesssage.createButton;

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

public class HelloJava3 {
    public static void main (String [] args){
      JFrame frame = new JFrame("HelloJava3");
      frame.add(new HelloComponent3("Hello, Java !"));
      frame.setSize(300, 300);
      frame.setVisible(true);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
class HelloComponent3 extends JComponent
implements MouseMotionListener, ActionListener
{
  String theMessage;
  int messageX = 125, messageY = 95;
  JButton theButton;
  int colorIndex;
  static Color[] someColors = {
    Color.black, Color.red, Color.green, Color.blue, Color.magenta
  };
  public HelloComponent3(String message){
    theMessage = message;
    theButton = new JButton("Change Color");
    setLayout(new FlowLayout());
    add(theButton);
    theButton.addActionListener(this);
    addMouseMotionListener(this);
  }
  public void paintComponent(Graphics g){
    g.drawString(theMessage, messageX, messageY);
  }
  public void mouseDragged(MouseEvent e){
    messageX = e.getX();
    messageY = e.getY();
    repaint();
  }
  public void mouseMoved(MouseEvent e){ }
  public void actionPerformed(ActionEvent e){
    if(e.getSource() == theButton)
    changeColor();
  }
  synchronized private void changeColor(){
    if(++colorIndex == someColors.length)
    colorIndex = 0;
    setForeground(currentColor());  // здесь проблема с currentColor()
    repaint();
  }
}

2b1fb4337b0d43c58cfc7f239384a3c2.png5336f79c511b42a4aff21f33e4385d78.pnga3a44c490eef4b7ab5ef23dfdb47ae22.png
Like written in a book.
Help me please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Shkurenko, 2016-06-15
@Timak31

Github link
You don't have currentColor() method. Above is a link where it is present

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question