0
私が書いているJavaプログラムに関する助けを探しています。私は、JListの選択イベントによって生成された配列に保存されたデータに基づいて矩形で構成される単純な棒グラフを表示する必要があります。しかし、JPanelには何も描かれておらず、私はそれを修正する方法を理解できません。JListから長方形を描く方法選択
try
{
BufferedReader in = new BufferedReader((new FileReader("scores.txt")));
String sLine;
while ((sLine = in.readLine()) != null)
{
vecAthlete.addElement(new Stage3_DataFile(sLine));
}
String[] saAthlete = new String[vecAthlete.size()];
for (int iI = 0; iI < vecAthlete.size(); iI++)
saAthlete[iI] = vecAthlete.get(iI).sName;
jList_Athlete.setListData(saAthlete);
jList_Athlete.setSelectedIndex(0);
// Reading input line by line
// Close file
in.close();
}
catch (IOException e)
{
System.out.println("ERROR: Could not read text file!");
}
jList_Athlete.addListSelectionListener(new javax.swing.event.ListSelectionListener()
{
public void valueChanged(javax.swing.event.ListSelectionEvent e)
{
double[][] daRun = new double[2][7];
final double dScoreUsed = 5;
double dMinScoreRun1 = 10, dMaxScoreRun1 = 0;
double dMinScoreRun2 = 10, dMaxScoreRun2 = 0;
double dTotalRun1 = 0, dTotalRun2 = 0;
jLabel_Athlete.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).sName);
jLabel_Country.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).sCountry);
jLabel_Run1_Score_1.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun1[0]);
jLabel_Run1_Score_2.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun1[1]);
jLabel_Run1_Score_3.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun1[2]);
jLabel_Run1_Score_4.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun1[3]);
jLabel_Run1_Score_5.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun1[4]);
jLabel_Run1_Score_6.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun1[5]);
jLabel_Run1_Score_7.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun1[6]);
jLabel_Run2_Score_1.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun2[0]);
jLabel_Run2_Score_2.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun2[1]);
jLabel_Run2_Score_3.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun2[2]);
jLabel_Run2_Score_4.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun2[3]);
jLabel_Run2_Score_5.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun2[4]);
jLabel_Run2_Score_6.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun2[5]);
jLabel_Run2_Score_7.setText(vecAthlete.get(jList_Athlete.getSelectedIndex()).saTempRun2[6]);
daRun[0][0] = Double.parseDouble(jLabel_Run1_Score_1.getText());
daRun[0][1] = Double.parseDouble(jLabel_Run1_Score_2.getText());
daRun[0][2] = Double.parseDouble(jLabel_Run1_Score_3.getText());
daRun[0][3] = Double.parseDouble(jLabel_Run1_Score_4.getText());
daRun[0][4] = Double.parseDouble(jLabel_Run1_Score_5.getText());
daRun[0][5] = Double.parseDouble(jLabel_Run1_Score_6.getText());
daRun[0][6] = Double.parseDouble(jLabel_Run1_Score_7.getText());
daRun[1][0] = Double.parseDouble(jLabel_Run2_Score_1.getText());
daRun[1][1] = Double.parseDouble(jLabel_Run2_Score_2.getText());
daRun[1][2] = Double.parseDouble(jLabel_Run2_Score_3.getText());
daRun[1][3] = Double.parseDouble(jLabel_Run2_Score_4.getText());
daRun[1][4] = Double.parseDouble(jLabel_Run2_Score_5.getText());
daRun[1][5] = Double.parseDouble(jLabel_Run2_Score_6.getText());
daRun[1][6] = Double.parseDouble(jLabel_Run2_Score_7.getText());
for(int i = 0; i < 7; i++)
{
if(daRun[0][i] <= dMinScoreRun1)
{
dMinScoreRun1 = daRun[0][i];
}
if(daRun[0][i] >= dMaxScoreRun1)
{
dMaxScoreRun1 = daRun[0][i];
}
dTotalRun1 += daRun[0][i];
}
dTotalRun1 = (dTotalRun1 - dMinScoreRun1 - dMaxScoreRun1)/dScoreUsed;
jLabel_TotalRun1.setText(String.valueOf(dTotalRun1));
jLabel_Run1AddInfo.setText("Min Score: " + (dMinScoreRun1) + ", Max Score: " + (dMaxScoreRun1));
for(int i = 0; i < 7; i++)
{
if(daRun[1][i] <= dMinScoreRun2)
{
dMinScoreRun2 = daRun[1][i];
}
if(daRun[1][i] >= dMaxScoreRun2)
{
dMaxScoreRun2 = daRun[1][i];
}
dTotalRun2 += daRun[1][i];
}
dTotalRun2 = (dTotalRun2 - dMinScoreRun2 - dMaxScoreRun2)/dScoreUsed;
jLabel_TotalRun2.setText(String.valueOf(dTotalRun2));
jLabel_Run2AddInfo.setText("Min Score: " + (dMinScoreRun2) + ", Max Score: " + (dMaxScoreRun2));
if(dTotalRun1 >= dTotalRun2)
jLabel_FinalScore.setText(String.valueOf(dTotalRun1));
else
jLabel_FinalScore.setText(String.valueOf(dTotalRun2));
jPanel_Graph.repaint();
}
});
JPanelの定義
JPanel jPanel_Graph = new JPanel();
jPanel_Graph.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
jPanel_Graph.setBounds(729, 159, 700, 200);
frame.getContentPane().add(jPanel_Graph);
img = new BufferedImage(jPanel_Graph.getWidth(),
jPanel_Graph.getHeight(),
BufferedImage.TYPE_INT_RGB);
g2dimg = (Graphics2D)img.getGraphics();
// Draw a filled white coloured rectangle on the entire area to clear it.
g2dimg.setPaint(Color.WHITE);
g2dimg.fill(new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight()));
JPanelの描画イベント
class myJPanel extends JPanel
{
private Rectangle2D.Double rectangle;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for(int i = 0; i < 7; i++)
{
dHeight = daRun[0][i];
rectangle = new Rectangle2D.Double(dX, dY, dWidth, dHeight);
g2dimg.setPaint(Color.red);
g2dimg.draw(rectangle);
g2dimg.fill(rectangle);
dX += dXIncrement;
g2dimg.drawImage(img, 0, 0, null);
}
}
}
リストの選択イベント
ありがとうございます!
、グラフペインに直接更新したいデータを渡します実際にはそうだったのですが、実際にはうまくいくでしょう。 'jPanel_Graph'はペイントすべきことをどのように知っていますか? – MadProgrammer
'JPanel'(これはこれまでのモデル)がモデルのレンダリングを担当しているので、' ListSelectionListener'はそのモデルを生成し、 'JPanel'に渡して結果をレンダリングできるようにします。 – MadProgrammer
その結果をレンダリングするためにJPanelにその情報を実際に渡すにはどうすればいいですか? –