Saturday, 21 May 2022

Write a java program to blink image on the Frame continuously.

 DOWNLOAD  SLIP6Q1



import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

/**
* Created by : Amar Ghugare
* created on : 21-05-2022 18:36
* Purpose of class :
*/
public class Slip6Q1 extends JComponent {
BufferedImage image;
boolean showImage;
int x = -1;
int y = -1;
Random r;

Slip6Q1() throws IOException {

// put your image reading code here..

image = new BufferedImage(32,32,BufferedImage.TYPE_INT_ARGB);
Graphics g = image.createGraphics();
g.setColor(Color.ORANGE);
g.fillOval(0,0,32,32);

r = new Random();
ActionListener listener = new ActionListener(){
public void actionPerformed(ActionEvent ae) {
if (image!=null) {
if (!showImage) {
int w = image.getWidth();
int h = image.getHeight();
int rx = getWidth()-w;
int ry = getHeight()-h;
if (rx>-1 && ry>-1) {
x = r.nextInt(rx);
y = r.nextInt(ry);
}
}
showImage = !showImage;
repaint();
}
}
};
Timer timer = new Timer(600,listener);
timer.start();
setPreferredSize(new Dimension(150,100));
JOptionPane.showMessageDialog(null, this);
timer.stop();
}

public void paintComponent(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(0,0,getWidth(),getHeight());
if (showImage && image!=null) {
g.drawImage(image,x,y,this);
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
new Slip6Q1();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}

No comments:

Post a Comment

Write a Java program to display given extension files from a specific directory on server machine.

 DOWNLOAD     SLIP14Q2 /** * STEPS TO RUN CODE * Step 01 compile the code * Step 02 run the code * Step 03 give a file directory locatio...