• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

Put header guards in your file:

Code:
#ifndef DRAWOBJECT_H
#define DRAWOBJECT_H

class DrawObject {
...
};

#endif

If your header file is included across multiple files you can get these errors.

Even after putting it there it didn't fix it...

Weirdly enough there was a hidden reference in the file folder that was supposed to have been removed but wasn't, which apparently contained a function called "DrawObject" that I don't ever remember writing...
 
I'm lost as to why compare isn't returning a 0 if the string entered is the same when reversed, can anyone point out where I'm going wrong?

Code:
#include <iostream>
#include <string>
using namespace std;

void reverse(string, int, string&);

int main()
{
	string input1, input2, returned;
	int length, palindromechecker;

	getline(cin, input1);

	length = input1.length();

	reverse(input1, length, returned);

	cout << returned << endl;

	palindromechecker = input1.compare(returned);

	cout << palindromechecker << endl;

	return 0;

}

void reverse(string input1, int length, string& returned)
{
	for(int i = length; i > -1; i--)
	{
	  returned += input1[i];
	}


}

Seems like you are creating the reverse of the string starting from 1 past the last index in input1. i = length - 1 should fix the issue.
 
Unless I can do a vector of float arrays?

you could have: a vector of vectors of floats, a vector of pointers to a parent class [eg. shape] (which would require casting to if you want to access specific shape functions), vectors of strings if you like..

i've probably been spending too much time in java (damn school) but you could do something in C/C++ that is the equivalent of having a key/value pair hashmap where the value contains the object and the key is a 2D vector of the coordinates. This is assuming no object ever has the same coordinates as another.

Or you could create a custom class container that provides access to a list of objects with automatic sorting if that's required. etc.

A can't remember exactly if you can have a vector of arrays but there shouldn't be anything stopping you having a vector of pointers to arrays.
 

Deadly

Member
Can anyone help me with some C#?
{
const decimal plan_A = 100000m;
const decimal plan_B = 150000m;


int age;
string plan;
int deductible;
int month;
decimal coverage;
double monthly;
double total;

age = int.Parse(ageTxtbox.Text);
plan = planTxtbox.Text;
deductible = int.Parse(deductibleTxtbox.Text);
month = int.Parse(monthTxtbox.Text);

if (age >= 18 && age <= 35)
{
if (plan == a)
{
coverage = plan_A;
}
if (deductible == 75)
{
monthly = deductible * 1.05;
total = monthly * month;

coverageTxtbox.Text = coverage.ToString("c");
monthlypremiumTxtbox.Text = monthly.ToString("c");
totalpremiumTxtbox.Text = total.ToString("c");
}
What I want to do is an if to see if it's a or b in the plan textbox. The error I'm getting right now is 'a' does not exist in the current context.
 

Slavik81

Member
Ah misread what you said.

And yeah I already have them in a class, but I figure strings would be better because it can either be a point, line segment, triangle, rectangle, or circle, hence why I was thinking an array of some sort would be best, ala strings.
Just because you can represent anything as a string, doesn't mean you should. This will make your transformations more complex. You'll constantly be reparsing to recreate your typed data.

What operations do you need to perform on shapes?
 
Just because you can represent anything as a string, doesn't mean you should. This will make your transformations more complex. You'll constantly be reparsing to recreate your typed data.

What operations do you need to perform on shapes?

Move, delete, reshape.

Move => based on the position of the mouse at the given time
delete => self explanatory
reshape => grab a vertex and resize/transform that way.

I only gravitated toward strings because I wasn't sure if you could do array of vectors... I've also been up 26 straight hours studying for midterms so I haven't been thinking clearly.
 

Spoo

Member
Move, delete, reshape.

Move => based on the position of the mouse at the given time
delete => self explanatory
reshape => grab a vertex and resize/transform that way.

I only gravitated toward strings because I wasn't sure if you could do array of vectors... I've also been up 26 straight hours studying for midterms so I haven't been thinking clearly.

When you're in this kind of situation, unless you're ... I don't know, rushed, it works out to bust out a little OO practice here. If a Shape can move, delete, and reshape, then those are excellent candidates for base-class functionality you can then implement in derived classes.

class Shape
{
public:
Shape()
virtual ~Shape();
virtual void Move() = 0;
virtual void Delete() = 0;
virtual void ReShape() = 0;
virtual void Draw() = 0;
};


From there, you can become a bit more granular:

class Line : public Shape
{
private:
Point p1; // build yourself a Point structure. (composed of floats x, y and z)
Point p2;
// etc

// implementations of abstract base class functions
}

The benefit here is, of course, you have then have your array, or vector of Shapes that react for you in polymorphic ways.

std::vector<Shape*> myShapes;
myShapes.push_back(new Line(...));
myShapes.push_back(new Triangle(...));

// ...

for (Shape* shape : myShapes)
shape->Draw(); // some generic processing.

I haven't been able to find the source of what this assignment is for you, but you're trying to kill too many birds with a single stone I think.
 
When you're in this kind of situation, unless you're ... I don't know, rushed, it works out to bust out a little OO practice here. If a Shape can move, delete, and reshape, then those are excellent candidates for base-class functionality you can then implement in derived classes.

~snip

I haven't been able to find the source of what this assignment is for you, but you're trying to kill too many birds with a single stone I think.

Ashamed to say I forgot some of my OO stuff when I went to using c for a while.

Remind me though, why are the initial Shape functions set equal to 0?

but that's pretty much exactly what I need to do. Thanks a ton.
 

iapetus

Scary Euro Man
Heh, I started with qBasic when I was 6 too... I think my 386 still has those text adventure games! Next time I coded other than Basic was like 4 years ago though. Did some scripting etc. meanwhile but not much.

Yep, BASIC was where it was at back in those days, when you could start coding straight from boot. :) I started out in BASIC on the ZX80, typing in listings and modifying them initially, then writing my own simple games. Moved on to the infinitely superior BBC BASIC and 6502 assembler - by about 10 or 11 I was coding games with 6502 sprite rendering code and increasing amounts of game logic, and BASIC for the remaining logic and setup. Then it was on to ARM code and eventually C replacing the BASIC.

It's a crying shame that there isn't such an easy route into coding these days - there's so much complexity between you and code. When I started, you turned the computer on and immediately you could type "PRINT 1 + 1" and off you went. Getting started now is so much trickier, and there are so many more distractions. You're battling your environment as much as you are the coding itself - IDEs, compilers, other applications.
 

Spoo

Member
Ashamed to say I forgot some of my OO stuff when I went to using c for a while.

Remind me though, why are the initial Shape functions set equal to 0?

but that's pretty much exactly what I need to do. Thanks a ton.

The =0 stuff just means the functions are "pure" virtual functions; this has the effect of making the class Shape a non-solid type (you cannot make an instance of a "Shape" object, but are free to have instances of derived types). Also known as "abstract classes."

Glad I could help, and good luck!

iapetus said:
It's a crying shame that there isn't such an easy route into coding these days - there's so much complexity between you and code. When I started, you turned the computer on and immediately you could type "PRINT 1 + 1" and off you went. Getting started now is so much trickier, and there are so many more distractions. You're battling your environment as much as you are the coding itself - IDEs, compilers, other applications.

It's a funny thing, really. I often feel pretty lucky, sometimes thinking "Oh man, I'm glad I got into this in a time when there's IDES, and all these other applications!" but then, Academics happened, and sucked all of that right out from under me. But it was for the best, going through that. There really are a lot of distractions, it's just that they look like really nice distractions. At the end of the day, the price we pay for dealing with arbitrarily complicated software engineering tasks, but I'm sure someone out there is talking about how much simpler it was in the card-punching days ;)
 

Kinitari

Black Canada Mafia
I'm having the WORST trouble normalizing this database. My brain is just not registering right now - I think the problem might be because my boss wants it to be an excel based database, and even though I am going to refuse her that, I'm still thinking about it with the Excel framing she provided.

Also, maybe GAF can give me some advice as to what technologies I should use for this project.

I work in a community centre, and we try to connect with a lot of community partners to help our clients out (most of us are government funded, and subsidiaries of the local school board). My boss's boss asked her to make a database where we could organize all the info we have on our community partners - and eventually have a web based representation of this data on our website for our clients to sort through.

My boss wants to be able to organize the organizations by the services they provide, for example, settlement services, employment services and disability services. All this with the ability to at a future time add new service types if deemed necessary.

So in my head I have two tables, Organization(Id, name, phone, etc) and ServiceType(id, description). Unfortunately I am having trouble normalizing this. Can someone help me out here?

Also, how would GAF approach this? I am thinking about creating an offline form for the employees here, where they could add new organizations or edit information easily enough, with updates being pushed to an online database. And to appease my bosses excel desires, the ability to create excel reports in this form (I REALLY hope she doesn't want the ability to push updates to the database through excel).

What's GAF's advice?
 

Milchjon

Member
Yep, BASIC was where it was at back in those days, when you could start coding straight from boot. :) I started out in BASIC on the ZX80, typing in listings and modifying them initially, then writing my own simple games. Moved on to the infinitely superior BBC BASIC and 6502 assembler - by about 10 or 11 I was coding games with 6502 sprite rendering code and increasing amounts of game logic, and BASIC for the remaining logic and setup. Then it was on to ARM code and eventually C replacing the BASIC.

It's a crying shame that there isn't such an easy route into coding these days - there's so much complexity between you and code. When I started, you turned the computer on and immediately you could type "PRINT 1 + 1" and off you went. Getting started now is so much trickier, and there are so many more distractions. You're battling your environment as much as you are the coding itself - IDEs, compilers, other applications.

You were able to code before I was able to read... I'll stand in the corner now.
 

Slavik81

Member
When you're in this kind of situation, unless you're ... I don't know, rushed, it works out to bust out a little OO practice here. If a Shape can move, delete, and reshape, then those are excellent candidates for base-class functionality you can then implement in derived classes.

class Shape
{
public:
Shape()
virtual ~Shape();
virtual void Move() = 0;
virtual void Delete() = 0;
virtual void ReShape() = 0;
virtual void Draw() = 0;
};


From there, you can become a bit more granular:

class Line : public Shape
{
private:
Point p1; // build yourself a Point structure. (composed of floats x, y and z)
Point p2;
// etc

// implementations of abstract base class functions
}

The benefit here is, of course, you have then have your array, or vector of Shapes that react for you in polymorphic ways.

std::vector<Shape*> myShapes;
myShapes.push_back(new Line(...));
myShapes.push_back(new Triangle(...));

// ...

for (Shape* shape : myShapes)
shape->Draw(); // some generic processing.

I haven't been able to find the source of what this assignment is for you, but you're trying to kill too many birds with a single stone I think.
This actually is not what he needs, either, I think. There is the requirement to modify individual vertexes. That may result in circles turning into squares, and vise versa.

Defining all shapes as a set of vertexes that you can perform common operations on seems better for the case in which each vertex is editable. Lasthope was on the right track.
 
I guess this isn't really the best place to post about it, and I was going to wait until after the award ceremony, but I can't contain myself anymore.

I won the Philadelphia Section IEEE 2013 Young Engineer of the Year Award for the work I did for the architecture/engineering of my company's software suite. I am super proud and I just feel lucky to be at a software company that placed so much trust in me and allowed me to really delve into the entire life-cycle of software development, and took my ideas seriously and implemented them.
 

JeTmAn81

Member
I'm having the WORST trouble normalizing this database. My brain is just not registering right now - I think the problem might be because my boss wants it to be an excel based database, and even though I am going to refuse her that, I'm still thinking about it with the Excel framing she provided.

Also, maybe GAF can give me some advice as to what technologies I should use for this project.

I work in a community centre, and we try to connect with a lot of community partners to help our clients out (most of us are government funded, and subsidiaries of the local school board). My boss's boss asked her to make a database where we could organize all the info we have on our community partners - and eventually have a web based representation of this data on our website for our clients to sort through.

My boss wants to be able to organize the organizations by the services they provide, for example, settlement services, employment services and disability services. All this with the ability to at a future time add new service types if deemed necessary.

So in my head I have two tables, Organization(Id, name, phone, etc) and ServiceType(id, description). Unfortunately I am having trouble normalizing this. Can someone help me out here?

Also, how would GAF approach this? I am thinking about creating an offline form for the employees here, where they could add new organizations or edit information easily enough, with updates being pushed to an online database. And to appease my bosses excel desires, the ability to create excel reports in this form (I REALLY hope she doesn't want the ability to push updates to the database through excel).

What's GAF's advice?

It sounds to me like you are on the right track. Main organizational details in one table, service types in another. But don't include the description in the service types table. Make a third table for holding those ID's and descriptions, and then just store the service type ID with the organizational ID in that second table. Then you can just change the descriptions in the ServiceTypes table and it will be reflected immediately because you'll be doing a join from the OrganizationServiceTypes table (holds servicetype ID and organization ID).
 

Zoe

Member
You could break the organization table as far down as you want. Basically any field that can have repeated data can be splintered off to its own table like City, State, Zip, etc.
 

Kinitari

Black Canada Mafia
I guess this isn't really the best place to post about it, and I was going to wait until after the award ceremony, but I can't contain myself anymore.

I won the Philadelphia Section IEEE 2013 Young Engineer of the Year Award for the work I did for the architecture/engineering of my company's software suite. I am super proud and I just feel lucky to be at a software company that placed so much trust in me and allowed me to really delve into the entire life-cycle of software development, and took my ideas seriously and implemented them.

Congrats brah! That sounds like a seriously amazing achievement, honestly I am a bit jealous - I want to have that sort of skill. Definitely be proud.

It sounds to me like you are on the right track. Main organizational details in one table, service types in another. But don't include the description in the service types table. Make a third table for holding those ID's and descriptions, and then just store the service type ID with the organizational ID in that second table. Then you can just change the descriptions in the ServiceTypes table and it will be reflected immediately because you'll be doing a join from the OrganizationServiceTypes table (holds servicetype ID and organization ID).

Thanks! Good advice, and I kind of just realized what was tripping me up. Any of these organizations can have multiple services, and in my head I was doing a 0....1 relationship as opposed to a 0 to many.

edit: Fuck, actually it's a many to many. Service types can have a lot of associated centres, and a centre can have a lot of associated services. If I remember my normalization classes, that generally means I should make a child table.

You could break the organization table as far down as you want. Basically any field that can have repeated data can be splintered off to its own table like City, State, Zip, etc.

Which will probably make it an easier/more controlled form to fill out. Which is a good idea, the databases they use in this centre have a lot of validation errors - sending out a mass email using their mailing list is a fucking nightmare.
 

usea

Member
I guess this isn't really the best place to post about it, and I was going to wait until after the award ceremony, but I can't contain myself anymore.

I won the Philadelphia Section IEEE 2013 Young Engineer of the Year Award for the work I did for the architecture/engineering of my company's software suite. I am super proud and I just feel lucky to be at a software company that placed so much trust in me and allowed me to really delve into the entire life-cycle of software development, and took my ideas seriously and implemented them.
Congrats!
 
I have a quick question for you guys. I'm trying to pass a filename, from the command line, from the client program to the server program and then have the server program check to see if that file exists and then open it. The problem I am having is sending the filename across to to the server and checking it. Does anyone have a function call or or some man page I could look at for help?
 

Slavik81

Member
I guess this isn't really the best place to post about it, and I was going to wait until after the award ceremony, but I can't contain myself anymore.

I won the Philadelphia Section IEEE 2013 Young Engineer of the Year Award for the work I did for the architecture/engineering of my company's software suite. I am super proud and I just feel lucky to be at a software company that placed so much trust in me and allowed me to really delve into the entire life-cycle of software development, and took my ideas seriously and implemented them.

How do you get into the list of contenders? I presume you need to be a member and someone at the company needs to nominate you?
 

pompidu

Member
Im new to developing android apps, there a good site that examples and open source code? Google android developer site is good but it doesnt clearly layout things out.
 

upandaway

Member
I think it's way easier to do a little planning in your head and start coding, then refactor that formless pulp of code into a beautiful program.

TDD is way better for this than a piece of paper since it actually forces you to think how you are going to use that code and then follow the cycle of make it fail, make it work, make it better. I'm not a full believer of TDD but it works very well in those situations.
I get what you mean but when I'm stuck while planning out pseudo-code I just can't write any tests at all. I guess either I'm not compatible with it or I'll get used to it in the future or what. It's easier to just grab a paper and start drawing a bunch of squiggly lines that somehow make sense in that tender, special moment.

Am I screwing myself over with habits?
 

usea

Member
It's amazing how dumb you can feel when doing something new and unfamiliar. I've been learning rust a little bit in my spare time after work. I'm trying to write something fairly simple that would take me under 30 minutes in C#. It's incredibly slow-moving. I feel like an idiot haha.
 
The =0 stuff just means the functions are "pure" virtual functions; this has the effect of making the class Shape a non-solid type (you cannot make an instance of a "Shape" object, but are free to have instances of derived types). Also known as "abstract classes."

Noted. Thanks!

This actually is not what he needs, either, I think. There is the requirement to modify individual vertexes. That may result in circles turning into squares, and vise versa.

Defining all shapes as a set of vertexes that you can perform common operations on seems better for the case in which each vertex is editable. Lasthope was on the right track.

Quite right. I ended up solving it with

Code:
vector<Points> cpoint

where Points is a struct of points (x1, y1, etc and other information needed), and then calling
Code:
cpoints.push_back(struct)

each time an object was drawn.


Now I just need to come up with a bounding box for the vertices..
 
Im new to developing android apps, there a good site that examples and open source code? Google android developer site is good but it doesnt clearly layout things out.

I'm also trying to learn Android to make a game. From what I found, the official developer site is the best resource to learn. There was not a single book that stood out as being better than the official docs, not to mention a lot of books are already outdated.

I found the following helpful:
http://developer.android.com/training/notepad/index.html
 
I get what you mean but when I'm stuck while planning out pseudo-code I just can't write any tests at all. I guess either I'm not compatible with it or I'll get used to it in the future or what. It's easier to just grab a paper and start drawing a bunch of squiggly lines that somehow make sense in that tender, special moment.

Am I screwing myself over with habits?

You don't need pseudocode to write a test. The best parts of tests is that they actually make you think given X, I should expect Y as a results. How you go from X to Y is another complete problem and make you question where you should really expect Y or Z as a result.
 

Linkhero1

Member
I want to learn LAMP since a lot of jobs are asking for one or more of these as a requirements. What's the best resource to learn it? Can someone point me in the right direction.
 
I want to learn LAMP since a lot of jobs are asking for one or more of these as a requirements. What's the best resource to learn it? Can someone point me in the right direction.

Install linux, delete windows, start building some random webpages like a tool to manage your videogame collection.

Anyway, learn a bit of it then move on to Nginx, Postgres and some PHP frameworks or other languages Python/Ruby.
 
Never knew people used visual studio for web development.
Saw some pretty cool stuff yesterday about web development in html,CSS and Javascript at the Microsoft tech days here in the netherland. Most of the syntax went over my head not a web developer but the tools looked awesome. color block showing the color before a color codes.

Also have no idea how good those tools are in new IDE or text editors.
 

jimi_dini

Member
Yep, BASIC was where it was at back in those days, when you could start coding straight from boot. :) I started out in BASIC on the ZX80, typing in listings and modifying them initially, then writing my own simple games. Moved on to the infinitely superior BBC BASIC and 6502 assembler - by about 10 or 11 I was coding games with 6502 sprite rendering code and increasing amounts of game logic, and BASIC for the remaining logic and setup. Then it was on to ARM code and eventually C replacing the BASIC.

I started with BASIC as well on IBM/xt using DOS 3.3. I wrote some ASCII graphics adventure games in 5th or 6th class - I'm not sure anymore. I really don't get all the BASIC hate. It's the perfect language for beginners. And DOS BASIC for example worked by just entering commands. You could specify a program by inputting lines and then save or load that program by entering commands.

Then I used PowerBASIC (which was a really great language, it even had an inline assembler and much more) and also cracked quite a lot of copy protections in games. That's possibly the best way to learn machinecode and also learn what happens low-level. I never really liked C, but I still used it quite a lot. First time I used to do stuff in C++ was for ScummVm. My main languages nowadays are ABAP/4 and C. Mainly ABAP/4, C for some commandline stuff like converting GIF/BMP to OTF or creating barcodes as OTF, where speed matters (OTF is an internal SAP format for graphics - normally you need to upload graphic files manually before being able to print them - which means stuff like barcodes/editable images are normally not possible unless you use printer commands, which are of course limited to specific printers and won't work everywhere).
 

Cheeto

Member
Never knew people used visual studio for web development.
Saw some pretty cool stuff yesterday about web development in html,CSS and Javascript at the Microsoft tech days here in the netherland. Most of the syntax went over my head not a web developer but the tools looked awesome. color block showing the color before a color codes.

Also have no idea how good those tools are in new IDE or text editors.

Visual Studio is actually a surprisingly amazing IDE for a lot of things.
 

upandaway

Member
Argh I give up.. can someone help me feel really dumb by saying what I'm doing wrong?

In Java, I just wanna have a flashing rectangle appear and disappear every visible moment, using JPanel.

Code:
public void paintComponent(Graphics g) {
	g.setColor(Color.white);
	g.fillRect(0, 0, this.getWidth(), this.getHeight());
	try {
		Thread.sleep(100);
	} catch (Exception e) {
	}
	g.setColor(Color.red);
	g.fillRect(0, 0, 50, 50);
}
I have a for loop in main where I call repaint() . What happens is starts with full white, then changes to red without keeping the white background, then stays that way. I print a statement to console every loop instance outside of the repaint(), and it prints them WAY faster than the sleep() should be allowing (I checked and the sleep doesn't throw any exceptions.. even with a sleep of a few second, it prints them like crazy). No clue what's going on.

Edit: If I remove the sleep, it keeps the white background when drawing the red rectangle. Freaking sleep(), man. What the hell.
So I guess I solve that by adding another full white recolor after the sleep.. but that doesn't fix the main problem.

Edit 2: okay there has to be something fundamental about swing I'm not getting. Is it just not being "thread safe" that's messing me up? It's still wrong even when I remove the sleep though..
 

Zoe

Member
Never knew people used visual studio for web development.
Saw some pretty cool stuff yesterday about web development in html,CSS and Javascript at the Microsoft tech days here in the netherland. Most of the syntax went over my head not a web developer but the tools looked awesome. color block showing the color before a color codes.

Also have no idea how good those tools are in new IDE or text editors.

I started my template in Dreamweaver to make sure everything looked right (you can't preview Razor like you can with plain HTML), but otherwise I do everything in VS.
 

Slavik81

Member
Code:
public void paintComponent(Graphics g) {
	g.setColor(Color.white);
	g.fillRect(0, 0, this.getWidth(), this.getHeight());
	try {
		Thread.sleep(100);
	} catch (Exception e) {
	}
	g.setColor(Color.red);
	g.fillRect(0, 0, 50, 50);
}

I know nothing about Java, but there is no way on earth you should ever be sleeping during your render cycle. That should always be fast.

The way to make something flash is to alternate the color you paint it in each paint event. Not within the same single paint event.

EDIT: There's probably also some sort of function you have to call on your component that says "Hey! What I'll render has changed! Paint me again!" So actually, you'll probably need to set up a timer that calls that function on timeout.
 

Linkhero1

Member
Install linux, delete windows, start building some random webpages like a tool to manage your videogame collection.

Anyway, learn a bit of it then move on to Nginx, Postgres and some PHP frameworks or other languages Python/Ruby.

Any good tutorials? I've never dealt with apache and have little experience with mysql, pearl and php.
 

upandaway

Member
I know nothing about Java, but there is no way on earth you should ever be sleeping during your render cycle. That should always be fast.

The way to make something flash is to alternate the color you paint it in each paint event. Not within the same single paint event.

EDIT: There's probably also some sort of function you have to call on your component that says "Hey! What I'll render has changed! Paint me again!" So actually, you'll probably need to set up a timer that calls that function on timeout.
So it's just a Bad Thing to do two different things on a single repaint? Hmm..

Okay, I split it inside the repaint using an if statement, and it works (thanks man!), but...

Code:
Boolean flag = true;	
public void paintComponent(Graphics g) {
	if (flag) {
		g.setColor(Color.white);
		g.fillRect(0, 0, this.getWidth(), this.getHeight());
	} else {
		g.fillRect(0, 0, 50, 50); 
        }
}
(and a flag = !flag in the repaint() loop + the sleep() in that loop too)

But there's no way that this is okay, right?

So basically my problem now is how to get paintComponent to call different code that uses g.. eh I'm not sure I even care at this point
 

Jokab

Member
So it's just a Bad Thing to do two different things on a single repaint? Hmm..

Okay, I split it inside the repaint using an if statement, and it works (thanks man!), but...

Code:
Boolean flag = true;	
public void paintComponent(Graphics g) {
	if (flag) {
		g.setColor(Color.white);
		g.fillRect(0, 0, this.getWidth(), this.getHeight());
	} else {
		g.fillRect(0, 0, 50, 50); 
        }
}
(and a flag = !flag in the repaint() loop + the sleep() in that loop too)

But there's no way that this is okay, right?

So basically my problem now is how to get paintComponent to call different code that uses g.. eh I'm not sure I even care at this point
That looks perfectly fine. I'm not sure what you mean by that question, you just send the g if a certain criteria is met to the function that should be used.
 
Congrats brah! That sounds like a seriously amazing achievement, honestly I am a bit jealous - I want to have that sort of skill. Definitely be proud.

Congrats!

How do you get into the list of contenders? I presume you need to be a member and someone at the company needs to nominate you?

Thanks guys!

Every year the local section takes nominations from local businesses/branches. You have to be under 35 and work in electric engineering or a related field (which software engineering counts as). Our CTO nominated me. Apparently I was up against some guys from Lockheed and L3 Communications and some other big players, so that definitely adds to the satisfaction level as well. I won the award based on my work on our Decision Support/Threat Evaluation/Asymmetric Warfare Managment software. I've been working on integrating data feeds from various sensor sources into our threat evaluation engine.
 

upandaway

Member
That looks perfectly fine. I'm not sure what you mean by that question, you just send the g if a certain criteria is met to the function that should be used.
If you can just throw g around.. the book said I shouldn't touch it, but thinking about it now I guess they just meant creating it. Okay!

It all works, in any case! Now it's a silly little scrolling platformer with terrible physics and only works with one keypress at a time. I learned so much doing this one thing that it's kind of scary.
Next up is uh Pacman I guess. I made it with an Engine object so it should be easy to just replace that and finish this quickly...
 
Argh I give up.. can someone help me feel really dumb by saying what I'm doing wrong?

In Java, I just wanna have a flashing rectangle appear and disappear every visible moment, using JPanel.

Code:
public void paintComponent(Graphics g) {
	g.setColor(Color.white);
	g.fillRect(0, 0, this.getWidth(), this.getHeight());
	try {
		Thread.sleep(100);
	} catch (Exception e) {
	}
	g.setColor(Color.red);
	g.fillRect(0, 0, 50, 50);
}
I have a for loop in main where I call repaint() . What happens is starts with full white, then changes to red without keeping the white background, then stays that way. I print a statement to console every loop instance outside of the repaint(), and it prints them WAY faster than the sleep() should be allowing (I checked and the sleep doesn't throw any exceptions.. even with a sleep of a few second, it prints them like crazy). No clue what's going on.

Edit: If I remove the sleep, it keeps the white background when drawing the red rectangle. Freaking sleep(), man. What the hell.
So I guess I solve that by adding another full white recolor after the sleep.. but that doesn't fix the main problem.

Edit 2: okay there has to be something fundamental about swing I'm not getting. Is it just not being "thread safe" that's messing me up? It's still wrong even when I remove the sleep though..

Code:
	try {
		Thread.sleep(100);
	} catch (Exception e) {
	}

Having your catch block do nothing is never a good idea. Should at least print out e.getMessage() to see what is going on if it fails.

For your general problem? Your second approach is more along the right lines. Any control of threads or pausing should be done in your main engine loop. It should also be responsible for setting any flags on objects and then they take care of painting themselves whenever they can (if things do not update at the same time, you'll hit plenty of problems).
 

jon bones

hot hot hanuman-on-man action
I guess this isn't really the best place to post about it, and I was going to wait until after the award ceremony, but I can't contain myself anymore.

I won the Philadelphia Section IEEE 2013 Young Engineer of the Year Award for the work I did for the architecture/engineering of my company's software suite. I am super proud and I just feel lucky to be at a software company that placed so much trust in me and allowed me to really delve into the entire life-cycle of software development, and took my ideas seriously and implemented them.

damn b, congrats! i'm sure your folks are super proud of you
 
An I a weirdo for over-commenting my code? I spend plenty of time writing Javadocs for my little game that nobody but me will see the source for, making sure they're clear and formatted correctly, and sometimes I think, "Who am I writing these for?" lol

I think I do it as a distraction from doing actual programming sometimes...
 

CrankyJay

Banned
An I a weirdo for over-commenting my code? I spend plenty of time writing Javadocs for my little game that nobody but me will see the source for, making sure they're clear and formatted correctly, and sometimes I think, "Who am I writing these for?" lol

I think I do it as a distraction from doing actual programming sometimes...

I only really comment code that isn't clear or to section off areas of a larger method...like a complex algorithm or something.
 

IceCold

Member
I only really comment code that isn't clear or to section off areas of a larger method...like a complex algorithm or something.

This. Only comment code you know may be unclear. Doing something like this is useless for example:

if(string.trim().isEmpty() || string == null ) // Verify that the passed string is valid
{
throw IllegalArgumentException("String is blank! gtfo!); // throw error message
}


The comments here don't serve any purpose since what I'm doing is obvious. You don't want to clutter your code with comments.
 

Slavik81

Member
An I a weirdo for over-commenting my code? I spend plenty of time writing Javadocs for my little game that nobody but me will see the source for, making sure they're clear and formatted correctly, and sometimes I think, "Who am I writing these for?" lol

I think I do it as a distraction from doing actual programming sometimes...
Try to write code that is so clear in purpose that comments are redundant.

If you are considering writing a comment, consider if you could just pull the code out into a well named function, or if you could use variables to give names to the values you use.

Of course, you'll still need some comments, but far fewer. Most functions should not need comments.
 
Top Bottom