したがって、リジッド部分がJPanel(キャンバス)を内部に持つJScrollPane(mainGUIクラス)の単純なグラフィックGUIを作成しました。ボックスクラスを使用して(ボックスクラスを使用して)描画し、それを実行しようとすると、スクロールするときにボックスタイプのレンダリングが遅すぎたり、何か説明が難しいWindowsXPグラフィックエラーが発生します。スクロールした後、一様でないグリフの描画を取得するにはどうすればよいですか?レンダリングオプションなどはありますか?JScrollPane内のJPanelスクロール中にグラフィックスの不具合が発生する
コード:事前に
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Grafin{
public static void main(String args[]){
mainGUI gui = new mainGUI();
}
}
class mainGUI{
private JFrame mainFrame;
private JPanel toolsPanel;
private JPanel canvasPanel;
public mainGUI(){
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
}catch(Exception e){
System.out.println(e.getMessage());
}
mainFrame = new JFrame("Grafin | untitled");
mainFrame.setLayout(new GridBagLayout());
mainFrame.setSize(920, 580);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
loadGUI();
}
public void loadGUI(){
GridBagConstraints c = new GridBagConstraints();
//toolsPanel (izq) y canvasPanel (der) dentro de mainFrame
toolsPanel = new JPanel(new GridBagLayout());
toolsPanel.setPreferredSize(new Dimension(250,580));
c.gridx = 0;
c.gridy = 0;
c.weightx = 0.1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
mainFrame.add(toolsPanel, c);
canvasPanel = new JPanel(new GridBagLayout());
canvasPanel.setBackground(Color.white);
canvasPanel.setPreferredSize(new Dimension(1500,1000));
JScrollPane canvasScroll = new JScrollPane(canvasPanel);
canvasScroll.setPreferredSize(new Dimension(670,580));
c.gridx = 1;
c.gridy = 0;
c.weightx = 0.9;
mainFrame.add(canvasScroll, c);
canvasScroll.setSize(canvasScroll.getPreferredSize());
canvasScroll.setBackground(Color.white);
//CanvasPanel:
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.weighty = 1;
Box b1 = new Box(10, 200, 30, 128);
Box b2 = new Box(200, 10, 120, 40);
canvasPanel.add(b1, c);
canvasPanel.add(b2, c);
mainFrame.pack();
}
}
class Box extends JPanel{
private Color borderColor;
private Color fillColor;
public int x;
public int y;
public int width;
public int height;
public Box(){
borderColor = Color.black;
fillColor = new Color(242, 242, 242);
x = y = 1;
width = height = 30;
}
public Box(int px, int py, int w, int h){
x = px;
y = py;
borderColor = Color.black;
fillColor = new Color(242, 242, 242);
width = w;
height = h;
}
public void changeFillColor(Color c){
fillColor = c;
}
public void changeBorderColor(Color c){
borderColor = c;
}
public void paint(Graphics g){
g.setColor(fillColor);
g.fillRect(x, y, width, height);
g.setColor(borderColor);
g.drawRect(x, y, width, height);
}
}
おかげ 幸せな休日
後半の応答のため申し訳ありませんが、決勝で忙しかったです。あなたの答えをありがとう、それを試してみると、それが動作するかどうかを確認してください。 –
アップデート:これは私がしたいように機能します。再びありがとう。 –
@ Luis_V:私に戻ってくれてありがとう、うまくいきました。 –