Misklahr Light Poster

Hello there!
I'm trying to understand some basic android animation programming using Android Studio, and I have followed some tutorials on the internet.
I already know some basic java programming, but I have a problem with my "copy/paste app" I made from youtube.

This is my MainActivity:

public class MainActivity extends ActionBarActivity {

    private Button play;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        play = (Button) findViewById(R.id.play_button);

        play.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                Intent startGame = new Intent("com.abc.test.DRAWGAME2");
                startActivity(startGame);

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

This is DrawGame2.java:

public class DrawGame2 extends Activity implements View.OnTouchListener {

    MyView surfaceView;
    float y;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        surfaceView = new MyView(this);
        surfaceView.setOnTouchListener(this);
        setContentView(surfaceView);
    }

    @Override
    protected void onPause() {
        super.onPause();
        surfaceView.pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        surfaceView.resume();
    }

    @Override
    public boolean onTouch(View v, MotionEvent me) {

        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        switch (me.getAction()){
            case MotionEvent.ACTION_DOWN:
                y = me.getY();
                break;
            case MotionEvent.ACTION_UP:
                y = me.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                y = me.getY();
                break;
        }

        return true;
    }

    public class MyView extends SurfaceView implements Runnable {

        Thread thread = null;
        SurfaceHolder holder;
        boolean isRunning = false;

        public MyView(Context context) {
            super(context);
            holder = getHolder();
            y = 0;
        }

        public void run() {
            while (isRunning) {
                if (!holder.getSurface().isValid()) {
                    continue;
                }
                Canvas canvas = ...

Misklahr

Earlier, I tried to figure out how to do it, and this is what I got:
[CODE]<?php

$randomquery = mysql_query("SELECT * FROM table");

if (!mysql_num_rows($randomquery) <= 0) {

while ($row = mysql_fetch_assoc($randomquery)) {

    if (mysql_real_escape_string(time() - $row[time] >= 600)) {
        unlink($row[picname1]);
        unlink($row[thumb1]);
        mysql_query("DELETE FROM table WHERE session = '$row[session]'");
    }
}

}
?>[/CODE]
How about that? Will it work?

Misklahr

My table looks like this:
picname = name
thumbname = thumbname
time = the time it was uploaded
session = $_SESSION['random'] (Given a random number)

The thing is that I don't know how to make a script that checks if the sessions in the database are active or if the user has closed their browser

Misklahr

The thing is that I don't want to have any time limit. I just want to check if the session (called $_SESSION['random']) is still active. If it isn't, delete the files.

[CODE]SELECT * FROM table WHERE session = [?????????] [/CODE]

I just need help with the [????????] part.

Misklahr

How do i create a PHP code that checks if every session in the database is active?

Misklahr

Aha, I'll just see if I can get it to work!

Thanks

Misklahr Light Poster

Hi!

Is there any way to remove a file from a directory when a session ends?
(Like when a user closes the webbrowser)

Misklahr

Just thinking:
Maybe it's better to have the thumbnails and resized pictures in a folder, insted of in a database as a blob?

Misklahr Light Poster

Hi

I want to create a code so that the user can upload an image. The code then resizes it and then [U]stores it as a blob[/U] in my database.

[URL="http://www.9lessons.info/2009/03/upload-and-resize-image-with-php.html"]The code I'm using[/URL]

I have done everything, but need to know which variable (in the link) to store into the blob in my mysql database. I don't want imagejpeg() to create the pictures in a /images/ folder, like this code does, I want the data in a variable and then store it as a blob.

How do i assign a variable the "raw" data (like opening an image in notepad.exe) from the created and resized images?

Misklahr

[QUOTE=haleel;1538308]By using subtring(), you can extract the first four digits. [/QUOTE]
Does it work if the numbers are for example:
0413, 0929, 08, 019

What I mean is that they all not are 4 digit numbers.
Then see if any of the numbers matches the input.

Misklahr Light Poster

Hi.

I have a problem with some JS at my website.

The user has to fill out a form, enter his/her name, e-mail, phonenumber, location etc.
The problem is that I don't want to have a fake phonenumber entered to my database, so I want to verify the phonenumber by comparing the area codes of my country (Sweden).

I have all the area codes, (2, 3 or 4 digits) but dont know how to take the entered value and see if the first characters matches the area codes.

[CODE]
function validateForm() {

var msg = "Check:\n\n";

var name=document.forms["quest"]["name"].value
if (name==null || name==""){
msg += " - Name\n";
}

var tele=document.forms["quest"]["tele"].value
var telelangd = tele.length;
if (tele==null || tele=="" || isNaN(tele) || telelangd < 8)

msg += " - Telephonenumber\n";

//continues with e-mail etc...[/CODE]
msg = A warning message. If the user types something wrong, it will show when msg is alerted after clicking the "check" button.

Tried to understand startWith(), but it failed.

Can anyone help me?

Misklahr

Oh, sorry, nevermind, I now understand what you mean, mschroeder.

Thank you very much!

Misklahr

The thins is that when i rightclick on the image and choose "Show image" -> "domain.com/get?id=149", i get redirected to "domain.com/get.php?id=149"

This is when my .htaccess file is like this:

[CODE]ErrorDocument 404 http://www.domain.com/404.html

RewriteEngine On
RewriteRule ^get/([0-9]+)$ get.php?id=$1 [NC,L]
RewriteRule ^([a-z0-9-]+)$ /$1.php [L][/CODE]

Misklahr

My .htcacces file looks like this:

[CODE]ErrorDocument 404 http://www.mywebsite.com/404.html

RewriteEngine On
RewriteRule ^get/([0-9]+)$ get.php?id=$1 [NC,L]
RewriteRule ^([a-z0-9-]+)$ /$1.php [L]
[/CODE]

(The last "RewriteRule" deletes the .php at the end)

But the one mschroeder wrote didn't work x/
(RewriteRule ^get/([0-9]+)$ get.php?id=$1 [NC,L])

Misklahr

[QUOTE=Suzie999;1463896][/QUOTE]
It's not that, the code works and displays an image, but instead of the URL showing
"mywebsite.com/get?id=137"

I want the URL to be
"mywebsite.com/137" or "mywebsite.com/get/137"

Misklahr Light Poster

Hi

I have a website that has a get.php file that echoes out the specific image i want

Here is the code:
[CODE]$id = addslashes($_REQUEST['id']);

$image = mysql_query("SELECT * FROM items WHERE id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];

header("Content-type: image/jpeg");

echo $image;[/CODE]

To get an image from the database with the id $id, i use this code:

[CODE]img src='get.php?id=$id'[/CODE]

The problem is that when someone wants to save the image, the URL is for example "mywebsite.com/get.php?id=137" and the webbrowser wants to save it as "get.php".

I want it to be saved as just "$id", or as in this example "137"

Misklahr

[QUOTE=jon.kiparsky;1389149]That's what that means. So you're in business. Good job![/QUOTE]
Thank you so much!
I still have a problem though, but I think I can handle it now ;-)
Thanks again!

Misklahr

Okay!

I have this in gp1
[CODE] public void setDajs(int dice) {
this.dajs = dice;
JOptionPane.showMessageDialog(null, dajs);
}[/CODE]

And this in Board
[CODE] public class MyAction implements ActionListener {
public void actionPerformed(ActionEvent ae) {
str = ae.getActionCommand();

    if(str.equals("Roll the dice")) {

    gp1 gp1Dice = new gp1();

    no1 = (int)(Math.random()*6) + 1;
    no2 = (int)(Math.random()*6) + 1;
    dice = no1 + no2;

    text.setText("You rolled " + no1 + " and " + no2 + " which equals: " + dice);
    text.disable();

    gp1Dice.setDajs(dice);

}
}
}[/CODE]

So now I have a message that tells me the right number! That must mean that dajs in gp1 has the dice value in Board?

Misklahr

[QUOTE=jon.kiparsky;1389102]Your setDajs(int dajs) is correct - it can be written either way. The way you wrote it the first time is also correct.

In actionPerformed, I don't know whether you want to instantiate a new gpl (or is it gp1? letter 'l' or numeral '1'?) each time someone rolls the dice, because I don't realy know what that class is. If it's supposed to keep some state of play, then you want to refer to an existing gp1 object.

So what else does that class do?[/QUOTE]
It's gp1 gp(one) - numeral (As you said, GamePlayer1) :)

Each time the dice is rolled, dajs should get a new value, (the value of dice).
((Later) I'll store that value in another int (let's call it "position") in gp1.java, then add position with dajs, which will replace the current position with (position+dajs), so position=position+dajs.)

I've written everything MyAction does.
But Board also draws the moving picture, the background and adds the button

Misklahr

[QUOTE=jon.kiparsky;1389079]My bad. Yes, you're going to modify MyAction.actionPerformed. You want to roll the dice, then you want that "dajs" variable to get that value. You just wrote the method to do this.[/QUOTE]

Is this right in gp1:
[CODE] public void setDajs(int dajs) {
this.dajs = dajs;
}[/CODE]

I don't really know.. Hum..
In MyAction.actionPerformed after the dice
[CODE]
gp1 gp1Dice = new gp1();
gp1Dice.setDajs(dice);[/CODE]

Misklahr

[QUOTE=jon.kiparsky;1389018]Bingo.
Style-wise, and this is just a side note, some people like to use distinct names as you've done for the incoming parameter and the variable being set.
Others prefer to use the same name, and it would look like this:

[CODE] public void setDajs(int dajs) {
this.dajs = valOfDajs;
}[/CODE]

[CODE]this.dajs[/CODE] means "the dajs that is a field of the class in which this method appears" -this is neeed to get around the shadowing caused by having a formal parameter with the same name. Don't worry if this doesn't make sense - I mention it because you'll sometimes see this form, and it's good to be aware of it. It means exactly the same thing as the form you wrote.

Now, how are you going to modify actionPerformed()?[/QUOTE]
Well... I assume that I in some way need to call "valOfDajs" or, the parameter.
(btw modify "Board"? the "actionPerformed(ActionEvent ae)")

Misklahr

[QUOTE=jon.kiparsky;1389002]You want a method that isn't the constructor - something that your actionPerformed() method can call.

So it's a method, it's got to be public, it'll have a return type of "void" (since it's returning a value), and it'll take a single paramter, which will be an int.
Typical name of such a method would be "setDajs".

In the body of the method, you'll set dajs equal to the parameter. It's literally a one-liner.[/QUOTE]
Ah, so if I got it right, it'll look like
[CODE] public void setDajs(int valOfDajs) {
dajs = valOfDajs;
}[/CODE]

Misklahr

[QUOTE=jon.kiparsky;1388981]Read my sig.

Okay, so you want to make a setter for "dajs" in "gp1", and then you want to call that from MayAction.actionPerformed.

First things first: what does the setter look like?[/QUOTE]
Isn't it just [COLOR="Green"]dajs = dajse.getDice();[/COLOR]?
Since
[CODE]
public void dajsen() {
Board dajse = new Board();
dajs = dajse.getDice();
}[/CODE]
The thing is that it runs before the rolling of hte dice.

Misklahr

[QUOTE=jon.kiparsky;1388942]Try making a method in gp1 that does what you want (that is, calls getDice()), and call it from the actionPerformed() method.
Alternatively, you could make a method which allows another class to set gp1.dajs, and call that after you get a value for dice.

One way, your actionPerformed rolls the dice, and tells your gp1 object to call board.getDice(), the other way your actionPerformed rolls the dice and then tells gp1 to set dajs to whatever value has wound up in dice. The result should be the same, which you choose depends on which makes more sense to you.

(Style note: It would be nice if your classes were named in a more descriptive fashion - that way I'd know more about what you're modelling. In future, maybe you'd like to use a name like "GamePlayer" for the class name, if that's what it stands for.)[/QUOTE]
Ok, the second one sound easiest, but I have failed at both x/ looks like I just fail at java totally :/

Misklahr

[QUOTE=jon.kiparsky;1388900]Since you're assigning dajs=dajse.getDice() in the constructor, it'll have the value that dajse.dice contained whenever you instantiated the gp1 class.
That is, whenever you have a line like
[ICODE]gp1 foo = new gp1();[/ICODE]
it'll get the value of dice at that moment. Has the user rolled the dice at that point? If not, it'll be zero.

You want gp1 to respond to the player's roll of the dice, no? So maybe the [ICODE]dajs=dajse.getDice()[/ICODE] should be in a method that's triggered by your MyAction.[/QUOTE]

At the moment, the dajs value is 0. (I made the picture move to the right if dajs==0, and it did).

Yes, I want gp1 to respond to the roll of dice, but have no idea of how to make a method that's only triggered by MyAction, and not automatically at the beginning. :/

Misklahr Light Poster

Hi

I'm new to java, and I'm trying to make a very basic game.
The problem is that i have 3 classes that cooperates and I can't get one of the values from class "Board.java" to "gp1.java".

In Board i have a button that "rolls the dice" x2 and adds the values.
In gp1 i make a picture move around (game marker or whatever it's called), but I want it to move depending on the dice.

Some of the code of "Board":
[CODE] public class Board extends JPanel implements ActionListener {

    private int dice;

    JButton button;
    TextField text = new TextField(26);
    private String str;

    public int no1, no2;

    public Board(){

    ...

    button = new JButton("Roll the dice");
    button.addActionListener(new MyAction());
    add(button);

    ...

}

    public class MyAction implements ActionListener {
    public void actionPerformed(ActionEvent ae) {
    str = ae.getActionCommand();

    if(str.equals("Roll the dice")) {

    no1 = (int)(Math.random()*6) + 1;
    no2 = (int)(Math.random()*6) + 1;
    dice = no1 + no2;

    text.setText("You rolled " + no1 + " and " + no2 + " which equals: " + dice);
    text.disable();

}
}
}
public int getDice() {
return dice;
}

}[/CODE]

I get the textfield to work, and it shows the dice numbers

Some of the code of gp1:

[CODE]

public class gp1 {

public int dajs;

    ...

    public void dajsen() {
    Board dajse = new Board();
    dajs = dajse.getDice();

}

    ...

}
[/CODE]

I just want "dajs" in gp1 to contain the "dice" value from Board

I know that I'm doing it totally wrong, but ...