So I need to display images when the button is clicked upon. When I click on the button it displays nothing. Is it possible to pass a parameter into void paint method so that I can check the condition using if condition? If square button is clicked it should print square and if circle button is clicked it should print circle
import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Jungle extends Applet implements ActionListener{
Button n;
Button o;
public void init()
{
n=new Button("Square");
add(n);
n.addActionListener(this);
o = new Button("circle");
add(o);
o.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==n)
{
repaint();
};
}
class Delta
{
void graphics(Graphics g)
{
g.drawRect(40, 40, 20, 20);
g.fillRect(40, 40, 20, 20);
}
}
}
答案 0 :(得分:1)
..这样我就可以使用条件来检查条件..
将图像声明为类属性,并将其值设置为null
:
Image img = null;
单击该按钮时,将图像设置为等于要显示的图像。
在paint(Graphics)
方法中,在绘制图像之前检查null。 E.G:
if (img!=null) {
g.drawImage(img, 0, 0, this);
}