Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Help Java

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
BlackKilla
Expert Cheater
Reputation: 0

Joined: 02 Jul 2006
Posts: 207
Location: North America ~Rollin up the Trees...

PostPosted: Sun Mar 18, 2007 11:56 pm    Post subject: Help Java Reply with quote

Well...This is a project i have due in a few days...and I cant figure out the width and height of a polygon...any help,Hears my code
Code:

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

public class Shapes extends Applet {
   
   private int last_x=0;
   private int last_y=0;
   private Color current_color=Color.black;
   private Button clear_button;
   private Choice color_choices;
   private Choice shape_choices;
//Setting up all the buttons   
   public void init()
    {  this.setBackground(Color.white);
        clear_button=new Button("Clear");
       clear_button.setForeground(Color.black);
       clear_button.setBackground(Color.lightGray);
       this.add(clear_button);
       
//Setting the choices for colors
       color_choices=new Choice();
       color_choices.addItem("Black");
       color_choices.addItem("Red");
       color_choices.addItem("Yellow");
       color_choices.addItem("Green");
       color_choices.addItem("Cyan");
       color_choices.addItem("Blue");
       color_choices.addItem("Dark Gray");
       color_choices.addItem("Gray");
       color_choices.addItem("Magenta");
       color_choices.addItem("Orange");
       color_choices.addItem("Pink");
       color_choices.addItem("White");
       color_choices.setForeground(Color.black);
       color_choices.setBackground(Color.lightGray);
       this.add(new Label("Color"));
       this.add(color_choices);
//Setting the choices for shapes
       shape_choices=new Choice();
       shape_choices.addItem("Circle");
       shape_choices.addItem("Polygon");
       shape_choices.addItem("Oval");
       shape_choices.addItem("Square");
       this.add(new Label("Shape"));
       this.add(shape_choices);      
   }
//Doing all the indivual methods for drawing the shapes
   public boolean Circle(){
      Graphics g=this.getGraphics();
      g.setColor(current_color);
      g.drawOval(100,100,50,50);
      return true;
      }
   public boolean Square(){
      Graphics g=this.getGraphics();
      g.setColor(current_color);
      g.drawRect(200,200,50,50);
      return true;
      }
   public boolean Polygon(){
      Graphics g=this.getGraphics();
      g.setColor(current_color);
      g.drawRect(150,150,50,50);
      return true;
      }
   public boolean Oval(){
      Graphics g=this.getGraphics();
      g.setColor(current_color);
      g.drawOval(50,50,50,75);
      return true;
      }
//Letting the user draw
public boolean mouseDown(Event e,int x,int y)
  {
     last_x=x;
   last_y=y;
   
System.out.println(x+","+y);
   
   return true;
  }
 public boolean mouseDrag(Event e,int x,int y)
 {
    Graphics g=this.getGraphics();
   g.setColor(current_color);
   g.drawLine(last_x,last_y,x,y);
   last_x=x;
   last_y=y;
   
   return true;
 }      
//Setting the actions for the buttons
public boolean action(Event event,Object arg)
 {
    if(event.target==clear_button){
      Graphics g=this.getGraphics();
      Rectangle r=this.bounds();
      g.setColor(this.getBackground());
      g.fillRect(r.x,r.y,r.width,r.height);
      return true;
       }
       else if(event.target==color_choices){
          if(arg.equals("Black"))current_color=Color.black;
          else if(arg.equals("Yellow"))current_color=Color.yellow;
          else if(arg.equals("Red"))current_color=Color.red;
          else if(arg.equals("Green"))current_color=Color.green;
          else if(arg.equals("Cyan"))current_color=Color.cyan;
          else if(arg.equals("Blue"))current_color=Color.blue;
          else if(arg.equals("Dark Gray"))current_color=Color.darkGray;
          else if(arg.equals("Magenta"))current_color=Color.magenta;
          else if(arg.equals("Orange"))current_color=Color.orange;
          else if(arg.equals("Pink"))current_color=Color.pink;
          else if(arg.equals("White"))current_color=Color.white;
         return true;
         }
      else if(event.target==shape_choices){
         if(arg.equals("Circle"))Circle();
         else if(arg.equals("Square"))Square();
         else if(arg.equals("Polygon"))Polygon();
         else if(arg.equals("Oval"))Oval();
         return true;
      }
      else return super.action(event,arg);
   } 
}

_________________
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
ravicus
Master Cheater
Reputation: 0

Joined: 16 Dec 2006
Posts: 464

PostPosted: Mon Mar 19, 2007 8:42 am    Post subject: Reply with quote

What grade are you in? Or what year in college?
_________________
Back to top
View user's profile Send private message
DeltaFlyer
Grandmaster Cheater
Reputation: 0

Joined: 22 Jul 2006
Posts: 666

PostPosted: Mon Mar 19, 2007 4:15 pm    Post subject: Reply with quote

My school taught applets in grade 10.

Anyways, I see that you're using a drawRect for your draw polygon. Instead of using that, create a Polygon class with the points that define it. Here's the constructor:

Code:

Polygon
public Polygon(int[] xpoints,
               int[] ypoints,
               int npoints)Constructs and initializes a Polygon from the specified parameters.

Parameters:
xpoints - an array of x coordinates
ypoints - an array of y coordinates
npoints - the total number of points in the Polygon
Throws:
NegativeArraySizeException - if the value of npoints is negative.
IndexOutOfBoundsException - if npoints is greater than the length of xpoints or the length of ypoints.
NullPointerException - if xpoints or ypoints is null.


Next, there is a non-static method named getBounds that takes in no parameters and returns a Rectangle object. In that rectangle object, you could find the width and height from the two field variables.

Here's a brief example:

Code:

final int NUM_OF_POINTS = 10;

int[] x = new int [NUM_OF_POINTS];
int[] y = new int [NUM_OF_POINTS];

//Read in user input for array

Polygon poly = new Polygon (x, y, NUM_OF_POINTS);

Rectangle rect = poly.getBounds ();

System.out.println ("The width is: " + rect.getWidth () + "\r\n" +
        "The height is: " + rect.getHeight ());

_________________

Wow.... still working at 827... what's INCA thinking?
zomg l33t hax at this place (IE only). Over 150 people have used it, what are YOU waiting for?
Back to top
View user's profile Send private message
ravicus
Master Cheater
Reputation: 0

Joined: 16 Dec 2006
Posts: 464

PostPosted: Mon Mar 19, 2007 4:39 pm    Post subject: Reply with quote

I'm in seventh grade -_-
_________________
Back to top
View user's profile Send private message
DeltaFlyer
Grandmaster Cheater
Reputation: 0

Joined: 22 Jul 2006
Posts: 666

PostPosted: Mon Mar 19, 2007 5:08 pm    Post subject: Reply with quote

ravicus wrote:
I'm in seventh grade -_-


I was merely giving you an answer to the question you asked. No need for the -_- face.

_________________

Wow.... still working at 827... what's INCA thinking?
zomg l33t hax at this place (IE only). Over 150 people have used it, what are YOU waiting for?
Back to top
View user's profile Send private message
ravicus
Master Cheater
Reputation: 0

Joined: 16 Dec 2006
Posts: 464

PostPosted: Mon Mar 19, 2007 7:07 pm    Post subject: Reply with quote

-_- isn't an insult, it is just telling you why I am not very good at Java yet, I've only been at it for 2 days.
_________________
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites