我刚刚开始在Java上使用图形,所以如果我问一些明显的东西,请耐心等待,我需要在JPanel中绘制函数的图形,图形必须在for循环中绘制,而新的x和y生成。这是循环:
for(int i=0;i<iter;i++) {
xv=(int)x0[0];
yv=(int)x0[1];
x0[0]=d1[0][0]*((e[0][1]+f[0][1])*x0[1]+b[0]);
x0[1]=d1[1][1]*((e[1][0]+f[1][0])*x0[0]+b[1]);
x=(int)x0[0];
y=(int)x0[1];
g2.drawLine(xv,yv,x,y);
}
我不知道如何在JPanel中显示该行,因为我只需要工作图,如果有一些更简单的解决方案,我已经准备好学习了。 提前谢谢。
编辑:这是我的完整代码
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package jacobi;
import java.awt.Color;
import java.awt.Graphics;
import java.util.LinkedList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
*
* @author Stella
*/
public class NewJFrame extends javax.swing.JFrame {
double a[][]=new double [2][2];
double b[]=new double[2];
double x0[]=new double[2];
double d1[][]=new double[2][2];
boolean appl=false;
double d[][]=new double [2][2];
double e[][]=new double [2][2];
double f[][]=new double [2][2];
int iter;
int x=0;
int y=0;
int xv=0;
int yv=0;
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jFrame1 = new javax.swing.JFrame();
jFrame2 = new javax.swing.JFrame();
jFrame3 = new javax.swing.JFrame();
jFrame4 = new javax.swing.JFrame();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jButton4 = new javax.swing.JButton();
javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
jFrame1.getContentPane().setLayout(jFrame1Layout);
jFrame1Layout.setHorizontalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jFrame1Layout.setVerticalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
javax.swing.GroupLayout jFrame2Layout = new javax.swing.GroupLayout(jFrame2.getContentPane());
jFrame2.getContentPane().setLayout(jFrame2Layout);
jFrame2Layout.setHorizontalGroup(
jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jFrame2Layout.setVerticalGroup(
jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
javax.swing.GroupLayout jFrame3Layout = new javax.swing.GroupLayout(jFrame3.getContentPane());
jFrame3.getContentPane().setLayout(jFrame3Layout);
jFrame3Layout.setHorizontalGroup(
jFrame3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jFrame3Layout.setVerticalGroup(
jFrame3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
javax.swing.GroupLayout jFrame4Layout = new javax.swing.GroupLayout(jFrame4.getContentPane());
jFrame4.getContentPane().setLayout(jFrame4Layout);
jFrame4Layout.setHorizontalGroup(
jFrame4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jFrame4Layout.setVerticalGroup(
jFrame4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Prima equazione");
jLabel2.setText("Da generare");
jLabel3.setText("Seconda equazione");
jLabel4.setText("Da generare");
jButton1.setText("Genera matrice");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Controlla se il metodo è applicabile");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("Risolvi equazione");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jLabel5.setText("Da controllare");
jLabel6.setText("Da risolvere");
jButton4.setText("Genera grafico");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton3)
.addComponent(jLabel2)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jButton2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel4)
.addComponent(jLabel3)))
.addComponent(jButton1)
.addComponent(jButton4)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 104, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jLabel3))
.addGap(33, 33, 33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jLabel4))
.addGap(31, 31, 31)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton4)
.addGap(38, 38, 38))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
for(int i=0; i<a.length; i++)
{ for(int j=0; j<a.length; j++)
{ a[i][j]=(int)(Math.random()*10)+1;
}
}
for(int i=0;i<b.length;i++)
{
b[i]=(int)(Math.random()*10)+1;
x0[i]=(int)(Math.random()*10)+1;
}
String c=a[0][0]+"x+"+a[0][1]+"y="+b[0];
String d=a[1][0]+"x+"+a[1][1]+"y="+b[1];
jLabel2.setText(c);
jLabel4.setText(d);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if((Math.abs(a[0][0])>Math.abs(a[0][1]))&&(Math.abs(a[1][1])>Math.abs(a[1][0])))
{
jLabel5.setText("Il metodo di Jacobi puテイ essere applicato.");
appl=true;
}
else
{
for(int i=0;i<2;i++)
{
d[i][i]=a[i][i];
}
e[1][0]=-a[1][0];
f[0][1]=-a[0][1];
for (int i=0;i<2;i++)
{
d1[i][i]=(Math.pow(d[i][i],-1));
}
double bj[][]=new double [2][2];
bj[0][0]=(d1[0][0]*(e[0][0]+f[0][0]))+(d1[0][1]*(e[1][0]+f[1][0]));
bj[0][1]=(d1[0][0]*(e[0][1]+f[0][1]))+(d1[0][1]*(e[1][1]+f[1][1]));
bj[1][0]=(d1[1][0]*(e[0][0]+f[0][0]))+(d1[1][1]*(e[1][0]+f[1][0]));
bj[1][1]=(d1[1][0]*(e[0][1]+f[0][1]))+(d1[1][1]*(e[1][1]+f[1][1]));
double norma = 0;
for (int i = 0; i < 2; i++) {
double sum = 0;
for (int j = 0; j < 2; j++) {
sum= sum+(Math.abs(bj[i][j])); // compute the column sum
}
if (sum > norma) {
norma = sum; // found a new largest column sum
}
}
if(norma<=1)
{
jLabel5.setText("Il metodo di Jacobi puテイ essere applicato.");
appl=true;
}
else{
jLabel5.setText("Il metodo di Jacobi non può essere applicato.");
}
}
}
boolean gr=false;
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(appl==true)
{
String ite=JOptionPane.showInputDialog("Inserire il numero di iterazioni");
iter=Integer.parseInt(ite);
for(int i=0;i<iter;i++)
{
xv=(int)x0[0];
yv=(int)x0[1];
x0[0]=d1[0][0]*((e[0][1]+f[0][1])*x0[1]+b[0]);
x0[1]=d1[1][1]*((e[1][0]+f[1][0])*x0[0]+b[1]);
x=(int)x0[0];
y=(int)x0[1];
}
jLabel6.setText("I risultati dopo "+iter+" iterazioni sono: x1="+x0[0]+" e x2="+x0[1]);
}
if(appl==false){
jLabel6.setText("Il metodo di Jacobi non può essere applicato.");
}
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
public void paint(Graphics g) {
g.drawLine(xv, yv, x, y);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JFrame jFrame1;
private javax.swing.JFrame jFrame2;
private javax.swing.JFrame jFrame3;
private javax.swing.JFrame jFrame4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
// End of variables declaration
}