Window is like a painter’s canvas.
App must paint its window contents
Java components paint themselves
Anything else: Programmer
Painting a Swing Component:
Three methods are at the heart of painting a swing component
paintComponent()
paintBorder()
paintChildren()
The three methods are invoked in this order:
paintComponent — The main method for painting. By default, it first paints the background. Then it performs any custom painting.
paintBorder — Tells the component's border (if any) to paint. Do not invoke or override this method.
paintChildren — Tells any components contained by this component to paint themselves. Do not invoke or override this method
Yours' Painting Strategy:
Steps
Subclass JPanel
public class MyPanel extends JPanel
Override the public void paintComponent(Graphics g) method
Inside method using graphics object g and, do whatever drawing you want to do.
Install that JPanel inside a JFrame
When frame becomes visible through the paintChildren() method your panel become visible
To become visible your panel will call paintComponent() method which will do your custom drawing
//PainPanel.java
import java.awt.*;
import javax.swing.*;
public class PaintPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);//For calling super class constructor to erase behavior
Graphics2D g2D = (Graphics2D)g;
g2D.drawRect(20,20,20,20);
g2D.setColor(Color.blue);
g2D.fillOval(50,50,20,20);
g2D.drawString("Hello World", 120, 50);
}
}
//PaintOnFrame.java
import java.awt.*;
import javax.swing.*;
public class PaintOnFrame {
JFrame myFrame = null;
PaintPanel pp = null; //yours PaintPanel objectRefernce
Container con = null;
public PaintOnFrame() {
myFrame = new JFrame();
con = myFrame.getContentPane();
con.setLayout(new BorderLayout());
//Here is creating PaintPanel Object
pp = new PaintPanel();
con.add(pp);//now adding that PaintPanel object which shows your painting
myFrame.setSize(200,200);
myFrame.setVisible(true);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new PaintOnFrame();
}
} Animation:
Constantly need to call paintComponent() and draw the shape at new place (new co-ordinates).
Painting is managed by system and calling paintComponent() directly is not recommended at all.
Use Repaint() mechanism.
//AnimatePanel.java
import java.awt.*;
import javax.swing.*;
public class AnimatePanel extends JPanel {
int mX = 200;
int mY = 0;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.fillOval(mX, mY, 20, 20);
}
}
//AnimateOnFrame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AnimateOnFrame implements ActionListener {
JFrame myFrame = null;
PaintPanel pp = null;
Container con = null;
int x = 5 , y = 3;
public AnimateOnFrame() {
myFrame = new JFrame();
con = myFrame.getContentPane();
con.setLayout(new BorderLayout());
pp = new PaintPanel();
con.add(pp);
myFrame.setSize(400,400);
myFrame.setVisible(true);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Timer t = new Timer (5, this);
t.start();
}
public void actionPerformed(ActionEvent e) {
if (myFrame.getWidth()-40 == pp.mX)
x= -5;
if (myFrame.getHeight()-40 == pp.mY)
y= -3;
if (pp.mX == 0 )
x = 5;
if (pp.mY == 0 )
y = 3;
pp.mX += x;
pp.mY += y;
pp.repaint();
}
public static void main(String[] args) {
new AnimateOnFrame();
}
0 comments:
Post a Comment