Computer Science Canada Help.... with graphics |
Author: | nonamedude [ Fri May 07, 2010 11:38 pm ] |
Post subject: | Help.... with graphics |
I am writing an applet, and i am fairly new to it, still learning as I type. I was just wondering how to call a graphics method this one for example public void paint (int count[] [], Graphics g) { } if this is nt possible, then, how to display a shape if a certain condtion is met eg if (int a ==1) drawoval ty for your help ![]() |
Author: | DemonWasp [ Sat May 08, 2010 4:29 am ] | ||
Post subject: | RE:Help.... with graphics | ||
The syntax is as follows:
|
Author: | TerranceN [ Sat May 08, 2010 9:34 am ] |
Post subject: | RE:Help.... with graphics |
What do you mean by "calling a graphics method"? Do you mean how to draw stuff using a graphics object, or how to get a graphics object so you can call a method that requires one? If it is the former, read this page. If it is the latter, you need to have a component (things like applets and frames are components already) and call its getGraphics() method. |
Author: | nonamedude [ Sat May 08, 2010 11:03 am ] |
Post subject: | Re: Help.... with graphics |
What i i am stuck goes like this, static int counter[] [] = new int [10] [10]; public void init () { button1 = new JButton ("Button1"); button1.addActionListener (this); button1.setFont (f3); button1.setBackground (Color.green); button1.setForeground (Color.white); contentPane.add (button1); } public void actionPerformed (ActionEvent e) { if (e.getSource () == button1) { for (int x = 6 ; x <= 0 ; x++) { if (count4 [x] [0] == 0) <------------------------------------------------- ADD ONE TO THE COUNTER IF THE USER CLICKS THE BUTTON { count4 [x] [0] = 1; break; } } } } public void paint (Graphics g) { super.paint (g); if (count4 [6] [0] == 1) { g.setColor (Color.cyan); g.fillOval (20, 135, 100, 100); <---------------------------------------------------------------- IF USER CLICKS THE BUTTON, DRAWS AN OVAL } } THIS IS JUST PART OF THE CODE It doesn't seem to, as in the program runs without errors but the oval is not drawn when the button is clicked |
Author: | TheGuardian001 [ Sat May 08, 2010 11:15 am ] | ||
Post subject: | Re: Help.... with graphics | ||
Your for loop doesn't run.
you tell it to continue while x is less than 0. But x starts at 6. |
Author: | nonamedude [ Sat May 08, 2010 12:22 pm ] |
Post subject: | Re: Help.... with graphics |
oh ![]() |
Author: | nonamedude [ Sat May 08, 2010 1:59 pm ] |
Post subject: | Re: Help.... with graphics |
it sorta works now but each time i want a shape to appear, i have to maximize then restore it. This is a problem since if the user clicks the several times, several circles will appear. Btw i am using ready to program |
Author: | nonamedude [ Sat May 08, 2010 2:11 pm ] |
Post subject: | Re: Help.... with graphics |
If i maximize it, or restore it, it runs the program again . How can i stop this ![]() |
Author: | TheGuardian001 [ Sat May 08, 2010 2:11 pm ] |
Post subject: | Re: Help.... with graphics |
do you ever call repaint(); in your program? If not, paint(Graphics g) won't be called unless the window manager forces repaint (such as when you resize). nonamedude wrote: Btw i am using ready to program Unless this is for school and you have to, please stop using that. RTP is an old, broken thing. It uses an obsolete version of Java alongside its own severely crippled versions of other java classes. |
Author: | chrisbrown [ Sat May 08, 2010 2:18 pm ] |
Post subject: | RE:Help.... with graphics |
Generating an event, i.e. clicking, does not imply that the display should be updated. Add a repaint() at the end of your actionPerformed method. This will (at a basic level) call the paint method each time you click. |
Author: | nonamedude [ Sat May 08, 2010 2:30 pm ] |
Post subject: | Re: Help.... with graphics |
this is for school so i have to use rtp ![]() i have to make a game and i decided on doing count4 i'll try the repaint |
Author: | nonamedude [ Sat May 08, 2010 2:55 pm ] |
Post subject: | Re: Help.... with graphics |
The repaint works ![]() |
Author: | nonamedude [ Sat May 08, 2010 3:21 pm ] |
Post subject: | Re: Help.... with graphics |
I have come across another problem lol... Since i am doing count4, I have to change the color of the piece every turn. right now i am doing this static int check; static int count4[] [] = new int [7] [7]; public void actionPerformed (ActionEvent e) { if (e.getSource () == button1) { for (int x = 6 ; x >= 0 ; x--) { if (count4 [x] [0] == 0) { /////////////////////////////////////////////////////decides if the piece goes into the first coloumn count4 [x] [0] = 1; break; } } } public void paint (Graphics g) { for (int row = 6 ; row <= 0 ; row++) { for (int column = 6 ; column <= 0 ; column++) { check += count4 [row] [column]; } } if (check % 2 == 0) { g.setColor (Color.yellow); } if (check % 2 != 0) { g.setColor (Color.red); } if (count4 [6] [0] == 1) { g.fillOval (20, 795, 100, 100); check += 1; } } THE PROBLEM with this is that if a new piece is added all the pieces change color |
Author: | TheGuardian001 [ Sat May 08, 2010 4:21 pm ] | ||||
Post subject: | Re: Help.... with graphics | ||||
Well, you have one variable keeping track of what colour to use, but you have 49 things that you need to keep track of the colour of. Can you see any flaw in this? Now, even if you start using multiple variables (or an array) to say what colour each square is, you still have the problem that:
Neither of these are valid for loops. They have the same problem as your other one. However changing the sign to > won't help these ones, since you don't have a break in these for loops, meaning that you will make them go from not running to being infinite loops. I'm not sure what you were going for with these ones, but they won't work at all unless you completely restructure them. Oh, and in the future, please use code tags:
|
Author: | nonamedude [ Sat May 08, 2010 5:04 pm ] |
Post subject: | Re: Help.... with graphics |
Sorry for not using code tags. I'll rewrite the program by using a 2d array and assign a color for each square. |
Author: | nonamedude [ Sat May 08, 2010 5:34 pm ] |
Post subject: | Re: Help.... with graphics |
i got it to work ![]() |