これが私が構築しているアプリケーションですhttps://github.com/chrisbramm/LastFM-History-Graph .
以下は、 のコントローラ クラスの一部ですsrc/lastfmhistoryclasses
。がOutputGUIView
作成されると、3 つのコンポーネント a が作成され、これがと別のJPanel graphPanel
に追加されます。これらはすべて に追加されます。次に、以下のリスナーが の優先サイズを変更し、ボタンを初めて押すと、UI が更新されて、サイズが大きくなり、 のスクロールバーが変化したことを示します。JScrollPane graphScrollPanel
JPanel autocompletePanel
JFrame OutputGUIView
graphPanel
graphPanel
graphScrollPanel
ただし、別のボタンを押すと、優先サイズが変更されますが、UI は更新されません。たとえば、最大化してウィンドウのサイズを変更すると、更新されます。
class Zoom2000 implements ActionListener{
public void actionPerformed(ActionEvent e){
outputGUIView.graphPanel.setPreferredSize(new Dimension(screenWidth, 2000));
outputGUIView.graphScrollPanel.updateUI();
}
}
class ZoomDefault implements ActionListener{
public void actionPerformed(ActionEvent e){
outputGUIView.graphPanel.setPreferredSize(new Dimension(screenWidth, screenHeight));
outputGUIView.graphScrollPanel.updateUI();
}
}
class Zoom6000 implements ActionListener{
public void actionPerformed(ActionEvent e){
outputGUIView.graphPanel.setPreferredSize(new Dimension(screenWidth, 6000));
outputGUIView.graphScrollPanel.updateUI();
}
}
さまざまなコンポーネントで(および再描画)など、さまざまなことを試しましinvalidate/validate/revalidate
たが、graphPanelはそこに座っているだけで、ウィンドウのサイズを変更したときにのみ再描画します。
私が間違っていることはありますか?
編集の更新: GraphPanel クラスは次のとおりです。
package lastfmhistoryguis;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import javax.swing.*;
import de.umass.lastfm.*;
import lastfmhistoryclasses.*;
SuppressWarnings("serial")
public class GraphPanel extends JPanel {
private LastFMHistory graphData;
private int graphHeight;
private int graphWidth;
private int zoom;
private final int PAD = 20;
public GraphPanel(LastFMHistory model, int zoom){
this.graphData = model;
if (zoom != 1){
this.zoom = zoom;
}else{
this.zoom = 1;
System.out.println("getHeight() returning:" + getHeight());
}
System.out.println("Width" + getWidth() + "Height" + getHeight());
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("Drawing");
Graphics2D graph = (Graphics2D) g;
if (graphData == null) {
System.err.println("No data found");
} else {
System.out.println("paintComponent Width" + getWidth() + "Height" + getHeight());
graphWidth = getWidth() - 5 * PAD;
//graphHeight = getHeight() - 2 * PAD;
graphHeight = 6000 - 2* PAD;
System.out.println(graphWidth + ", " + graphHeight);
int x0 = graphWidth + PAD;
graph.draw(new Rectangle2D.Double(PAD, PAD, graphWidth, graphHeight));
double xInc = (double) (graphWidth) / (graphData.dayMax);
double secondHeight = (double) (graphHeight) / 86400;
for (int i = 0; i <= 86400; i++) {
if (i % 3600 == 0) {
graph.draw(new Line2D.Double(x0, (i * secondHeight) + PAD,
x0 + 10, (i * secondHeight) + PAD));
String hour = Integer.toString(i / 3600);
graph.drawString(hour, x0 + 15,
(int) ((i * secondHeight) + PAD));
}
}
for (Track t : graphData.history) {
if (t.getPlayedWhen() != null) {
Color color = t.getColour();
int duration = t.getDuration();
int day = Math.abs(t.getDay());
double dayOrigin = x0 - ((day + 1) * xInc);
double timeOrigin = t.getGraphHeight() * secondHeight + PAD;
double trackHeight = duration * secondHeight;
graph.setColor(color);
// System.out.println("PLOTTING TRACK, " + day + ", " +
// dayOrigin + ", " + t.getGraphHeight() + ", " + timeOrigin
// + ", " + trackHeight);
// graph.draw(new Rectangle2D.Double(dayOrigin, timeOrigin,
// xInc, trackHeight));
graph.fillRect((int) dayOrigin, (int) timeOrigin,
(int) xInc, (int) trackHeight);
}
}
for (int i = 0; i < graphData.dayMax;){
graph.draw(new Line2D.Double(x0 - i * xInc, PAD, x0 - i * xInc, graphHeight + PAD));
i = i + 7;
}
}
}
public void zoom(int zoom){
this.zoom = zoom;
repaint();
}
}
これは、大きな輪郭を描く四角形が描画される Graphics2D オブジェクトを作成し、各トラック四角形がこの Graphics2D グラフ オブジェクトのどこかに描画されます。ここで推奨されているようにsetPreferredSizeの使用を削除しましたが、GraphPanelのgraphHeightを6000などの高さに手動で設定すると、graphScrollPanelは、すべてのgraphHeightが実際に大きいため、含まれているgraphPanelが実際に大きいことを認識しません。行うことは、高さが ~6000px の長方形を描画することです。この場合、setPreferredSize を使用する必要がありますか、または Graphics2D オブジェクトのサイズを設定する別の方法はありますか?