I am having trouble getting my JButton to generate images to match my randomized
ID: 643276 • Letter: I
Question
I am having trouble getting my JButton to generate images to match my randomized text. I have an ActionListener set up but my but cannot figure out how to get it to access variables in another class. See my work below:
\myJPanel6.java
//import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class myJPanel6 extends JPanel implements ActionListener
{
myJButton b1,b2;
JLabel l1,l2, l3, l4;
public myJPanel6()
{
setLayout(new GridLayout(1,1));
//=====================================
student st1 = new student("Michael", "Robinson", 20);
//=====================================
b1 = new myJButton(st1.getName());
b1.addActionListener(this);
add(b1);
//=====================================
b2 = new myJButton(st1.WhatIsUp());
// ImageIcon imagereading = new ImageIcon("images/reading.jpg");
add(b2);
//=====================================
ImageIcon imagereading = new ImageIcon("images/reading.jpg");
l1 = new JLabel(imagereading);
add(l1);
//=====================================
ImageIcon imagetalking = new ImageIcon("images/talking.jpg");
l2 = new JLabel(imagetalking);
add(l2);
//=====================================
ImageIcon imageinteracting = new ImageIcon("images/interacting.jpg");
l3 = new JLabel(imageinteracting);
add(l3);
//========================================
// l4 = new JLabel(".......");
// add(l4);
}
@Override
public void actionPerformed(ActionEvent event)
{
Object obj = event.getSource();
//=====================================
// If(obj == b1);
//if (obj == b2){l1.setText("-"+b1.getText()+"- was clicked");}
// l1.setText("button was clicked");
if (obj == b2){l1.setImage(l1.getImage());}
}
}
--------------------------------------------------------------------------------
\student.java
import javax.swing.ImageIcon;
import javax.swing.JLabel;
class student
{
//---------Declaring attributes----
String firstName;
String lastName;
int age;
String status;
//------------------------------
private Object b2;
private Object reading;
//----------Constrcutor------------
public student(String a, String b, int c)
{
firstName = a;
lastName = b;
age = c;
if(age<=25) status = "Traditional";
else status = "Non-Traditional";
}
//---------------------------------
//---------- METHODS --------
String getName()
{
return("NAME = "+firstName+ " "+lastName + ", Age = "+age+", Status = "+status);
}
int getAge()
{
return age;
}
String getStatus()
{
return status;
}
String WhatIsUp()
{
String b = "dunno";
double r = Math.random();
int myNumber = (int)(r*3f);
if (myNumber == 0) b = "reading";
// JLabel MyImage = new JLabel(new ImageIcon("images/reading.jpg"));
if (myNumber == 1) b = "talking";
{
ImageIcon imagetalking;
// imagetalking = new ImageIcon("images/talking.jpg");
}
if (myNumber == 2) b = "interacting";
ImageIcon imageinteracting = new ImageIcon("images/interacting.jpg");
return b;
}
//--------------------------------------------
}
Explanation / Answer
you can try this code
public class MyJPanel6 extends Action listener
{
private final File FILECLOSEBUTTON = new File("img\boutonrondX.png"); //My round button with transparent corners
private JButton closeButton = new JButton(); //Creating it empty to be able to place it and resize the image after the button size is known
public MyJPanel6() throws IOException
{
setLayout(null);
setSize(150, 150);
closeButton.setSize(100, 100);
closeButton.setContentAreaFilled(false);
closeButton.setBorderPainted(false);
add(closeButton);
closeButton.addMouseListener(new MouseListener()
{
public void mouseClicked(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
System.out.println("Alpha value of pixel (" + e.getX() + ", " + e.getY() + ") is: " + clickAlphaValue(closeButton.getIcon(), e.getX(), e.getY()));
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
});
Image imgCloseButton = ImageIO.read(FILECLOSEBUTTON);
//Resize the image to fit the button
Image newImg = imgCloseButton.getScaledInstance((int)closeButton.getSize().getWidth(), (int)closeButton.getSize().getHeight(), java.awt.Image.SCALE_SMOOTH);
closeButton.setIcon(new ImageIcon(newImg));
}
private int clickAlphaValue(Icon icon, int posX, int posY)
{
int width = icon.getIconWidth();
int height = icon.getIconHeight();
BufferedImage tempImage = (BufferedImage)createImage(width, height);
Graphics2D g = tempImage.createGraphics();
icon.paintIcon(null, g, 0, 0);
g.dispose();
int alpha = (tempImage.getRGB(posX, posY) >> 24) & 0x000000FF;
return alpha;
}
public static void main(String[] args)
{
try
{
MyJPanel6 myJPanel6 = new MyJPanel6();
myJPanel6.setVisible(true);
myJPanel6.setDefaultCloseOperation(Action listener.EXIT_ON_CLOSE);
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
public class MyJPanel6 extends Action listener
{
private final File FILECLOSEBUTTON = new File("img\boutonrondX.png"); //My round button with transparent corners
private JButton closeButton = new JButton(); //Creating it empty to be able to place it and resize the image after the button size is known
public MyJPanel6() throws IOException
{
setLayout(null);
setSize(150, 150);
closeButton.setSize(100, 100);
closeButton.setContentAreaFilled(false);
closeButton.setBorderPainted(false);
add(closeButton);
closeButton.addMouseListener(new MouseListener()
{
public void mouseClicked(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
System.out.println("Alpha value of pixel (" + e.getX() + ", " + e.getY() + ") is: " + clickAlphaValue(closeButton.getIcon(), e.getX(), e.getY()));
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
});
Image imgCloseButton = ImageIO.read(FILECLOSEBUTTON);
//Resize the image to fit the button
Image newImg = imgCloseButton.getScaledInstance((int)closeButton.getSize().getWidth(), (int)closeButton.getSize().getHeight(), java.awt.Image.SCALE_SMOOTH);
closeButton.setIcon(new ImageIcon(newImg));
}
private int clickAlphaValue(Icon icon, int posX, int posY)
{
int width = icon.getIconWidth();
int height = icon.getIconHeight();
BufferedImage tempImage = (BufferedImage)createImage(width, height);
Graphics2D g = tempImage.createGraphics();
icon.paintIcon(null, g, 0, 0);
g.dispose();
int alpha = (tempImage.getRGB(posX, posY) >> 24) & 0x000000FF;
return alpha;
}
public static void main(String[] args)
{
try
{
MyJPanel6 myJPanel6 = new MyJPanel6();
myJPanel6.setVisible(true);
myJPanel6.setDefaultCloseOperation(Action listener.EXIT_ON_CLOSE);
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.