I'm trying to call my paintComponent method by using repaint but it is never being called. This is my first class:
public class start
{
public static void main(String[] args){
Frame f = new Frame();
f.createFrame();
}
}
And this is the class that I want the paintComponent method to be called to but all that happens is a blank frame appears:
import javax.swing.JButton;
import javax.swing.JComponent;
import java.awt.Graphics;
import javax.swing.JFrame;
import java.awt.image.*;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.Timer;
public class Frame implements Runnable,ActionListener
{
JFrame window = new JFrame("Frame");
int i = 0;
Canvas myCanvas = new Canvas();
public void createFrame(){
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(30, 30, 700, 500);
window.setFocusable(true);
window.setFocusTraversalKeysEnabled(false);
window.setVisible(true);
(new Thread(new Frame())).start();
}
public void run(){
Timer timer = new Timer (17,this);
timer.start();
}
public void actionPerformed(ActionEvent e){
myCanvas.updateGame();
myCanvas.render();
window.add(myCanvas);
}
}
class Canvas extends JPanel{
int x = 10;
int y = 10;
public void updateGame(){
x++;
}
public void render(){
repaint();
System.out.println("repaint");
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g.drawString("hi",x,y);
System.out.println("paint");
}
}
Repaint is called multiple times but paint is never called. Why isn't the paintComponent method being called by repaint?
Instead of creating a new frame, pass in the existing frame, I've commented the line below:
You should really consider the following improvements as well:
1) Don't name your class
Frame
, as it collides with Frame2) Move the body of
createFrame
as well as the following three lines:Into a constructor and make these local variables like
Canvas
into member fields.3) Remove the last line that creates the thread out of the constructor, and expose it as a method (e.g 'startAnimation') and call it after you've created your object.
EDIT:
Try this: