gpt4 book ai didi

java - 如何在 Java 中平滑滚动 JFrame

转载 作者:搜寻专家 更新时间:2023-11-01 04:00:44 26 4
gpt4 key购买 nike

我的 Java 应用程序中有一个 JFrame,它包含一个 JPanel,其中我有一些在运行时创建的绘图对象。问题是在为大数字滚动 JFrame 时,滚动变慢并且滚动条移动不流畅。请注意,我正在使用 Graphics 2D 对象并在滚动操作上执行 repaint

有什么方法可以使 JFrame 的滚动 Action 平滑吗?

部分代码

public class DiagramPanel implements MouseListener{

int click=0;
Point p1;
Point p2;
private Dimension panelDimension;
.... // variables

public void go() {
p1 = new Point();
p2 = new Point();

JFrame f = new JFrame();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setSize(1200,500);
panelx = new DiaPanel();
panelx.setOpaque(true);
panelx.setBackground(Color.white);
panelx.setAutoscrolls(true);
panelx.addMouseListener(this);


JScrollPane scrollPane = new JScrollPane();

// scrollPane.add(panelx);

ClassRectangle tempRect = null;
for (ClassRectangle rect : this.classRectangles) {
tempRect = rect;
}


Rectangle rect = new Rectangle();
rect.setBounds(tempRect.getW() - 100, 0, 1000,
tempLife.getEndpointY() * 500);

panelDimension = new Dimension(0,0);
for (ClassRectangle rectx : classRectangles){
panelDimension.width=rectx.getW()+300;
}
for (LifeLine life : lifeLines) {
panelDimension.height=life.getEndpointY()+300;
}

scrollPane.setViewportView(panelx);
panelx.computeVisibleRect(rect);
JScrollPane scrollPane1 = new JScrollPane(panelx);

panelx.setPreferredSize(panelDimension);
panelx.repaint();
panelx.revalidate();
p1.x=0;
p1.y=0;
p2.y=panelDimension.height;
p2.x=panelDimension.width;
f.add( scrollPane1);
scrollPane.revalidate();
f.setBackground(Color.white);
}

public DiagramPanel(ArrayList<Rectangle> classRectangles,
ArrayList<Pair> pairs, ArrayList<Line> lines,
ArrayList<Life> meth) {

// constructing obj of DrawingPanel Here
}

public class SeqDiaPanel extends JPanel {
/**
*
*/

private static final long serialVersionUID = 1L;

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d2 = (Graphics2D) g;
g2d2.setColor(Color.orange);

//grid
for (int i = 0; i < panelDimension.height; i++) {
g2d2.drawLine(0, 0 + i * 5, panelDimension.width+1000, 0 + i * 5);

}

for (int i = 0; i < panelDimension.width; i++) {
g2d2.drawLine(0 + i * 5, 0, 0 + i *5,panelDimension.height+300);
}

g2d2.setColor(Color.black);

// objects
.......... some objects here


}
}
// draw Lines

Stroke drawingStroke = new BasicStroke(2, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL, 0, new float[] { 5 }, 0);
// Stroke drawingStroke = new BasicStroke();
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(drawingStroke);
for (Line life : lines) {
g2d.drawLine(life.getStartpointX(), life.getStartpointY(),
life.getEndpointX(), life.getEndpointY());
panelDimension.height=life.getEndpointY()+300;
}

// draw methodLfe
for (Object2 ml1 : Obj2) {
g2d2.fill3DRect(ml1.StartX(), ml1.getMethodStartY(),
ml1.getBreadth(), ml1.getEndX(),true);
}

}


}

// tobeused

public int calculateWidth(String name){
Font font = new Font("Serif", Font.BOLD, 12);
FontMetrics metrics = new FontMetrics(font){

/**
*
*/
private static final long serialVersionUID = 1L;};
int tempInt2=SwingUtilities.computeStringWidth( metrics, name);
tempInt2=tempInt2+10;
return tempInt2;
}




/*public class MouseClick implements MouseListener{
Point p = new Point(0,0);
@Override
public void mouseClicked(MouseEvent evnt) {

p.x=evnt.getX();
p.y=evnt.getY();
System.out.println("MouseClicked @"+p.x+":"+p.y);

}

@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub

}

}*/

@Override
public void mouseClicked(MouseEvent evnt) {
click++;
if(click==1){
//Point p= new Point();
p1.x=evnt.getX();
p1.y=evnt.getY();
// System.out.println("MouseClicked1 @"+p1.x+":"+p1.y);
}

if(click==2){
p2.x=evnt.getX();
p2.y=evnt.getY();
//System.out.println("MouseClicked2 @"+p2.x+":"+p2.y);
click=0;
if(p1.x<p2.x&&p1.y<p2.y){
panelx.repaint();
}
else{

}

}/*else{
p1.x=0;
p1.y=0;
p2.x=panelDimension.width+500;
p2.y=panelDimension.height+700;
}*/
}

@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub

}
}

最佳答案

这个想法也许能帮到你

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;

public class TilePainter extends JPanel implements Scrollable {

private static final long serialVersionUID = 1L;

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
JFrame frame = new JFrame("Tiles");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(new JScrollPane(new TilePainter()));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
private final int TILE_SIZE = 50;
private final int TILE_COUNT = 100;
private final int visibleTiles = 10;
private final boolean[][] loaded;
private final boolean[][] loading;
private final Random random;

public TilePainter() {
setPreferredSize(new Dimension(TILE_SIZE * TILE_COUNT, TILE_SIZE * TILE_COUNT));
loaded = new boolean[TILE_COUNT][TILE_COUNT];
loading = new boolean[TILE_COUNT][TILE_COUNT];
random = new Random();
}

public boolean getTile(final int x, final int y) {
boolean canPaint = loaded[x][y];
if (!canPaint && !loading[x][y]) {
loading[x][y] = true;
Timer timer = new Timer(random.nextInt(500),
new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
loaded[x][y] = true;
repaint(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
}
});
timer.setRepeats(false);
timer.start();
}
return canPaint;
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Rectangle clip = g.getClipBounds();
int startX = clip.x - (clip.x % TILE_SIZE);
int startY = clip.y - (clip.y % TILE_SIZE);
for (int x = startX; x < clip.x + clip.width; x += TILE_SIZE) {
for (int y = startY; y < clip.y + clip.height; y += TILE_SIZE) {
if (getTile(x / TILE_SIZE, y / TILE_SIZE)) {
g.setColor(Color.GREEN);
} else {
g.setColor(Color.RED);
}
g.fillRect(x, y, TILE_SIZE - 1, TILE_SIZE - 1);
}
}
}

@Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(visibleTiles * TILE_SIZE, visibleTiles * TILE_SIZE);
}

@Override
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
return TILE_SIZE * Math.max(1, visibleTiles - 1);
}

@Override
public boolean getScrollableTracksViewportHeight() {
return false;
}

@Override
public boolean getScrollableTracksViewportWidth() {
return false;
}

@Override
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
return TILE_SIZE;
}
}

关于java - 如何在 Java 中平滑滚动 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11330268/

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