User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » Noobish C# graphics question Page [1]  
spöokyjon

18617 Posts
user info
edit post

Okay, so I haven't done any programming in years, and yesterday I decided to learn some stuff about C#. I tried to write a program to, just for starters, get a little circle to move around the screen.(Please keep in mind that I haven't taken any classes, I haven't read any books, so this code is probably super hideous. Don't judge me.)

The Planet object is drawing every cycle, but it the actual circle is moving on screen. It's x and y positions are updating properly, and I have the actual numbers being drawn to the screen. In debugging I have checked, and the actual rectangle used to draw the circle is being updated along with the Planet object's changes. Still, the circle that I see on screen remains stationary. Any suggestions? Here's part of the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace tehgraphhhixzss
{
public partial class planets : Form
{
private System.Windows.Forms.Timer timer1;

public planets()
{
InitializeComponent();

this.SetStyle(
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.DoubleBuffer, true);
}

public class Planet
{
double mass, radius, x, y, dx, dy = 0;

public Planet(double m, double r, double xpos, double ypos, double deltax, double deltay)
{
mass = m;
radius = r;
x = xpos;
y = ypos;
dx = deltax;
dy = deltay;
}

public void Draw(System.Drawing.Graphics graphicsContext)
{
System.Drawing.Rectangle planetRect =
new System.Drawing.Rectangle(
(int)(x - radius), (int)(y - radius), (int)(radius), (int)(radius));
graphicsContext.DrawEllipse(System.Drawing.Pens.Blue, planetRect);
}

public void Update()
{
x += dx;
y += dy;
}

public double getX()
{
return x;
}
public double getY()
{
return y;
}
}


int timesDrawn = 0;

Planet testPlanet = new Planet(10, 10, 100, 100, .5, .5);

protected override void OnPaint(PaintEventArgs e)
{
testPlanet.Draw(e.Graphics);
timesDrawn++;
}


private void timer1_Tick(object sender, System.EventArgs e)
{
testPlanet.Update();
label1.Text = "x: " + testPlanet.getX() + " y: " + testPlanet.getY();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void label1_Click(object sender, EventArgs e)
{
label1.Text = label1.Text + "!";
}
}
}

4/14/2008 12:26:22 PM

spöokyjon

18617 Posts
user info
edit post

I can post the rest of the project if needed.

4/14/2008 12:28:15 PM

spöokyjon

18617 Posts
user info
edit post

Okay, just realized that if I go to another window (LIEK TEHWOLFWEBS) and go back it redraws and does so in the correct position. I guess OnPaint() isn't getting called normally. teh hax.

4/14/2008 12:37:25 PM

spöokyjon

18617 Posts
user info
edit post

okay, I figured it out.

MISSION ACCOMPLISHED. THREAD.INVALIDATE();

4/14/2008 12:41:38 PM

spöokyjon

18617 Posts
user info
edit post

Feel free to tell me what's stupid about the rest of the code, though.

4/14/2008 12:49:45 PM

pttyndal
WINGS!!!!!
35217 Posts
user info
edit post

didn't want ya to feel lonely

[Edited on April 14, 2008 at 12:56 PM. Reason : ]

4/14/2008 12:56:32 PM

Rat
Suspended
5724 Posts
user info
edit post

lol @ namespace tehgraphhhixzss

hahaha

4/14/2008 4:32:40 PM

Rat
Suspended
5724 Posts
user info
edit post

grab the xna kit and have some 3d fun with c# kiddo

4/14/2008 4:33:41 PM

spöokyjon

18617 Posts
user info
edit post

Also:

       Planet[] planetArray = new Planet[5];

planetArray[0] = new Planet(10, 21, 100, 100, 5, 5, System.Drawing.Brushes.AliceBlue);


The second line is giving me several errors:

Error 1 Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
Error 2 Invalid token '=' in class, struct, or interface member declaration
Error 3 Method must have a return type
Error 4 Type expected
Error 5 Invalid token ')' in class, struct, or interface member declaration

What am I doing wrong?

4/15/2008 2:00:52 PM

pttyndal
WINGS!!!!!
35217 Posts
user info
edit post

looks like you have too many parameters for planet

4/15/2008 2:50:38 PM

spöokyjon

18617 Posts
user info
edit post

Sorry, the constructor is now this:

public Planet(double m, double r, double xpos, double ypos, double deltax, double deltay, System.Drawing.Brush pColor)
{
mass = m;
radius = r;
x = xpos;
y = ypos;
dx = deltax;
dy = deltay;
planetColor = pColor;
}


These work fine:
        Planet testPlanet = new Planet(10, 21, 100, 100, 5, 5, System.Drawing.Brushes.AliceBlue);

Planet planet2 = new Planet(20, 50, 300, 300, 2, 3, System.Drawing.Brushes.BlanchedAlmond);

4/15/2008 3:11:12 PM

spöokyjon

18617 Posts
user info
edit post

Anybody??

4/15/2008 9:26:38 PM

spöokyjon

18617 Posts
user info
edit post

srsly, can anybody help me?

4/16/2008 9:18:10 AM

afripino
All American
11350 Posts
user info
edit post

say Planet[0] = testPlanet;

what errors do you get?

[Edited on April 16, 2008 at 8:05 PM. Reason : ]

4/16/2008 8:00:57 PM

qntmfred
retired
40553 Posts
user info
edit post

^ already figured it out in message_topic.aspx?topic=522795

4/16/2008 9:47:13 PM

afripino
All American
11350 Posts
user info
edit post

oh...nice. thanks for the update.

4/17/2008 8:11:24 PM

spöokyjon

18617 Posts
user info
edit post

Thanks a lot to everybody who helped.

4/17/2008 9:15:52 PM

spöokyjon

18617 Posts
user info
edit post


GTIV ain't got shit on me, son.

5/9/2008 6:26:44 PM

afripino
All American
11350 Posts
user info
edit post

if red x is your goal...

5/9/2008 7:33:25 PM

spöokyjon

18617 Posts
user info
edit post

Let's try this with a little less fail this time:

BOW BEFORE MY PROWESS.

5/10/2008 9:53:25 PM

spöokyjon

18617 Posts
user info
edit post

In case anybody wants to see the type of leet shit I'm getting into these days:
http://www.mediafire.com/?4mlrzmcnytn
HD SUPER MARIO BROS WITH A RABBIT AND BEES AND SHIT WHAT WHAT

7/6/2008 11:59:32 AM

BigMan157
no u
103352 Posts
user info
edit post

pedobear adventures?

7/6/2008 12:18:16 PM

spöokyjon

18617 Posts
user info
edit post

Basically.

7/6/2008 12:32:41 PM

 Message Boards » Tech Talk » Noobish C# graphics question Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.38 - our disclaimer.