The Artima Developer Community
Sponsored Link

Java Answers Forum
paint()

2 replies on 1 page. Most recent reply: Apr 17, 2002 6:23 AM by Lynn Hollerman

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Lynn Hollerman

Posts: 67
Nickname: gmholler
Registered: Mar, 2002

paint() Posted: Apr 16, 2002 8:15 AM
Reply to this message Reply
Advertisement
Can anyone look at this code and tell me what I did wrong this time? This isn't behaving as I had thought it would - I wanted one character (a "p"" in this case) to be printed in a different color. I know there's a lot inelegant about this program - it just seemed like a nice example to start from. I figure I can make it pretty later!

I know the problem I'm having is somewhere in the paint() method - it's like that "if" statement isn't getting executed at all. Why not?

And the reason for that "APPLET" code at the top is a tip from a Java text, since I have to use the appletviewer to test applets (sysadmin won't load the Java plug-in for this browser until it's absolutely needed).

Here's the code:
/*
<APPLET CODE="assignment2.class" WIDTH=300 HEIGHT=300></APPLET>
*/
   
import java.util.*;
import java.applet.Applet;
import java.awt.*;    // import the java.awt package
import java.lang.String;
import java.awt.event.*;  // import the java.awt.event package
 
 
public class assignment2 extends Applet implements ActionListener,
MouseListener
    {
    Font f;
    String namearray = "     ", command = " ";
    TextField input;
    int xcord, ycord;
    int index;
   public void init()
   {
       setBackground(Color.blue);
       setForeground(Color.black);
       input = new TextField(20);
       add(input);
       input.addActionListener(this);
       f = new Font("Arial", Font.BOLD, 20);
   }
 
    public void start()
    {
 	//setSize(200,200);
     	setBackground(Color.white);
     	setForeground(Color.blue);
        addMouseListener(this);
    }
 
 
   public void actionPerformed( ActionEvent e )
   {
       command=e.getActionCommand();
       //System.out.println("command= "+command);
       repaint();
   }
 
    public void paint(Graphics g)
 {
    for (index=0; index < command.length(); index++)
          {
              String temp = " ";
              String c="p";
              temp+=command.charAt(index);
              if(temp.equals(c))
              {
               g.setColor(Color.red);
              }
              else 
              {
               g.setColor(Color.green);
              }
              System.out.println("temp ="+temp);//for debugging
              g.drawString(temp,xcord,ycord+10);//why "+10"? because it seemed like a good idea
              xcord = xcord+=10;
          }
  }
    // MouseListener event handlers
   public void mouseClicked( MouseEvent e )
   { 
   xcord=e.getX();
   ycord=e.getY();
   paint(getGraphics());
   repaint();
   }
 
   public void mousePressed( MouseEvent e ){}
   public void mouseReleased( MouseEvent e ){}
   public void mouseEntered( MouseEvent e ){}
   public void mouseExited( MouseEvent e ){}
}


Thanks in advance!

Lynn.


Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: paint() Posted: Apr 16, 2002 5:56 PM
Reply to this message Reply
It is not working as you expect it to and there is a valid reason behind it.

You are declaring temp = " " whereas you should be declaring it as temp = "".

temp+=command.charAt(index);              
if(temp.equals(c))              {               g.setColor(Color.red);              }              else               {               
g.setColor(Color.green);              
}


If you examine this part of code, you will notice that you are concatenating a character to temp. If the character you concatenate is 'p', then temp will have the value " p" (because of the space with which you initialized temp. Therefore your if condition fails.

Lynn Hollerman

Posts: 67
Nickname: gmholler
Registered: Mar, 2002

Re: paint() Posted: Apr 17, 2002 6:23 AM
Reply to this message Reply
NOW it works like I thought it would. Thank you!!! I was so worried about making sure I initialized everything, I totally overlooked what I was initializing things TO!

Thanks again!

Lynn.

Flat View: This topic has 2 replies on 1 page
Topic: inner classes Previous Topic   Next Topic Topic: How to position cursor while using JFileChooser?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use