Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Turing in java (applet)
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
r.m_spy




PostPosted: Sat May 01, 2010 8:43 am   Post subject: Turing in java (applet)

this post is for those who know both turing and java

GO North toronto collegate institude!

Java:

import javax.swing.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.AWTException;
public class Turing_drawing extends Applet implements MouseListener, MouseMotionListener
{
int maxx=getSize().width;
int maxy=getSize().height;
int mx = maxx/2;
int my = maxy/2;
boolean isButtonPressed = false;
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g)
{
//Check turing_drawing.txt for help
for(int xx=0;xx<1000;xx++)
{
drawfilloval(g,mx,my,10,10,Color.black);
drawtext(g,"Your mouse co-ordinates are ( "+mx+" , "+my+" )",10,10);
cls(g);
}
}
public void drawoval(Graphics g,int x,int y,int x2,int y2,Color clr)
{
g.setColor(clr);
g.drawOval(x-x2,y-y2,2*x2,2*y2);
}
public void drawfilloval(Graphics g,int x,int y,int x2,int y2,Color clr)
{
g.setColor(clr);
g.fillOval(x-x2,y-y2,2*x2,2*y2);
}
public void drawbox(Graphics g,int x,int y,int x2,int y2,Color clr)
{
g.setColor(clr);
g.drawRect(x,y,x2,y2);
}
public void drawfillbox(Graphics g,int x,int y,int x2,int y2,Color clr)
{
g.setColor(clr);
g.fillRect(x,y,x2,y2);
}
public void drawarc(Graphics g,int x,int y,int x2,int y2,int rad1,int rad2,Color clr)
{
g.setColor(clr);
g.drawArc(x-x2,y-y2,2*x2,2*y2,rad1,rad2);
}
public void drawfillarc(Graphics g,int x,int y,int x2,int y2,int rad1,int rad2,Color clr)
{
g.setColor(clr);
g.fillArc(x-x2,y-y2,2*x2,2*y2,rad1,rad2);
}
public void drawline(Graphics g,int x,int y,int x2,int y2,Color clr)
{
g.setColor(clr);
g.drawLine(x,y,x2,y2);
}
public void drawdot(Graphics g,int x,int y,Color clr)
{
g.setColor(clr);
g.drawLine(x,y,x,y);
}
public void delay(int x)
{
try
{
Thread.sleep(x);
}
catch(InterruptedException e)
{
}
}
public void cls(Graphics g)
{
repaint();
}
public void drawtext (Graphics g,String text,int x,int y)
{
g.drawString(text,x,y);
}
public void MouseWhere(MouseEvent e)
{
}
   public void mouseEntered( MouseEvent e ) {
      // called when the pointer enters the applet's rectangular area
   }
   public void mouseExited( MouseEvent e ) {
      // called when the pointer leaves the applet's rectangular area
   }
   public void mouseClicked( MouseEvent e ) {
      // called after a press and release of a mouse button
      // with no motion in between
      // (If the user presses, drags, and then releases, there will be
      // no click event generated.)
   }
   public void mousePressed( MouseEvent e ) {  // called after a button is pressed down
   isButtonPressed = true;
   e.consume();
   }
   public void mouseReleased( MouseEvent e ) {  // called after a button is released
      isButtonPressed = false;
   }
   public void mouseMoved( MouseEvent e ) {  // called during motion when no buttons are down
  mx = e.getX();
my = e.getY();
e.consume();
    }
   public void mouseDragged( MouseEvent e ) {  // called during motion with buttons down
   mx = e.getX();
my = e.getY();
e.consume();
   }
   public void RobotMouse(int x,int y,int btn)
   {
   try
   {
    Robot robot = new Robot(); // Create a new robot
               robot.mouseMove(x,y); // Move the mouse
               if (btn==1)
               {
               robot.mousePress(InputEvent.BUTTON1_MASK); // Press mouse button
               robot.mouseRelease(InputEvent.BUTTON1_MASK); // Release mouse button
               }
               robot.delay(60000); // 1 minute = 60000 ms
               }
               catch (AWTException e)
            {
               e.printStackTrace();
            }
   }
}



please help me with the following
1) when you use the robot mouse, it doesn't return to the program
2) I dont know how to get keyboard
3) any suggestions/complements/complaints Im all ears



turing_drawing.txt
 Description:
the help file

Download
 Filename:  turing_drawing.txt
 Filesize:  1.35 KB
 Downloaded:  166 Time(s)


Turing_drawing.html
 Description:
my html

Download
 Filename:  Turing_drawing.html
 Filesize:  284 Bytes
 Downloaded:  167 Time(s)


Turing_drawing.java
 Description:
this is the java file

Download
 Filename:  Turing_drawing.java
 Filesize:  3.53 KB
 Downloaded:  211 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
TerranceN




PostPosted: Sat May 01, 2010 10:02 am   Post subject: RE:Turing in java (applet)

I have no idea why you want to re-create Turing, but w/e...

1. Is it intended that the Robot mouse click in screen co-ordinates, or did you mean window co-ordinates? Anyway, the robot clicks don't seem to work outside of the applet (it's as if it clicked the top left icon of the other window, even if thats not where the mouse is, and I'm assuming you're using Windows). I don't know what's wrong, I have never used 'Robot' before.

2. Use a 'KeyListener' to get 'KeyEvent's like how you used 'MouseListener' to get 'MouseEvent's (the methods are a little different, just look it up). You can easily store what keys are down in an array of booleans so you can use it kind of like an 'array char of boolean' (IIRC) in Turing.

3. Why do you want to re-create Turing? I'm sure most people here agree that Java's standard libraries are better than Turing's. What's the point of re-creating every draw call? Is it that hard to learn to use (and see the advantages of) Graphics objects? The whole point is so that everything that uses graphics does not have to make its own drawing methods. Also, Indent your code (maybe it got butchered when you posted it?). Finally, it would be nice if you told us what you are trying to do with this (ie. is to to help people that use Turing learn Java?). Sorry for being so harsh, and maybe I'm missing something here, but IMO if you want to use Turing, just use Turing, no point in trying to re-create it elsewhere.

Hope that helps.
r.m_spy




PostPosted: Sat May 01, 2010 12:31 pm   Post subject: RE:Turing in java (applet)

as for 1) I do have windows, and it loops instead of going back to the program

for 2) Thank you so much!!!!
this is why I post my java programs online

and 3) I am trying to recreate turing so I can put it in html, instead of exe. plus, I can teach my friend graphics in java by him seeing where everything goes visually.

PS: I wouldnt of asked for complaints if i didnt want any

thank you

r.m_spy
r.m_spy




PostPosted: Sun May 02, 2010 8:43 am   Post subject: Re: Turing in java (applet)

Okay, Ive created this, but Im unable to intrgrate it in my turing applet. Help!!!
Java:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Keyboard1 extends Applet
   implements KeyListener, MouseListener {
   char s = ' ';

   public void init() {

      addKeyListener( this );
      addMouseListener( this );
   }

   public void keyPressed( KeyEvent e ) { }
   public void keyReleased( KeyEvent e ) { }
   public void keyTyped( KeyEvent e ) {
      char c = e.getKeyChar();
      if ( c != KeyEvent.CHAR_UNDEFINED ) {
         s = c;
         repaint();
         e.consume();
      }
   }

   public void mouseEntered( MouseEvent e ) { }
   public void mouseExited( MouseEvent e ) { }
   public void mousePressed( MouseEvent e ) { }
   public void mouseReleased( MouseEvent e ) { }
   public void mouseClicked( MouseEvent e ) {
   s=' ';
      repaint();
      e.consume();
   }

   public void paint( Graphics g ) {
   g.drawString(""+s,100,100);
   }
}
TerranceN




PostPosted: Sun May 02, 2010 3:55 pm   Post subject: RE:Turing in java (applet)

You need to add the KeyListener like you did the other listeners.

Java:
addKeyListener(this);
kingghaffari




PostPosted: Sun Oct 23, 2011 7:18 pm   Post subject: RE:Turing in java (applet)

I go to North Toronto too! Are you in grade 12?
I am in grade 11 Very Happy
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: