getGraphics() problem
hello guys,
I have some problem in Swing pl help,
What i have to do is draw a image on the JFrame so this is what i did
1. created a class
2. created the JFrame
3. got the image using getImage()
4. tried drawing it using drawImage()
but the image is not drawn
Here is the code
import java.awt.*;
import javax.swing.*;
class canvas12
{
JFrame jf;
Image img;
Toolkit tk;
Container cp;
canvas12()
{
jf=new JFrame("hello");
tk = Toolkit.getDefaultToolkit();
img = tk.getImage(getClass().getResource("puyo_blue.png"));
jf.setVisible(true);
jf.repaint();
}
publicvoid repaint()
{
Graphics g;
g = jf.getGraphics();
System.out.println(g);
g.drawImage(img,12,34,null);
}
publicstaticvoid main(String args[])
{
canvas12 c=new canvas12();
}
}
now the above class is not totaly correct i know i should have used the paintComponent() method must have subclassed JComponent, the class name is also unappropriate
but i want to do it using repaint() i know there is some error in my code because of which i 'm having problem drawing the image(one should not be using paint() when wortking with Swing)
what i lack is the concept behind the drawing of image i read books but i'm still not clear with the concepts pl help me understand the concept
Pl if you could correct my code and help me understand the point where i'm lacking i would be very gratefull
1) Don't mix AWT (Canvas) and Swing. Use a JPanel or JComponent as a canvas.2) Don't use repaint() to draw. Use the paintComponent() method of your chosen canvas.
I would definetly not mix Swing with AWT i did that intentionallybut doing so still works and in my case it doesn't work why sopl read my 1st query n help me grab the concept
> I would definetly not mix Swing with AWT i did that
> intentionally
Eh! This does not make sense! You 'definetly' have mixed AWT with Swing and I can't see how you can say you have not done so but in the same sentence say that you did it intentionally!
> but doing so still works and in my case it doesn't
> work
Eh! This does not make sense!
> why so
> pl read my 1st query n help me grab the concept
I have done but I until you have 'grabbed' the concept that AWT and Swing don't match I don't see any point in adding to the rubbish.
this is the below code of some other person i downloaded
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
// Referenced classes of package Puyo:
//PuyoLogic, Ball
public class PuyoWindow
implements KeyListener
{
JFrame mainFrame;
Canvas paintCanvas;
JLabel labelScore;
PuyoLogic puyoLogic;
public Image puyoImages[];
private Image doubleBuffer;
public static final int boardWidth = 6;
public static final int boardHeight = 12;
public static final int ballWidth = 32;
public static final int ballHeight = 32;
public static final int canvasWidth = 192;
public static final int canvasHeight = 384;
public PuyoWindow()
{
puyoLogic = null;
Toolkit toolkit = Toolkit.getDefaultToolkit();
puyoImages = new Image[4];
puyoImages[0] = toolkit.getImage(getClass().getResource("images/puyo_red.png"));
puyoImages[1] = toolkit.getImage(getClass().getResource("images/puyo_green.png"));
puyoImages[2] = toolkit.getImage(getClass().getResource("images/puyo_blue.png"));
puyoImages[3] = toolkit.getImage(getClass().getResource("images/puyo_yellow.png"));
mainFrame = new JFrame("PuyoWindow");
Container cp = mainFrame.getContentPane();
labelScore = new JLabel("Score:");
cp.add(labelScore, "North");
paintCanvas = new Canvas();
paintCanvas.addKeyListener(this);
paintCanvas.setSize(192, 384);
cp.add(paintCanvas, "Center");
JPanel panelButtom = new JPanel();
panelButtom.setLayout(new BorderLayout());
JLabel labelHelp = new JLabel("Start: S; Pause: P/SPACE", 0);
panelButtom.add(labelHelp, "North");
labelHelp = new JLabel("Speed: PgUp, PgDn", 0);
panelButtom.add(labelHelp, "Center");
labelHelp = new JLabel("Move/Rotate: Arrow keys", 0);
panelButtom.add(labelHelp, "South");
cp.add(panelButtom, "South");
mainFrame.addKeyListener(this);
//mainFrame.pack();
mainFrame.setResizable(false);
//mainFrame.setTitle("Puyo");
mainFrame.setDefaultCloseOperation(3);
mainFrame.setVisible(true);
beginPuyo();
}
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if(puyoLogic.running)
{
switch(keyCode)
{
case 38: // '&'
puyoLogic.rotateCW();
break;
case 40: // '('
puyoLogic.rotateCCW();
break;
case 37: // '%'
puyoLogic.moveLeft();
break;
case 39: // '\''
puyoLogic.moveRight();
break;
case 33: // '!'
puyoLogic.speedUp();
break;
case 34: // '"'
puyoLogic.speedDown();
break;
case 32: // ' '
case 80: // 'P'
puyoLogic.changePauseState();
break;
}
}
if(keyCode == 83 || keyCode == 10)
{
puyoLogic.running = true;
beginPuyo();
}
}
public void keyReleased(KeyEvent keyevent)
{
}
public void keyTyped(KeyEvent keyevent)
{
}
public void repaint()
{
Graphics g = paintCanvas.getGraphics();
if(doubleBuffer != null)
{
Graphics g2 = doubleBuffer.getGraphics();
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, 192, 384);
drawBalls(g2);
g2.dispose();
g.drawImage(doubleBuffer, 0, 0, null);
} else
{
doubleBuffer = paintCanvas.createImage(192, 384);
}
updateScore();
}
private int color2ImageIndex(int color)
{
return color - 1;
}
private void drawBalls(Graphics g)
{
label0:
{
label1:
{
if(puyoLogic == null)
{
break label0;
}
int i = 0;
do
{
PuyoLogic _tmp = puyoLogic;
if(i >= 2)
{
break label1;
}
g.drawImage(puyoImages[color2ImageIndex(puyoLogic.droppingBalls[i].color)], (puyoLogic.droppingBalls[i].x - 1) * 32, (12 - puyoLogic.droppingBalls[i].y) * 32, null);
i++;
} while(true);
}
for(int i = 1; i < 7; i++)
{
for(int j = 1; j < 13; j++)
{
PuyoLogic _tmp1 = puyoLogic;
if(puyoLogic.accumBalls[i][j] != 0)
{
g.drawImage(puyoImages[color2ImageIndex(puyoLogic.accumBalls[i][j])], (i - 1) * 32, (12 - j) * 32, null);
}
}
}
}
}
private void updateScore()
{
if(puyoLogic != null)
{
String s = (new StringBuilder()).append("Score: ").append(puyoLogic.score).toString();
labelScore.setText(s);
}
}
private void beginPuyo()
{
if(puyoLogic == null || !puyoLogic.running)
{
puyoLogic = new PuyoLogic(this);
(new Thread(puyoLogic)).start();
}
}
public static void main(String args[])
{
PuyoWindow puyoWin = new PuyoWindow();
}
}
i know he has mixed Swing with AWT now what i want to know is that what wrong do i do that i do not get my images drawn & the above code gets the images drawn
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import javax.imageio.*;
import javax.swing.*;
public class ImageComponent extends JPanel {
private static final long serialVersionUID = 0;
private BufferedImage image;
public ImageComponent(BufferedImage image) {
this.image = image;
}
public Dimension getPreferredSize() {
return new Dimension(image.getWidth(), image.getHeight());
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawRenderedImage(image, null);
}
public static void main(String[] args) throws IOException {
URL url = new URL("http://blogs.sun.com/roller/resources/jag/SouthParkJAG-small.png");
final JComponent comp = new ImageComponent(ImageIO.read(url));
EventQueue.invokeLater(new Runnable(){
public void run() {
JFrame f = new JFrame("Image");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(comp);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
}
if i convert this program to AWt then also i doesn't draws the image can anyone tell me why
why do we do super.paintComponent()
also i know that in AWT when we draw anything the paint method method gets called is that the same with paintComponent()
when we draw a Frame in AWT the paint method gets called automaticaly does the same happen in Swing(JFrame)
the below code works fine in AWT
class imgapp extends Frame {
private Image image;
public imgapp() {
super("Image application");
//setLayout(null);
image = getToolkit().getImage("puyo_blue.png");
setBounds(10,10,200,150);
setVisible(true);
}
public void paint(Graphics g) {
g.drawImage(image, 10, 30, 32, 32, this);
g.drawImage(image, 100, 30, 64, 64, this);
}
static public void main(String argv[]) {
new imgapp();
}
}
how do i do the same in Swing, pl remember that I'm new to Swing
I don't touch the old AWT, but in Swing, calling super.paintComponent() maydo things like paint the background.
when we draw a Frame in AWT the paint method gets called automaticaly does the same happen in Swing(JFrame)
Yes. For example repaint causes an paint event to be queued.
Later the framework processes that event and calls paint for you. As documented
in JComponent, [url=http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#paint(java.awt.Graphics)]paint[/url] calls:
1. paintComponent
2. paintBorder
3. paintChildren
in that order.
DrLaszloJamf in ur reply 5-10 u used a component comp and when it was supposed to be drawn the paintComponent method was called if i need to draw a image every time a key is pressed i need to call the paintComponent method at every key press how do i do that(how do i call paintComponent method i don't want to use the component comp as used by ur code)
and calling repaint would call the paintComponent method, n i need to call the repaint method on my JFrameeg. jframe.repaint()
If you want to comp to be repainted:comp.repaint();If you want your frame and everything in it to be repainted:frame.repaint();
is it a good coding prctice to impement listeners(as in AWT) in Swing or there is some other way of doing it in Swing
> why do we do super.paintComponent()
> also i know that in AWT when we draw anything the
> paint method method gets called is that the same with
> paintComponent()
> when we draw a Frame in AWT the paint method gets
> called automaticaly does the same happen in
> Swing(JFrame)
>
> the below code works fine in AWT
> > class imgapp extends Frame {
>
> private Image image;
>
> public imgapp() {
> super("Image application");
> //setLayout(null);
> image = getToolkit().getImage("puyo_blue.png");
> setBounds(10,10,200,150);
> setVisible(true);
> }
>
> public void paint(Graphics g) {
> g.drawImage(image, 10, 30, 32, 32, this);
> g.drawImage(image, 100, 30, 64, 64, this);
>
> }
>
> static public void main(String argv[]) {
> new imgapp();
> }
> }
>
> how do i do the same in Swing, pl remember that I'm
> new to Swing
I'm not sure that you have been reading what has been posted but I will have one last try.
1) When you override paint() you MUST call super.paint(g)
2) It is a bad idea to override paint() on a Frame. Override the paint() method on a Canvas and add an instance of the modified Canvas to your Frame.
