gpt4 book ai didi

java Swing : in paintComponent method how to know what to repaint?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:32:05 25 4
gpt4 key购买 nike

我的组件比屏幕大,部分组件没有显示(我将使用滚动条)。
当我在 paintComponent(g) 中收到调用时,我如何知道我应该绘制哪个区域?

最佳答案

我不确定这是不是你的意思,但问题是你每次接到电话时都必须在 JScrollPane 上调用 repaint()JPanelpaintComponent(Graphics g) 中,否则 JPanel 上的更新将不会在 JScrollPane 中可见.

我还看到你想使用 JScrollBar(或者你可能混淆了术语)?我推荐一个 JScrollPane

我做了一个小例子,它是一个 JPanel,它有一个网格,每 2 秒改变一次颜色(红色到黑色,反之亦然)。 JPanel/Grid 比 JScrollPane 大;不管我们必须在 JScrollPane 实例上调用 repaint() 否则网格不会改变颜色:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Test {

public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
new Test().createAndShowUI();
}
});
}

private void createAndShowUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initComponents(frame);
frame.setPreferredSize(new Dimension(400, 400));
frame.pack();
frame.setVisible(true);
}

private void initComponents(JFrame frame) {
JScrollPane jsp = new JScrollPane();
jsp.setViewportView(new Panel(800, 800, jsp));
frame.getContentPane().add(jsp);
}
}

class Panel extends JPanel {

private int across, down;
private Panel.Tile[][] tiles;
private Color color = Color.black;
private final JScrollPane jScrollPane;

public Panel(int width, int height, JScrollPane jScrollPane) {
this.setPreferredSize(new Dimension(width, height));
this.jScrollPane = jScrollPane;
createTiles();
changePanelColorTimer();//just something to do to check if its repaints fine
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < across; i++) {
for (int j = 0; j < down; j++) {
g.setColor(color);
for (int k = 0; k < 5; k++) {
g.drawRect(tiles[i][j].x + k, tiles[i][j].y + k, tiles[i][j].side - k * 2, tiles[i][j].side - 2 * k);
}
}
}
updateScrollPane();//refresh the pane after every paint
}

//calls repaint on the scrollPane instance
private void updateScrollPane() {
jScrollPane.repaint();
}

private void createTiles() {
across = 13;
down = 9;
tiles = new Panel.Tile[across][down];

for (int i = 0; i < across; i++) {
for (int j = 0; j < down; j++) {
tiles[i][j] = new Panel.Tile((i * 50), (j * 50), 50);
}
}
}

//change the color of the grid lines from black to red and vice versa every 2s
private void changePanelColorTimer() {
Timer timer = new Timer(2000, new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
if (color == Color.black) {
color = Color.red;
} else {
color = Color.black;
}
}
});
timer.setInitialDelay(2000);
timer.start();
}

private class Tile {

int x, y, side;

public Tile(int inX, int inY, int inSide) {
x = inX;
y = inY;
side = inSide;
}
}
}

Panel 类中,如果我们在 paintComponent(Graphics g) 中注释 updateScrollPane(); 行,我们将不会看到网格改变颜色.

关于java Swing : in paintComponent method how to know what to repaint?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12653927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com