수색…


Graphics 클래스 사용

소개

Graphics 클래스를 사용하면 Jpanel 과 같은 Java 구성 요소에 그릴 수 있으며 문자열, 선, 모양 및 이미지를 그리는 데 사용할 수 있습니다. 그리기를 위해 인수로받은 Graphics 객체를 사용하여 그리는 JComponentpaintComponent(Graphics g) 메서드를 재정의 하면됩니다.

계급 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);

그림 그리기

Graphics 클래스의 drawImage 메서드를 사용하여 이미지를 JComponent 그릴 수 있습니다.

이미지로드

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

xy 는 이미지의 상단 왼쪽 의 위치를 ​​지정합니다.

Repaint 메서드를 사용하여 기본 애니메이션 만들기

MyFrame 클래스는 JFrame을 확장하고 main 메소드도 포함합니다.

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

}

JPanel을 확장하고 paintComponent 메소드가있는 MyPanel 클래스

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

}


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow