i can get the background but i dont know how to move the fishes package newv; im
ID: 3635576 • Letter: I
Question
i can get the background but i dont know how to move the fishes
package newv;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MainGUI extends JFrame {
private JFrame frame;
private JPanel panel;
private JButton jbStartGame, jbHelp, jbExit;
Dimension dimPanel = null;
private JMenuItem newPlay, exit, about;
private JLabel jlIcon[];
private int distance=1;
/**
* MainGUI default constructor
*/
public MainGUI() {
jlIcon = new JLabel[3];
dimPanel = getSize();
/**
* set options for JFrame
*/
final BackgroundImage bp = new BackgroundImage();
// Menubar & items setup
JMenuBar jmb = new JMenuBar();
newPlay = new JMenuItem("New Game");
JMenu file = new JMenu("Game");
JMenu help = new JMenu("Help");
exit = new JMenuItem("Exit");
about = new JMenuItem("About");
file.add(newPlay);
file.add(exit);
help.add(about);
jmb.add(file);
jmb.add(help);
setJMenuBar(jmb);
setTitle("Fish Tank");
setSize(600, 380);
setLocationRelativeTo(null);
setResizable(false);
jlIcon[0] = new JLabel(new ImageIcon("fish1.gif"));
bp.add(jlIcon[0]);
//Create a timer
int delay = 10; //milliseconds ... 67 is approximately 15 frames/second
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//...Perform a task...
//move the icon
int x = jlIcon[0].getX();
int y = jlIcon[0].getY();
/*
For the fish to move from left to right across the screen
we need to add positive numbers to the x location. For the
to move right to left across the screen we need to add
negative numbers to the x location.
To decide which direction the fish moves, we need to know if
the fish has reached the boundary of the frame. When the fish
reaches a boundary, multiply the distance to move by (-1).
*/
if ((x <= -(jlIcon[0].getSize().getWidth()))) {
x=(int) bp.getWidth();
}
jlIcon[0].setLocation(x - distance, y);
}
};
new javax.swing.Timer(delay, taskPerformer).start();
jlIcon[1] = new JLabel(new ImageIcon("fish2.gif"));
bp.add(jlIcon[1]);
ActionListener taskPerformer1 = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//...Perform a task...
//move the icon
int x = jlIcon[1].getX();
int y = 200;
/*
For the fish to move from left to right across the screen
we need to add positive numbers to the x location. For the
to move right to left across the screen we need to add
negative numbers to the x location.
To decide which direction the fish moves, we need to know if
the fish has reached the boundary of the frame. When the fish
reaches a boundary, multiply the distance to move by (-1).
*/
if ((x >= (bp.getWidth()))) {
x=(int) -jlIcon[1].getSize().getWidth();
System.out.println(""+jlIcon[1].getSize().getWidth());
}
jlIcon[1].setLocation(x + distance, y);
}
};
new javax.swing.Timer(delay, taskPerformer1).start();
jlIcon[2] = new JLabel(new ImageIcon("fish3.gif"));
bp.add(jlIcon[2]);
ActionListener taskPerformer2 = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//...Perform a task...
//move the icon
int x = jlIcon[2].getX();
int y = 110;
/*
For the fish to move from left to right across the screen
we need to add positive numbers to the x location. For the
to move right to left across the screen we need to add
negative numbers to the x location.
To decide which direction the fish moves, we need to know if
the fish has reached the boundary of the frame. When the fish
reaches a boundary, multiply the distance to move by (-1).
*/
if ((x >= (bp.getWidth()))) {
x=(int) -jlIcon[2].getSize().getWidth();
System.out.println(""+jlIcon[2].getSize().getWidth());
}
jlIcon[2].setLocation(x + distance, y);
}
};
new javax.swing.Timer(delay, taskPerformer2).start();
add(bp);
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
});
newPlay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
}
});
about.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JOptionPane.showMessageDialog(MainGUI.this, "Fish -Tank" + " "
+ "sssss " + "sss");
}
});
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
/**
* BackgroundImage class to set background image
*/
class BackgroundImage extends JPanel {
Image water;
/**
* default constructor
*
* @exception e
*/
public BackgroundImage() {
try {
/**
* create image icon
*/
water = ImageIO.read(new File("lightblue.gif"));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Paint component method
*
* @param g
*/
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(water, 0, 0, this.getWidth(), this.getHeight(), this);
}
}
public static void main(String[] args) {
MainGUI mg = new MainGUI();
}
}
Explanation / Answer
package aquarium.gui;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Animation extends JFrame {
private static final long serialVersionUID = 1L;
private Aquarium aquarium = null;
private final int MARGE = 10;
public Animation(Aquarium aquarium) {
this.aquarium = aquarium;
}
/**
* La fonction d'affichage utilisée dans start
*/
public void afficher() {
add(aquarium);
// size
setSize(aquarium.getSizeX()+MARGE, aquarium.getSizeY()+MARGE);
// Optional: Ask for window decorations provided by the look and feel.
setDefaultLookAndFeelDecorated(true);
//2. Optional: What happens when the frame closes?
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
//public abstract class WindowAdapter
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//super.windowClosing(e);
int reponse = JOptionPane.showConfirmDialog(Animation.this, "Etes-vous sûr de vouloir quitter ?", "aquarium", JOptionPane.YES_NO_OPTION);
if (reponse == JOptionPane.OK_OPTION) {
Animation.this.dispose();
System.exit(0);
}
}
});
//4. Size the frame.
//pack();
//5. Show it.
setVisible(true);
while(true) {
aquarium.go();
try {
Thread.sleep(100);
} catch (InterruptedException e) {};
}
}
}
or u can try with the following
public class FishMain
{
public static void main (String[] args)
{
final int moves = 10;
Enviroment env = new Enviroment();
// Uncomment the lines below if using objects
//Fish fish1 = new Fish();
//env.addFish(fish1); //Adds a Fish object to the tank.
env.header();
env.display();
for (int h = 1; h <= moves; h++)
{
env.move();
env.display();
}
System.out.println("Number of bumps: " + env.getBumps());
System.out.println("----------------------------- ");
}
}
public class Enviroment
{
static final int env_rows = 1;
static final int env_cols = 5;
String[][] boundedEnv = new String[env_rows][env_cols];
String[] direction = {"left","right","up","down"};
static int bumps = 0;
static int loc = 0;
static int curloc = 0;
static int firstRun = 0;
public Enviroment()
{
for (int fill = 0; fill < 5; fill++)
boundedEnv[0][fill] = " ";
// Sets fish intitially in tank
curloc = (int)(Math.random() * 5);
boundedEnv[0][curloc] = ">=)";
firstRun = 1;
}
public void clear()
{
for (int clr = 0; clr < 5; clr++)
boundedEnv[0][clr] = " ";
}
public void display()
{
System.out.println("===============");
for (int i = 0; i < 5; i++)
System.out.print(boundedEnv[0][i]);
System.out.println(" ===============");
// Check to see if this run is the initial placement
if (firstRun != 1)
System.out.println("Position: " + direction[getLocation()] + " ");
else
{
System.out.println("***Initial Position***");
firstRun = 0;
}
}
public void header()
{
System.out.println("Fish Tank:");
}
public void move()
{
loc = (int)(Math.random() * 4);
if (loc == 0) //left
{
clear();
if (curloc != 0)
{
curloc -= 1;
boundedEnv[0][curloc] = ">=)";
}
else
{
boundedEnv[0][curloc] = ">=)";
bumps++;
}
}
else if (loc == 1) //right
{
clear();
if (curloc != 4)
{
curloc += 1;
boundedEnv[0][curloc] = ">=)";
}
else
{
boundedEnv[0][curloc] = ">=)";
bumps++;
}
}
else if (loc == 2) //up
{
clear();
boundedEnv[0][curloc] = ">=)";
bumps++;
}
else if (loc == 3) //down
{
clear();
boundedEnv[0][curloc] = ">=)";
bumps++;
}
}
public int getLocation()
{
return loc;
}
public int getBumps()
{
return bumps;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.