java.awt.Canvas

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 ? ???

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

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?

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?

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.

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

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

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… :’(

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

Thank you! ;D ;D ;D

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

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

haha… ;D

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… :smiley:

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…

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 :slight_smile: , 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 :slight_smile: .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 :slight_smile: .

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? :’(

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

Thanks… :slight_smile:

Here is a simple example that should get you started.


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);
    }
}

Hey… Thanks…

I will update my progress… THanks!!!
;D ;D 8);D ;D

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


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?