Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
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  
  java.awt.Canvas  (Read 1568 times)
0 Members and 1 Guest are viewing this topic.
Offline ENC

Junior Member





« Posted 2006-03-23 07:54:20 »

Hi ... I would like to know how to place a button on the awt.Canvas... since it extends from Container it should be able to put other components on it correct ?  Huh

I am really confuse... please help me with this... cause I dont see the buttons I want on  the canvas.
Offline Mr_Light

Senior Member




shiny.


« Reply #1 - Posted 2006-03-23 12:24:04 »

http://www.scit.wlv.ac.uk/~jphb/java/images/java.awt.gif
Since it doesn't you shouldn't be able.  Wink

confused with Panel are we?

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline ENC

Junior Member





« Reply #2 - Posted 2006-03-23 13:02:53 »

oh... I am trying to use canvas to do a game... most of my friends says that drawing graphics on a panel is not possible....

so what i want to know is that if a panel cannot do it? then painting on a canvas should be the right thing correct?

but the problem is that i have seen some games that panels overlap the graphics.... but due to the reason that canvas is a heavy component so how do i make the buttons be seen with the graphics?? cause i cant seem to get it right...

i have tried to overlap the canvas to allow the buttons to be seen ... but failed... do you think you can help me with this?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #3 - Posted 2006-03-23 17:25:56 »

Your friends are totally wrong.  You should use JPanel to do custom drawing.  Just subclass JPanel and override paintComponent(...).  Just don't forget to call super.painComponent(...) before doing your custom drawing.  You may also want to override getPreferredSize() to return the size you want the JPanel to be.  This will allow layout managers and scroll panes to properly display your panel.

Offline Mr_Light

Senior Member




shiny.


« Reply #4 - Posted 2006-03-23 22:09:21 »

go swing for overlapping or implent your own components if your doign active rendering. overlay heavy components doesn't work well.


It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline noblemaster

JGO Ninja


Medals: 20
Projects: 10


Age of Conquest makes your day!


« Reply #5 - Posted 2006-03-23 22:12:15 »

Yes, Swing is great, just override the necessary paintComponent(Graphics g) methods for your own look & feel as well as game graphics.

Offline ENC

Junior Member





« Reply #6 - Posted 2006-03-24 04:52:32 »

oh ok... I will try that method...

then may I know what does the createBufferStrategy(int numberOfBuffers) do? Cause it seems that only canvas and window have it... other components like JPanel/ Panel, Frame /JFrame does not have it...

it is really possible to create a game without this method?

cause i tried ... it seems that createBufferStrategy is the answer for a smooth game..... Cry

Can someone write a sample code to see? Then maybe I can ref the codes?  Grin

Thank you! Grin Grin Grin
Offline noblemaster

JGO Ninja


Medals: 20
Projects: 10


Age of Conquest makes your day!


« Reply #7 - Posted 2006-03-24 05:25:01 »

Swing has "setDoubleBuffered(boolean aFlag)". What game are you trying to do?

Offline ENC

Junior Member





« Reply #8 - Posted 2006-03-24 06:14:20 »

oh ... I am trying to do a simplified version of SilkRoad but it is 2D not 3D

haha.. Grin

but then problem is that there are able to put buttons or at least panels i think... on to the graphics screen...

been trying to replicate it... but still failed

so yah..

Thanks kingaschi...

i will take note of it.. Cheesy

but still I wish someone can give me some codes to see.... cause i am not a hardcore programmer.. i need codes to understand...

then i can write out my own set of codes... lolz.....
Avenger
Guest
« Reply #9 - Posted 2006-03-24 11:56:54 »

If you want to see codes you can very easily read 2 of the very best tutorials out there.

First tutorial comes in spanish too ^_^  , it is really well wrote , helped me a lot .

As for createBufferStrategy method , that one too is covered in the 11th page of the tutorial.The tutorial itself though is made so you read it page by page .. it makes deliberate mistakes in many of the pages and then in the next page explains why that was a mistake.So I suggest you read it page by page.


And ofcourse the second tutorial by a member of this forum Smiley .It has many things which the first tutorial I linked lacks so I suggest you read it too thoroughly!Also it has a walkthrough for java webstart.Hey ... I need to read that tutorial too ^_^ .
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ENC

Junior Member





« Reply #10 - Posted 2006-03-24 16:51:51 »

Thanks Avenger! But then the first tutorial is where my question comes from...
cause the game is built on a canvas... but the problem here is that since canvas is a heavy component.. it will be like a "window" if other components tries to overlap...

that's why I ask... is there any way that I can place buttons or panels on the canvas...

the layout is something like this....

          ------------------------------------------------------------
          |                                                                              |
          |                                                                              |
          |                                                                              |            <----- This is supposely a canvas
          |                 Graphics during game play            |
          |                                                                              |
          |            -----------------------------------------           |
          |            | This is the panel for the btns|           |
          ------------------------------------------------------------

Yeah.. something like that.... but i am unable to create anything like that....
does anyone knows who to make this thing???

if cannot create on the canvas, then can someone show me how is it done on a panel?  Cry

cause i am really really reach my limits le... just turn the library upside down but dont have any books that have the answer.  Sad

Thanks... Smiley
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #11 - Posted 2006-03-24 19:23:57 »

Here is a simple example that should get you started.
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  
import java.awt.*;
import javax.swing.*;

public class Test extends JPanel {
    public Test() {
        setLayout(new BorderLayout());
        JPanel p = new JPanel();
        p.setOpaque(false);
        p.add(new JButton("Test button"));
        add(p, 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);
    }
    public Dimension getPreferredSize() {
        return new Dimension(400, 300);
    }
    public static void main(String args[]) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new Test());
        f.pack();
        f.setVisible(true);
    }
}

Offline ENC

Junior Member





« Reply #12 - Posted 2006-03-25 06:27:59 »

Hey.. Thanks....

I will update my progress... THanks!!!!
 Grin Grin  CoolGrin Grin
Offline ENC

Junior Member





« Reply #13 - Posted 2006-03-31 06:44:49 »

Hi ... i am trying to replicate the same code above but then it does not work? Can someone explain why?

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  
import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;

public class MainPanel extends JPanel {

   
   public MainPanel() {
      super();
      initialize();
   }
   
   privateĀ  void initialize() {
      this.setLayout(new BorderLayout());
      this.setSize(800,600);
      this.setOpaque(false);
   }
   public void paintComponents(Graphics g){
      super.paintComponents(g);
      g.fillRect(10,10,50,50);
      g.setColor(Color.RED);
      g.fillOval(220,250,50,50);
   }
}


oh yah... I am using the IBM Rational Application Developer application ... so I tried to run the above code above with Java Beans (Running the application without "public static void main(String [] args)" )

someone can help?
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.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (25 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (13 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (43 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
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!