swing
ग्राफिक्स
खोज…
ग्राफिक्स वर्ग का उपयोग करना
पहचान
Graphics
वर्ग आपको जावा घटकों जैसे कि Jpanel
पर आकर्षित करने की अनुमति देता है, इसका उपयोग तार, रेखाओं, आकृतियों और चित्रों को खींचने के लिए किया जा सकता है। यह उस JComponent
के paintComponent(Graphics g)
विधि को ओवरराइड करके किया जाता है, जिसे आप ड्राइंग करने के लिए तर्क के रूप में प्राप्त Graphics
ऑब्जेक्ट का उपयोग कर रहे हैं:
क्लास Board
import java.awt.*;
import javax.swing.*;
public class Board extends JPanel{
public Board() {
setBackground(Color.WHITE);
}
@override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
// draws a line diagonally across the screen
g.drawLine(0, 0, 400, 400);
// draws a rectangle around "hello there!"
g.drawRect(140, 180, 115, 25);
}
}
आवरण वर्ग DrawingCanvas
import javax.swing.*;
public class DrawingCanvas extends JFrame {
public DrawingCanvas() {
Board board = new Board();
add(board); // adds the Board to our JFrame
pack(); // sets JFrame dimension to contain subcomponents
setResizable(false);
setTitle("Graphics Test");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLocationRelativeTo(null); // centers window on screen
}
public static void main(String[] args) {
DrawingCanvas canvas = new DrawingCanvas();
canvas.setVisible(true);
}
}
रंग की
विभिन्न रंगों के साथ आकृतियाँ बनाने के लिए, आपको प्रत्येक सेट setColor
उपयोग से पहले Graphics
ऑब्जेक्ट के रंग को सेट करना होगा:
g.setColor(Color.BLUE); // draws a blue square
g.fillRect(10, 110, 100, 100);
g.setColor(Color.RED); // draws a red circle
g.fillOval(10, 10, 100, 100);
g.setColor(Color.GREEN); // draws a green triangle
int[] xPoints = {0, 200, 100};
int[] yPoints = {100, 100, 280};
g.fillPolygon(xPoints, yPoints, 3);
चित्र बनाना
छवियों को एक JComponent
पर खींचा जा सकता है जो कि क्लास Graphics
की drawImage
विधि का उपयोग कर रहा है:
एक छवि लोड हो रहा है
BufferedImage img;
try {
img = ImageIO.read(new File("stackoverflow.jpg"));
} catch (IOException e) {
throw new RuntimeException("Could not load image", e);
}
चित्र बनाना
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = 0;
int y = 0;
g.drawImage(img, x, y, this);
}
x
और y
छवि के ऊपरी-बाएँ का स्थान निर्दिष्ट करते हैं।
बेसिक एनीमेशन बनाने के लिए रिपीट विधि का उपयोग करना
MyFrame वर्ग JFrame का विस्तार करता है और इसमें मुख्य विधि भी होती है
import javax.swing.JFrame;
public class MyFrame extends JFrame{
//main method called on startup
public static void main(String[] args) throws InterruptedException {
//creates a frame window
MyFrame frame = new MyFrame();
//very basic game loop where the graphics are re-rendered
while(true){
frame.getPanel().repaint();
//The program waits a while before rerendering
Thread.sleep(12);
}
}
//the MyPanel is the other class and it extends JPanel
private MyPanel panel;
//constructor that sets some basic staring values
public MyFrame(){
this.setSize(500, 500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//creates the MyPanel with paramaters of x=0 and y=0
panel = new MyPanel(0,0);
//adds the panel (which is a JComponent because it extends JPanel)
//into the frame
this.add(panel);
//shows the frame window
this.setVisible(true);
}
//gets the panel
public MyPanel getPanel(){
return panel;
}
}
MyPanel वर्ग जो JPanel का विस्तार करता है और उसके पास पेंटकंपोनेंट विधि है
import java.awt.Graphics;
import javax.swing.JPanel;
public class MyPanel extends JPanel{
//two int variables to store the x and y coordinate
private int x;
private int y;
//construcor of the MyPanel class
public MyPanel(int x, int y){
this.x = x;
this.y = y;
}
/*the method that deals with the graphics
this method is called when the component is first loaded,
when the component is resized and when the repaint() method is
called for this component
*/
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
//changes the x and y varible values
x++;
y++;
//draws a rectangle at the x and y values
g.fillRect(x, y, 50, 50);
}
}