• What do you need help with? Version 1
    5,001 replies, posted
So i am working on an assignment atm and i am having problems with this code. It is a java assignment and for some reason it is running the ActionPerformed method twice. If someone can help me i would appreciate it. [code] /* Created by Trent Jones 20th March 2010 */ //Importing packages. import java.applet.*; import java.awt.*; import java.awt.event.*; import java.lang.*; import javax.swing.*; import java.text.*; //Main class public class assignment extends Applet implements ActionListener { //Setting up the main image. Image picture_Main; //Set up the decimal format for the outputs. DecimalFormat outputFormat = new DecimalFormat("0.00"); //Setting up the background and the text colors. Color bg_main = new Color(0,180,180); Color text_main = new Color(0,0,0); //Setting up the fonts. Font font_title = new Font("Main", 1, 16); Font font_text1 = new Font("Main", 0, 14); Font font_button1 = new Font("Main", 1, 14); //Labels for the outputs Label label_OutputSides = new Label("SideA: 0 ,SideB: 0, SideC: 0,"); Label label_OutputArea = new Label("Area: 0"); Label label_OutputPerimeter = new Label("Perimeter : 0"); Label label_Type = new Label("The triangle type is : 'No triangle information yet!'"); Label label_Results = new Label("RESULTS"); //Side labels and text fields Label label_SideA = new Label("Side A:"); Label label_SideB = new Label("Side B:"); Label label_SideC = new Label("Side C:"); Label label_Title = new Label("TRIANGLE PROPERTIES"); TextField textF_SideA = new TextField(10); TextField textF_SideB = new TextField(10); TextField textF_SideC = new TextField(10); //Calculate button Button button_Calculate = new Button("Calculate"); public void init() { //Sets the default form and the color for the text and the background. setSize(600,300); setBackground(bg_main); setForeground(text_main); //Sets all the base stuff so it doesn't error. textF_SideA.setText("0"); textF_SideB.setText("0"); textF_SideC.setText("0"); /* Start of the GUI */ //Sets the base height and width for all text fields. int x,y,w,h; w = 100; h = 20; x = 60; y = 50; //Main title add(label_Title); label_Title.setBounds(x - 50,y - 25,w * 2,h); label_Title.setFont(font_title); //Side A add(textF_SideA); textF_SideA.setBounds(x,y,w,h); add(label_SideA); label_SideA.setBounds(x - 50,y,w,h); label_SideA.setFont(font_text1); //Side B add(textF_SideB); textF_SideB.setBounds(x,y + 25,w,h); add(label_SideB); label_SideB.setBounds(x - 50,y + 25,w,h); label_SideB.setFont(font_text1); //Side C add(textF_SideC); textF_SideC.setBounds(x,y + 50,w,h); add(label_SideC); label_SideC.setBounds(x - 50,y + 50,w,h); label_SideC.setFont(font_text1); //Results label add(label_Results); label_Results.setBounds(x - 50,y + 75,w + 150,h); label_Results.setFont(font_title); //Output Sides label add(label_OutputSides); label_OutputSides.setBounds(x - 50,y + 100,w + 150,h); label_OutputSides.setFont(font_text1); //Output Type label add(label_Type); label_Type.setBounds(x - 50,y + 125,w + 225,h); label_Type.setFont(font_text1); //Output Area label add(label_OutputArea); label_OutputArea.setBounds(x - 50,y + 150,w + 200,h); label_OutputArea.setFont(font_text1); //Output Perimeter label add(label_OutputPerimeter); label_OutputPerimeter.setBounds(x - 50,y + 175,w + 200,h); label_OutputPerimeter.setFont(font_text1); //Calculate button add(button_Calculate); button_Calculate.setBounds(x - 50,y + 200,w + 200,h); button_Calculate.addActionListener(this); button_Calculate.setFont(font_button1); // The main picture. picture_Main = getImage(getDocumentBase(),"picture.gif"); /* End of the GUI */ //Setting the layout setLayout(null); } public void actionPerformed(ActionEvent e) { double SideA = Double.parseDouble(textF_SideA.getText()); double SideB = Double.parseDouble(textF_SideB.getText()); double SideC = Double.parseDouble(textF_SideC.getText()); // This below was for a problem that i was not able to fix. Ignore this. /* textF_SideA.setText("0"); textF_SideB.setText("0"); textF_SideC.setText("0"); */ //Check if the triangle is valid. if (checkValidTriangle(SideA,SideB,SideC)) { //Calculating perimeter and area. double Perimeter = findTriPerimeter(SideA,SideB,SideC); double Area = findTriArea(SideA,SideB,SideC); //Checking the type of triangle. if (SideA == SideB && SideA == SideC) { label_Type.setText("Equalateral Triangle"); } else if ((SideA == SideB) || (SideB == SideC) || (SideC == SideA)) { label_Type.setText("Isosceles Triangle"); } else { label_Type.setText("Scalene Triangle"); } //Set the output labels. label_OutputSides.setText("SideA: "+ SideA +",SideB: "+ SideB +", SideC: "+ SideC +"."); label_OutputArea.setText ("Area: "+outputFormat.format(Area)+ " sq. units"); label_OutputPerimeter.setText ("Perimeter : "+Perimeter + " units"); } else { print("The values entered are not for a valid triangle.","ERROR"); } } public double findTriArea(double a,double b,double c) { //Calculates the are of the triangle using double s = (a + b + c) / 2; double s2 = (s * ((s - a)*(s - b)*(s - c))); double x = Math.sqrt(s2); return x; } public double findTriPerimeter(double a,double b, double c) { double x = a + b + c ; return x; } public boolean checkValidTriangle(double SideA,double SideB,double SideC) { boolean x = false; // Checks if the sum of the lengths of 2 sides of the triangle is greater then the third. if ((SideA + SideB > SideC)&&(SideB + SideC > SideA) && (SideA + SideC > SideB)) { //Checks if all sides of the triangle are greater then 0. if ((SideA > 0) && (SideB > 0) && (SideC > 0)) { x = true; } } // If both of the checks above are true then return true otherwise returns false. return x; } public void paint(Graphics g) { //Draw the background images. g.drawImage(picture_Main,350,25,this); g.drawImage(picture_Main,350,150,this); } public void print(String message,String Type) { //A custom method for printing a string in console and creating a popup message box. System.out.println(Type + ": "+ message); JOptionPane.showMessageDialog(null,message,Type,JOptionPane.INFORMATION_MESSAGE); } } [/code]
Are you calling init() twice on accident?
[QUOTE=Blynx6;20932275]Well, I wanted to make an engine, because I need to actually build it all from scratch for my project, a rule for the project is I can't use anything actual engines, unless I can provide full source code.[/QUOTE] There are open source engines. For 3D stuff, Ogre is on top of my head, though I've never used it.
[QUOTE=tjl;20932804]Are you calling init() twice on accident?[/QUOTE] I don't think so. The code is all above. I don't know wats causing it.
[QUOTE=Blynx6;20932275]Well, I wanted to make an engine, because I need to actually build it all from scratch for my project, a rule for the project is I can't use anything actual engines, unless I can provide full source code.[/QUOTE] Do you [b]have[/b] to create an engine?? You should just create the game.
[QUOTE=Blynx6;20932275]Well, I wanted to make an engine, because I need to actually build it all from scratch for my project, a rule for the project is I can't use anything actual engines, unless I can provide full source code.[/QUOTE] There are engines out there with full source code available, use them and save tons of work.
Don't you feel 'guilty' when you use other people's work? Even tho it's free to use by anyone I still prefer to make my own version. I mean, if you can use a game engine someone else made, why not use Game Maker or Construct or UDK? That saves even more work, you practically don't have to type any code. If someday someone told me: "Wow! You made a great game. It looks fantastic! How do you calculate collisions?" "Well, ugh... you know... I didn't exactly make that part of the game..." You could say "But I know how to calculate collisions!", well, if you know how to do it you must hae already done it before, so why not just use that and make your own engine? </rant>
I agree, but only to an extent. Think about, for example, DirectX or OpenGL. They take a hell of a lot of hassle out of repetitive, mundane tasks. Same with simpler 2D graphics libraries, like Allegro and SFML. Why write all of that functionality again from scratch, when someone else has done all the boring number crunching stuff for you? It's sort of reinventing the wheel ;)
[QUOTE=Darwin226;20957012]Don't you feel 'guilty' when you use other people's work? Even tho it's free to use by anyone I still prefer to make my own version. I mean, if you can use a game engine someone else made, why not use Game Maker or Construct or UDK? That saves even more work, you practically don't have to type any code. If someday someone told me: "Wow! You made a great game. It looks fantastic! How do you calculate collisions?" "Well, ugh... you know... I didn't exactly make that part of the game..." You could say "But I know how to calculate collisions!", well, if you know how to do it you must hae already done it before, so why not just use that and make your own engine? </rant>[/QUOTE] A game engine someone else made (especially a commercial one) is probably going to be way (WAY) more stable and feature-complete than you will ever be able to do yourself alone. Besides you are saving yourself an awful lot of time so you can focus on creating your game. Most people want to create games, not engines.
[QUOTE=Chris220;20957061]I agree, but only to an extent. Think about, for example, DirectX or OpenGL. They take a hell of a lot of hassle out of repetitive, mundane tasks. Same with simpler 2D graphics libraries, like Allegro and SFML. Why write all of that functionality again from scratch, when someone else has done all the boring number crunching stuff for you? It's sort of reinventing the wheel ;)[/QUOTE] Not to mention people can install a new version of DirectX and have your game magically run smoother without any action on your end.
Well, that was my personal opinion. I don't really want anything to do with hardware so I agree on using DirectX and such. It's not the point that it runs better or is more feature compatible, the point is that you didn't make it. I'm currently using XNA and I'm staring to thing that it does all the work for me...
[QUOTE=Z_guy;20957157]A game engine someone else made (especially a commercial one) is probably going to be way (WAY) more stable and feature-complete than you will ever be able to do yourself alone. Besides you are saving yourself an awful lot of time so you can focus on creating your game. Most people want to create games, not engines.[/QUOTE] And it's also going to have features you don't even need. If you have a solid game idea and you know it's going to take 5 years to make, then sure, that's understandable. Also your last point is stupid. Writing your own code doesn't mean you are making an engine. You can make a game without buying an engine.
[QUOTE=Darwin226;20958626]Well, that was my personal opinion. I don't really want anything to do with hardware so I agree on using DirectX and such. It's not the point that it runs better or is more feature compatible, the point is that you didn't make it. I'm currently using XNA and I'm staring to thing that it does all the work for me...[/QUOTE] We've had this discussion in this forum before. Having work done for you is good. Unless you want to learn how to make an engine, there's no point in reinventing the wheel. The most efficient programmers are the laziest ones. Only write what you have to write.
I don't make money from programing, therefore, I do not require efficiency. If I made something once, of coarse I'm not going to write the same code again. Since you seem to like the wheel metaphor, lets say I use the wheel because I know how it works, I don't use game engines because I don't know how they work.
DINGDINGDING Round 3: Databee vs Most Other people ON "Laziness or just Efficiency"
[QUOTE=layla;20958907]And it's also going to have features you don't even need. If you have a solid game idea and you know it's going to take 5 years to make, then sure, that's understandable. Also your last point is stupid. Writing your own code doesn't mean you are making an engine. You can make a game without buying an engine.[/QUOTE] This is basically what I'm trying to say: [QUOTE=arienh4;20959043]We've had this discussion in this forum before. Having work done for you is good. Unless you want to learn how to make an engine, there's no point in reinventing the wheel. The most efficient programmers are the laziest ones. Only write what you have to write.[/QUOTE]
I'm to trying to start an argument. I just saw that you were all like "Why are you making an engine you sick fuck" and felt the need to share my views. Maybe I just like the feeling then I spend 5 hours writing a class that by itself does nothing and then calling one method and see 5 hours of my own work come to life. It only gets better when I know that I won't have to write the same class again, just reuse it. [editline]10:14PM[/editline] Time to use the thread for what it was actually meant. I'm currently doing something in XNA and I was wondering, how do you push something in a array? I've read that you can't, instead you use arraylist, queue, list, stack... I've looked up arraylist and it seems that you only call add() and add elements but can those elements be something else than strings? I have a class DisplayObjects and I want to put all it's instances into an array. Thanks.
I like writing the renderer myself, because then I know exactly how it works and I can optimize it the best for my game type.
Ask yourself whether you're interested in the creative challenge of making a fun game, or the technical challenge of making an engine. It's not unreasonable for someone to write a new engine, even though there are free ones already available, if they actually [i]want[/i] to write an engine.
I think people just don't understand that you can write a game without writing an engine. Just because you wrote a .obj parser doesn't mean you're writing an engine. Sure, you can tag the word onto every sentence so you feel like a big boy but It doesn't mean shit.
[QUOTE=layla;20965839]I think people just don't understand that you can write a game without writing an engine. Just because you wrote a .obj parser doesn't mean you're writing an engine. Sure, you can tag the word onto every sentence so you feel like a big boy but It doesn't mean shit.[/QUOTE] "A [B]game engine[/B] is a [URL="http://en.wikipedia.org/wiki/Software"]software[/URL] system designed for the creation and development of [URL="http://en.wikipedia.org/wiki/Video_game"]video games[/URL]." An OBJ parser, a resource manager, and some rendering code IS a game engine. So you more or less need those, unless you're going to manually handle all resources, and hardcode your models you'll want a 'game engine'.
You seem to have not read past the first line on that wiki article. Game engines provide a suite of visual development tools in addition to reusable software components. You don't have to go developing a whole game engine that does everything possible, usually what comes out of making a game is a good framework you could use for other games. I just think it's childish to tag the word onto everything possible. 'An OBJ parser, a resource manager, and some rendering' can also be used for modelling software, would you call that a game engine too? Not sure why you want to argue about a word so much though. Use it all you want, I just think It's silly. [editline]03:08AM[/editline] That's all I've got to say so I won't shit this thread up any more.
So I'm doing the Stanford Engineering Everywhere course, and I need to write a simple program in Java to draw a pyramid of rectangles on the screen in a graphics window, like this: [img]http://i40.tinypic.com/250qm1h.png[/img] Here's my code [FIXED]: [code]/*This file displays a pyramid of * equally-sized rectangle objects * on the screen. */ import acm.graphics.*; import acm.program.*; import java.awt.*; public class Pyramid extends GraphicsProgram { /** Width of each brick in pixels */ private static final int BRICK_WIDTH = 30; /** Width of each brick in pixels */ private static final int BRICK_HEIGHT = 12; /** Number of bricks in the base of the pyramid */ private static final int BRICKS_IN_BASE = 14; /** Arbitrary variable*/ private static int i = 0; public void run() { for (i = 0; i < BRICKS_IN_BASE; i++) { layRow(i); } } private void layRow(int i) { //z = how many bricks in the current row OR current row number int z = i + 1; int x = (getWidth() / 2) - ((BRICK_WIDTH / 2) * (z)); int y = (getHeight()) - ((BRICK_HEIGHT * (BRICKS_IN_BASE - i))); for (int j = 0; j < z; j++) { x += (BRICK_WIDTH); GRect brick = new GRect (x,y,BRICK_WIDTH,BRICK_HEIGHT); brick.setFillColor(Color.BLUE); brick.setFilled(true); add (brick); } } } [/code] Ever since I nested that last 'for' loop inside the 'while' loop at the end, it's stopped drawing altogether. [b]Edit:[/b] Solved it. Fixed code is now here in case anyone has OCD in this thread.
you're not incrementing z
[QUOTE=turb_;20971341]you're not incrementing z[/QUOTE] If I comment out the while loop, or turn it into a for loop incrementing z, it draws this: [img]http://i43.tinypic.com/2znzbrt.png[/img] which is what I had before I added the while loop. Is that the z you were talking about? [editline]08:43AM[/editline] (also how do I set the dimensions of the graphics window so the edge isn't cut off?)
"Glados"? (On the titlebar) If that's what you've named your PC, then have a high 5. I named mine GLaDOS as well :smug:
That appears to be the project name actually.
[QUOTE=Wyzard;20964159]Ask yourself whether you're interested in the creative challenge of making a fun game, or the technical challenge of making an engine. It's not unreasonable for someone to write a new engine, even though there are free ones already available, if they actually [i]want[/i] to write an engine.[/QUOTE] I want the technical challenge of making my own render engine, so I know exactly how stuff like that works and I'll understand better how the GPU works and how to make the most out of it.
Hey guys, if you wanted to make a professional game in C# what library/framework (I don't know the terminology) would you us. Because I think XNA is good but it's not really for professional development. What is the best IDE for developing games in C++ using DirectX and can it be done in Visual Studio?
[QUOTE=Darwin226;20973333]What is the best IDE for developing games in C++ using DirectX and can it be done in Visual Studio?[/QUOTE] Of course you can.
Sorry, you need to Log In to post a reply to this thread.