Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,160,399 members, 7,843,166 topics. Date: Tuesday, 28 May 2024 at 07:43 PM

Java Cafe - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Java Cafe (2326 Views)

Wifi Timer Software For Cafe Biz / Please I Need An Original Eassy Browsing Cyber Cafe Timer / Cafe Timer Help (2) (3) (4)

(1) (2) (Reply) (Go Down)

Java Cafe by Dolemite(f): 7:00pm On May 14, 2011
I started this topic so people with problems can get help on problems concerning Java. . .

I'll begin, I have this code that creates a box, I want to make this box move when buttons are pressed but it doesn't work. . . There are two classes, the main class MovingBox and an inner class move. . .
public class MovingBox extends JFrame
{
    private Rectangle box = new Rectangle(250,300,90,95);
    private  int UP=0,DOWN=1,LEFT=2,RIGHT=3;
    private int boxDirection;
    public MovingBox(){
        super("Moving Box"wink;
        setSize(400,400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        move m1 = new move();
        m1.move();
    }
     public void paint(Graphics g){
      super.paint(g);
      g.setColor(Color.YELLOW);
      g.fillRect(box.x,box.y,box.width,box.height);
      }
      public class move implements KeyListener{
      public void move(){
          addKeyListener(this);
        }
      public void keyPressed(KeyEvent e){}
      public void keyReleased(KeyEvent e){}
      public void keyTyped(KeyEvent e){
        if(e.getKeyChar()=='a')
     {
     boxDirection = LEFT;
     }
     if(e.getKeyChar()=='s')
     {
     boxDirection = DOWN;
     }
    if(e.getKeyChar()=='d')
    {
    boxDirection = RIGHT;
     }
     
     if(e.getKeyChar()=='w')
     {
       boxDirection = UP;
     
     }
    }
}
}
Re: Java Cafe by Fayimora(m): 10:10pm On May 14, 2011
Do you want it to move gradually?I mean you see the way it moves or you just want it to disappear and reappear somewhere else,
Re: Java Cafe by Shimao(m): 11:19pm On May 14, 2011
Though there are issues with the structure of your code, there actually isnt any code that moves your box. For you to simulate movement in code, you need to change either the x or y or both coordinate of your target object. Then ofcourse, have the GUI repainted.
Re: Java Cafe by Dolemite(f): 1:26am On May 15, 2011
Shimao:

Though there are issues with the structure of your code
What are these issues?
Re: Java Cafe by csharpjava(m): 1:27am On May 15, 2011
Dolemite

The code should look like this:


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class MovingBox extends JFrame implements ActionListener
{

private boolean isRight = true;
private JButton MoveLeftButton = new JButton("Moving Box Right"wink;
private JButton MoveRightButton = new JButton("Moving Box Left"wink;

public MovingBox()
{
       
     setTitle("Moving Box"wink;
setLayout(new FlowLayout());
add(MoveLeftButton);
add(MoveRightButton);
setSize(500,200);
setLocation(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MoveRightButton.addActionListener(this);
MoveLeftButton.addActionListener(this);
getContentPane().setBackground(Color.yellow);
setVisible(true);
    }
   
public void paint(Graphics g)
{
super.paint(g);

if(isRight == true)
{
g.drawRect(100,100,90,90);
g.setColor(Color.blue);
g.fillRect(100,100,90,90);
   

}
else
{
g.drawRect(300,100,90,90);
g.setColor(Color.blue);
g.fillRect(300,100,90,90);;
     

}
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource() == MoveRightButton)
{
isRight = true;
repaint();
}

if(e.getSource() == MoveLeftButton)
{
isRight = false;
repaint();
}
}
}


public class runMovingBox {

public static void main(String[] args) {

new MovingBox();
}
}
Re: Java Cafe by Shimao(m): 5:42am On May 15, 2011
csharpjava has addressed some of it but it could still be better.
Declare the initial position of your box and its dimension as variables, so also the distance to move with each key press.
Use these as arguments to draw the box.
I believe you intend to use a KeyListener, in your event handling code, use a switch to check which key was pressed and increment or decrement the initial x or y position as it would apply, then repaint.
Your class need not implement an event handling interface as you may be giving it unintended responsibility.
Re: Java Cafe by Shimao(m): 6:37am On May 15, 2011
Check out this sample implementation code.

Re: Java Cafe by Dolemite(f): 3:33pm On May 15, 2011
Thanks guys, I knew you geniuses will never let me down. . .at this rate i'll be a genius like you guys in no time, lol. grin
Re: Java Cafe by Ajistotle(m): 5:18pm On Jun 10, 2011
Evry1 keeps talkin of programming, IDE's and co. After writing a program e.g a java program to add two numbers,how do I turn it into a stand alone application? i.e package it completely and run it on another person's computer
Re: Java Cafe by Fayimora(m): 5:26pm On Jun 10, 2011
Well firstly you have to code it. Secondly you need to implement a user interface and thirdly you deploy. You have to go tru those stages. I dnt think i shud explain more as if you research well on each of these stages you would understand what's up.

No offence but it seems to be that you cannot even write d code to do the 1+1. So get to it cheesy
Re: Java Cafe by SayoMarvel(m): 10:10pm On Jun 10, 2011
For more info on smooth movement (of your box), study FILTHY RICH CLIENTS; I can't provide lengthy code (working on A̷̷̴ mobile phone) but Swing contains a timing framework that facilitates that. Thumbs up guys.
Re: Java Cafe by Ajistotle(m): 6:04pm On Jun 13, 2011
:d
Re: Java Cafe by Ajistotle(m): 6:57pm On Jun 13, 2011
cheesy fayimora, I assure U I can write code to do that. Thanks for the reply
Re: Java Cafe by Ajistotle(m): 7:02pm On Jun 13, 2011
cheesy fayimora, I assure U I can write code to do that. Thanks for the reply
Re: Java Cafe by Fayimora(m): 7:03pm On Jun 13, 2011
Aiit cool.lol What language you developing with?
Re: Java Cafe by Ajistotle(m): 8:04pm On Jun 13, 2011
Java. And by d way, I've been stuck on dis 4 a while. Goes lyk dis; write an application that prompts the user to enter the side of a square, then displays a hollow square of dat size made of astericks. Your program should work for squares of all side lengths beteen 1 and 20. I saw it in a java textbook and I cant seem to come up with a working algorithm. Can you help?
Re: Java Cafe by Fayimora(m): 9:20pm On Jun 13, 2011
Sounds interesting, Well you obviously gonna use 2 for loops. One that would print in x number of times on a line and another to print it x number of times vertically.

Have you tried it yet?
Re: Java Cafe by Ajistotle(m): 10:48am On Jun 14, 2011
I've tried it using while loops and that's because the question is under the while loop section in the textbook.
Re: Java Cafe by Fayimora(m): 4:03pm On Jun 14, 2011
in this case it rely doesnt matter, Lemme see what you have
Re: Java Cafe by Danyl(m): 5:00pm On Jun 16, 2011
@ajistotle
i'll suggest that u package ur code's main class and helper classes(if any) into a .jar file then create an .exe version of ur program by using exe4j software jar-exe executable file converter.u are goin to need a manifest file for that.u can create an installer for it using inno-setup compiler and instal it.by so doing u'll have it as a desktop application.
I hope that helps
Re: Java Cafe by Dolemite(f): 7:45pm On Jun 18, 2011
public class User
{
private static int identifier = 100;
private String userName;
private int storageCapacity = 1000;
private int availableSpace;
private File f;
private String name;
private String date;
private String type;
private String size;
HashMap hm = new HashMap();
Collection c = hm.values();

public User(String userNameIn){
userName = userNameIn;
identifier--;
}
public void addFile(String nameIn, String dateIn, String typeIn, String sizeIn){
name = nameIn;
date = dateIn;
type = typeIn;
size = sizeIn;
f = new File (name, date, type,size);
hm.put(type,f);

}
public void searchFilesByType(String type){
Set<String> theKeys = hm.keySet();
if(theKeys.equals(type)){
System.out.println(hm.get(type));
}


}
public String getUserName(){
return userName;
}

}
I am trying to use the 'searchfilesbytype' method to iterate through the hashmap and produce values that are identical to those inputted by the user, but I'm getting an undesirable result.
Re: Java Cafe by Dolemite(f): 9:04pm On Jun 18, 2011
public class User
{
private static int identifier = 100;
private String userName;
private int storageCapacity = 1000;
private int availableSpace;
private FileSys f;
private String name;
private String date;
private String type;
private String size;
private Map<String, FileSys>hm;


public User(String userNameIn){
userName = userNameIn;
hm = new HashMap<String, FileSys>();
identifier--;
}
public void addFile(String nameIn, String dateIn, String typeIn, String sizeIn){
name = nameIn;
date = dateIn;
type = typeIn;
size = sizeIn;
f = new FileSys(name, date, type,size);
hm.put(type,f);

}
public void searchFilesByType(String typeIn){
Iterator<String> i = hm.iterator();
while(i.hasNext()){
if(ee.equals(typeIn)) {
System.out.println(hm.get(type));


}
}
}

public String getUserName(){
return userName;
}

}
I am trying to use the 'searchfilesbytype' method to iterate through the hashmap and produce values that are identical to those inputted by the user, but I'm getting an undesirable result.
Re: Java Cafe by Shimao(m): 7:16am On Jun 19, 2011
Your first approach was better. You are using a hashmap to store values so you'll need a keyset to iterate through it. After getting the keyset, use a for, Loop to iterate through the keyset (cos the keyset is a collection of all the keys used to index values in your hashmap) while matching the current key with the supplied key. Once you get a match, break out of the loop. You can handle a no match situation by throwing an exception. Also for your addFile method, you do not need instance variables to hold arguments passed in to your method. You can work directly with the values.
Re: Java Cafe by Dolemite(f): 5:26pm On Jun 19, 2011
public void addFile(String nameIn, String dateIn, String typeIn, int sizeIn){
name = nameIn;
date = dateIn;
type = typeIn;
size = sizeIn;
f = new FileSys(name, date, type,size);
totalSize=totalSize+size;
if(availableSpace>0){
availableSpace= storageCapacity-totalSize;
file.add(f);
}
else if(file.contains(nameIn)){
System.out.println("A file with that name already exists"wink;
}
else{
System.out.println("There is no space"wink;
}
}
The problem here is I want to check for similarity with the files, where a file with a similar filename would not be added. . .is it possible with an arraylist?
Re: Java Cafe by wassolldas: 5:50pm On Jun 19, 2011
Your code not not compile. A HashMap does not allow duplicate keys. To check if a value exists in a HashMap, use the method - HashMap.containsValue(Object value). Iterating through the set of keys is a performance killer.


containsValue

public boolean containsValue(Object value)

Returns true if this map maps one or more keys to the specified value.

Refer to the java API for more information.
Re: Java Cafe by Dolemite(f): 5:55pm On Jun 19, 2011
public void addFile(String nameIn, String dateIn, String typeIn, int sizeIn){
name = nameIn;
date = dateIn;
type = typeIn;
size = sizeIn;
f = new FileSys(name, date, type,size);
totalSize=totalSize+size;
map.put(nameIn,file);
if(availableSpace>0&&!map.containsKey(nameIn)){
availableSpace= storageCapacity-totalSize;
file.add(f);
}
else if(map.containsKey(nameIn)){
System.out.println("A file with that name already exists"wink;
}
else{
System.out.println("There is no space"wink;
}
}
I modified it, trying to use a hashmap with an arraylist, it doesn't work.
Re: Java Cafe by wassolldas: 6:00pm On Jun 19, 2011
you should use containsValue, and your check should be on the object not the name of the file
Re: Java Cafe by Dolemite(f): 6:05pm On Jun 19, 2011
File is just a variable name, i'll try that now,
Re: Java Cafe by Dolemite(f): 6:06pm On Jun 19, 2011
Didn't work sad
Re: Java Cafe by wassolldas: 6:07pm On Jun 19, 2011
post the code
Re: Java Cafe by Dolemite(f): 6:09pm On Jun 19, 2011
private ArrayList<FileSys>file=new ArrayList<FileSys>();
private Map<String, Collection<FileSys>> map = new HashMap<String, Collection<FileSys>>();

public void addFile(String nameIn, String dateIn, String typeIn, int sizeIn){
name = nameIn;
date = dateIn;
type = typeIn;
size = sizeIn;
f = new FileSys(name, date, type,size);
totalSize=totalSize+size;
map.put(nameIn,file);
if(availableSpace>0&&!map.containsValue(nameIn)){
availableSpace= storageCapacity-totalSize;
file.add(f);
}
else if(map.containsValue(nameIn)){
System.out.println("A file with that name already exists"wink;
}
else{
System.out.println("There is no space"wink;
}
}
Re: Java Cafe by wassolldas: 6:15pm On Jun 19, 2011
Does FileSys define an equals method? It should override

public boolean equals(Object obj) {
return (this == obj);
}


from Object

(1) (2) (Reply)

If You Want To Learn Angularjs With Php..using XAMP ...why Not Join In Here / My Ui/Ux Journey / Whats'app Group On Sharepoint

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 39
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.