Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Runtime problem with a simple program  (Read 849 times)
0 Members and 1 Guest are viewing this topic.
Avenger
Guest
« Posted 2006-01-06 13:13:45 »

Hey all , I am trying to get accustomed with swing , and windows , and JFrame .. JPanel and all those things.I had a book from long ago which contains some nice code examples and explains them.But for the love of god I can not understand why this poarticular code does not do what  thought it does.I am making a window with 3 buttons which when pressed change the panel's color to yellow , blue and red respectively.There is also a hotkey for each button. Ctrl + Y for yellow , Ctr + B for blue and Ctrl+ R for red.So here is the code.Any help would be really appreciated Smiley

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ActionTest
{
   public static void main(String[] args)
   {
      ActionFrame frame = new ActionFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
}

/**
A frame with a panel that demonstrates color change actions
*/


class ActionFrame extends JFrame
{
   public ActionFrame()
   {
      setTitle("ActionTest");
      setSize(DEFAULT_WIDTH , DEFAULT_HEIGHT);

      //add panel to frame

      ActionPanel panel = new ActionPanel();
      add(panel);
   }

   public static final int DEFAULT_WIDTH = 300;
   public static final int DEFAULT_HEIGHT = 200;
}

/**
A panel with buttons and keyboard shortcutsto change the background color
*/


class ActionPanel extends JPanel
{
   public ActionPanel()
   {
      //define actions

      Action yellowAction = new ColorAction("Yellow" , new ImageIcon("yellow-ball.gif") , Color.YELLOW);
      Action blueAction = new ColorAction("Blue" , new ImageIcon("blue-ball.gif") , Color.BLUE);
      Action redAction = new ColorAction("Red" , new ImageIcon("red-ball.gif") , Color.RED);

      //add buttons to these actions

      add(new JButton(yellowAction));
      add(new JButton(blueAction));
      add(new JButton(redAction));

      //associate the Y , B and R keys with names

      InputMap imap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

      imap.put(KeyStroke.getKeyStroke("ctrl Y") , "panel.yellow");
      imap.put(KeyStroke.getKeyStroke("ctrl B") , "panel.blue");
      imap.put(KeyStroke.getKeyStroke("ctrl R") , "panel.red");

      //associate the names with actions

      ActionMap amap = getActionMap();
      amap.put("panel.yellow" , yellowAction);
      amap.put("panel.blue" , blueAction);
      amap.put("panel.red" , redAction);
   }

   public class ColorAction extends AbstractAction
   {
      /**
      Constructs a color action
      @param name the name to show on the button
      @param icon the icon to display on the button
      @param c the background color
      */

      public ColorAction(String name , Icon icon , Color c)
      {
         putValue(Action.NAME , name);
         putValue(Action.SMALL_ICON , icon );
         putValue(Action.SHORT_DESCRIPTION , "Set panel color to "+name.toLowerCase());
      }

      public void actionPerformed(ActionEvent event)
      {
         Color c = (Color) getValue("color");
         setBackground(c);
      }
   }
}
Offline antoinelechacal

Junior Member





« Reply #1 - Posted 2006-01-06 13:27:30 »

First of all, I think you need a repaint() at the end of your actionPerformed() method so that the display is re drawn when changed.

My name is nobody
Offline pepijnve

Junior Member




Java games rock!


« Reply #2 - Posted 2006-01-06 13:30:39 »

Calling repaint is not needed. setBackground already takes care of invalidating the component and scheduling a repaint. In general it's rarely necessary to explicitly call repaint in swing.

The color you pass to the ColorAction constructor is not being stored anywhere. In actionPerformed you try to retrieve the color using
1  
getValue("color")

but this is never set, so the action always sets the background color to null. Adding
1  
putValue("color", c);

to the ColorAction constructor should do the trick.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Avenger
Guest
« Reply #3 - Posted 2006-01-06 13:43:27 »

Hey thanks ! It did work Smiley

But ... I should not have even posted here :p

Why ? Because even thought I read the code in the book  3 times I omitted that putCode() line all 3 times and did not see it  Roll Eyes

Thanks again mate Smiley

I am so excited about learning to actually do things with Java now , all these things with swing , learning UI components and swing .... I feel all warm and fuzzy inside  Smiley
Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #4 - Posted 2006-01-07 02:02:10 »

1  
setBackground already takes care of invalidating the component and scheduling a repaint. 


Invalidating? Unlikely, I'd say. It surely cannot be necessary to re-layout sub-components just in order to change the background color. It does repaint, though.
Offline pepijnve

Junior Member




Java games rock!


« Reply #5 - Posted 2006-01-09 20:35:39 »

Invalidating? Unlikely, I'd say. It surely cannot be necessary to re-layout sub-components just in order to change the background color. It does repaint, though.
Yeah well... that's what I meant Wink My bad...
Note to self: think first, type later
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (38 views)
2014-04-15 18:08:23

BurntPizza (34 views)
2014-04-15 03:46:01

UprightPath (50 views)
2014-04-14 17:39:50

UprightPath (32 views)
2014-04-14 17:35:47

Porlus (49 views)
2014-04-14 15:48:38

tom_mai78101 (71 views)
2014-04-10 04:04:31

BurntPizza (130 views)
2014-04-08 23:06:04

tom_mai78101 (230 views)
2014-04-05 13:34:39

trollwarrior1 (193 views)
2014-04-04 12:06:45

CJLetsGame (200 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!