Solved

Picture as button

Posted on 2003-12-02
19
224 Views
Last Modified: 2010-03-31
Hey can anyone help me with adding a picture as a button to a java program, the picture is "pic.jpg".  When the button is pressed a value is saved in a string variable called "picValue".
Any help on how to do this would be great,
cheers.
Raftor
0
Comment
Question by:Raftor
  • 7
  • 6
  • 6
19 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9860907
JButton b = JButton(new ImageIcon(
"pic.jpg"));

What is the 'value' you refer to about?
0
 
LVL 92

Expert Comment

by:objects
ID: 9860995
JButton b = JButton(new ImageIcon("pic.jpg"));
b.addActionListener(new ActionListener()
   { public void actionPerformed(ActionEvent e) { picValue = getValueFromSomewhere(); }});

0
 

Author Comment

by:Raftor
ID: 9861416
There are three buttons, the user has to choose one and when he does a string  "pic1", "pic2", or "pic3"  is stored in "value" depending on which was pressed.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9861449
Make you main class implement ActionListener and then

public void actionPerformed(ActionEvent e) {
  Object button = e.getSource();
  if (button == b1) {
     value = "pic1";
   }
  else if (button == b2
  ...
}

if you get the idea
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 9861463
JButton b1 = JButton(new ImageIcon("pic.jpg"));
b1.addActionListener(new ActionListener()
   { public void actionPerformed(ActionEvent e) { picValue = "pic1"; }});
JButton b2 = JButton(new ImageIcon("pic.jpg"));
b1.addActionListener(new ActionListener()
   { public void actionPerformed(ActionEvent e) { picValue = "pic2"; }});
JButton b3 = JButton(new ImageIcon("pic.jpg"));
b1.addActionListener(new ActionListener()
   { public void actionPerformed(ActionEvent e) { picValue = "pic3"; }});
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
ID: 9861554
You can use the JButton's name property to do this:

 JButton b1 = JButton(new ImageIcon("pic.jpg"));
  b1.setName("pic1");
  b1.addActionListener(this);
 
  ...
 
 
  public void actionPerformed(ActionEvent e) {
    Object o = null;
    if ((o = e.getSource()) instanceof JButton) {
      value = ((Component)o).getName();
    }
  }
0
 

Author Comment

by:Raftor
ID: 9861572
Also is it possible to return strings from threads in java?
This is my header, how do i define it to return a string?

public class control extends Thread{
    ....
    ...
    return(string);
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9861601
You need an instance variable and an accessor

String x;

public String getX() {
   return x;
}
0
 

Author Comment

by:Raftor
ID: 9861609
Is this kind of thing possible? If not how would you go about it?

String playerOneResponse = control.start();
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Raftor
ID: 9861622
CEHJ do i do that in a simple function inside the tread?  I though i would have to have a string return type on the class header!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9861637
Yes, instance function:

control.start();
String x = control.getX();
0
 
LVL 92

Expert Comment

by:objects
ID: 9861766
public class control extends Thread{
   private String S;
    ....
    ...
    public String getS() {
       return S;
    }
}

How exactly is the string being set and used?
0
 

Author Comment

by:Raftor
ID: 9863115
Whats wrong with this code?


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class evaluateChoices()
{
      public PrintWriter playerOneWrite,playerTwoWrite;
      public int returnWinner(String P1Choice, String P2Choice,Socket P1Socket, Socket P2Socket)
      {
            playerOneWrite = new PrintWriter(player1.getOutputStream(), true);
            int returnValue = 0;//1 means player1 wins 2 means player 2 wins 0 means draw
            if(P1Choice.equalsIgnoreCase(P2Choice))
            {
                  returnValue = 0;
            }
            else if((P1Choice.equalsIgnoreCase("rock")) && (P2Choice.equalsIgnoreCase("scissors")))
            {
                  returnValue = 1;
            }
                  
            return(returnValue);
      }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9863139
Can we have one question at a time please? ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 9863144
>          playerOneWrite = new PrintWriter(player1.getOutputStream(), true);

variable player1 is never defined.
0
 
LVL 92

Expert Comment

by:objects
ID: 9863146
Should probably be P1Socket
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9863466
:-)
0
 

Author Comment

by:Raftor
ID: 9863509
cheers lads :-)
0
 
LVL 92

Expert Comment

by:objects
ID: 9863518
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Systems talking to each other 5 123
java  and programming certification ? 4 86
stackato and cloud 4 64
even odd program using while loop 3 25
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now