Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
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  
  paintComponent(Graphics g)  (Read 1282 times)
0 Members and 1 Guest are viewing this topic.
Offline ENC

Junior Devvie





« Posted 2006-04-12 11:44:41 »

Hi Again..


I have this piece of Code
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  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JLabel;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.JProgressBar;
import sun.java2d.SunGraphics2D;

public class MainPanel extends JPanel {

   
   private JPanel controlPanel = null;
   private JPanel statusPanel = null;
   private JLabel hplbl = null;
   private JProgressBar healthPB = null;
   private JLabel mplbl = null;
   private JProgressBar manaPB = null;
   private JProgressBar experiencePB = null;
   private JPanel chatPanel = null;
   private JPanel charUsePanel = null;
   

   private ArrayList actors;
   private BufferedImage background, backgroundTile;
   private int backgroundY;
   private SunGraphics2D graphics;


   public MainPanel() {
      super();
      initialize();
   }
   public void game(){
      initWorld();
   }

   private  void initialize() {
      this.setLayout(new BorderLayout());
      this.setSize(800,600);
      this.setOpaque(false);
      this.add(getControlPanel(), java.awt.BorderLayout.SOUTH);
   }
   public void paintComponent(Graphics g){
      super.paintComponent(g);
      g.fillRect(10,10,50,50);
      g.setColor(Color.RED);
      g.fillOval(220,250,50,50);
     
      graphics =(SunGraphics2D) g;
   }
   public void initWorld(){
     
      super.paint(graphics);
      graphics.fillRect(20,20,50,50);
      this.paint(graphics);
   }

   private JPanel getControlPanel() {
      if (controlPanel == null) {
         controlPanel = new JPanel();
         controlPanel.setLayout(new BorderLayout());
         controlPanel.add(getChatPanel(), java.awt.BorderLayout.EAST);
         controlPanel.add(getCharUsePanel(), java.awt.BorderLayout.CENTER);
         
      }
      return controlPanel;
   }
 
   private JPanel getStatusPanel() {
      if (statusPanel == null) {
         hplbl = new JLabel();
         mplbl = new JLabel();
         statusPanel = new JPanel();
         GridBagConstraints gbc = new GridBagConstraints();
         statusPanel.setLayout(new GridBagLayout());
         gbc.gridx = 0;
         gbc.gridy = 0;
         gbc.anchor = GridBagConstraints.CENTER;
         gbc.ipadx = 15;
         gbc.insets = new Insets(5,10,5,0);
         hplbl.setText("HP");
         mplbl.setText("MP");
         statusPanel.add(hplbl, gbc);
         gbc.gridx++;
         statusPanel.add(getHealthPB(), gbc);
         gbc.gridx = 0;
         gbc.gridy++;
         statusPanel.add(mplbl, gbc);
         gbc.gridx++;
         statusPanel.add(getManaPB(), gbc);
      }
      return statusPanel;
   }
 
   private JProgressBar getHealthPB() {
      if (healthPB == null) {
         healthPB = new JProgressBar();
         healthPB.setValue(75);
         healthPB.setString("Test");
         healthPB.setStringPainted(true);
      }
      return healthPB;
   }
 
   private JProgressBar getManaPB() {
      if (manaPB == null) {
         manaPB = new JProgressBar();
         manaPB.setString("test2");
         manaPB.setStringPainted(true);
         manaPB.setValue(65);
      }
      return manaPB;
   }

   private JProgressBar getExperiencePB() {
      if (experiencePB == null) {
         experiencePB = new JProgressBar();
         experiencePB.setStringPainted(true);
      }
      return experiencePB;
   }
 
   private JPanel getChatPanel() {
      if (chatPanel == null) {
         chatPanel = new JPanel();
      }
      return chatPanel;
   }

   private JPanel getCharUsePanel() {
      if (charUsePanel == null) {
         charUsePanel = new JPanel();
         charUsePanel.setLayout(new BorderLayout());
         charUsePanel.add(getExperiencePB(), java.awt.BorderLayout.NORTH);
         charUsePanel.add(getStatusPanel(), java.awt.BorderLayout.WEST);
      }
      return charUsePanel;
   }
  }


I have this problem that of which that I am unable to loop the repainting of components...

This means... after i have painted on to the component...

I want to loop the repainting to take place in a loop...

however... since Graphics is an abstract class I can't seem to figure it out...

any helpers?

Thanks you!  Smiley
Offline Kova

Senior Devvie





« Reply #1 - Posted 2006-04-12 12:00:12 »

Where's your loop anyway? How do you expect to loop through something if you don't have a loop to start with.  Tongue
If you're aiming for active rendering you need a game loop, where in every pass your stuff will be painted and game state updated. I can tell you basic logic how it could be done. For details I suggest Killer Game Programming in Java.

you start game
you construct everything on top of some drawable component, like JPanel ... use setIgnoreRepaint(true) to turn off OS painting
you enter a loop, ex: while (game_running) { ...}
in that loop you call to gameStateUpdate() to update the game and gameRender() to render to some offscreen BufferedImage which you'll later draw on screen (double buffering)
for that drawable component you override paint() (or paintComponent()?, whatever) method and paint offscreen buffered image you created on screen.
loop repeats until you finish the game or exit, then you set game_running to false and game ends.
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 836
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2006-04-12 13:05:47 »

@ENC

You can't (or... shouldn't) call Component.paintComponent(Graphics) yourself.

If you want to call it in a loop, call Component.repaint();
Everything else will be done for you.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jeff

JGO Coder




Got any cats?


« Reply #3 - Posted 2006-04-12 22:12:10 »

OR go to active rendering.

http://java.sun.com/docs/books/tutorial/extra/fullscreen/rendering.html

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline ENC

Junior Devvie





« Reply #4 - Posted 2006-04-14 14:35:03 »

Thanks.. I will update your all my progress...  Grin

Cheers!
Offline xiaohouzi

Junior Newbie





« Reply #5 - Posted 2006-04-18 10:10:28 »

As Kova said, you don't have a loop going.

The second example on this page is what you're looking for, in simple terms. The ClockApplet

http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter08/demoClock.html

implement runnable
create a new thread
start the new thread
override the run method
have your loop within the run method which calls repaint()

hope this helps.
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.

trollwarrior1 (34 views)
2014-11-22 12:13:56

xFryIx (73 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (66 views)
2014-11-07 19:57:14

TehJavaDev (70 views)
2014-11-03 22:04:50

BurntPizza (68 views)
2014-11-03 18:54:52

moogie (83 views)
2014-11-03 06:22:04

CopyableCougar4 (82 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!