在笛卡尔平面上绘制方程 [英] Plotting equations on a cartesian Plane
问题描述
嘿,我有点像Java的初学者,而我正在研究一个程序,该程序接受方程式的输入(例如y = 3x),并在笛卡尔坐标系上绘制可能的值,即单独的JPanel.
这是我的代码:这是Class 1
Hey, Im sort of a beginner of java, and i at the moment of working on a program that takes the input of an equation (eg. y=3x) and plots possible values for it on a cartesian plane thats on a seperate JPanel.
Here is my code: This is Class 1
import java.awt.Color;
import java.awt.Desktop;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Graphs extends JFrame implements KeyListener
{
GraphingPanel p = new GraphingPanel();
JPanel[] Panel = new JPanel[2];
JLabel[] Label = new JLabel[100];
JTextField[] Field = new JTextField[100];
JButton[] Enter = new JButton[100];
JButton[] Clear = new JButton[100];
JComboBox Subjects = new JComboBox();
JComboBox Topics = new JComboBox();
public Graphs()
{
super();
setSize(600, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setUndecorated(true);
setLocationRelativeTo(null);
LoadUI();
}
public void LoadUI()
{
Field[0] = new JTextField();
Panel[0] = new JPanel(null);
Panel[1] = p;
Field[0].setBounds(240, 20, 120, 30);
Field[0].addKeyListener(this);
Panel[0].add(Field[0]);
Panel[0].add(Panel[1]);
add(Panel[0]);
setVisible(true);
}
public static void main(String[] args)
{
Graphs Main = new Graphs();
}
@Override
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==10)
{
String str = Field[0].getText();
DrawGraph g = new DrawGraph(str);
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
第二类:
The Second Class:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class GraphingPanel extends JPanel
{
public GraphingPanel()
{
setBounds(50, 50, 500, 400);
setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
setBackground(Color.WHITE);
}
public void paintComponent(Graphics g)
{
super.paintComponents(g);
int panelWidth = getWidth();
int panelHeight = getHeight();
g.setColor(Color.WHITE);
g.fillRect(0, 0, panelWidth, panelHeight);
g.setColor(Color.BLACK);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(1f));
g2.draw(new Line2D.Double( 0, panelHeight/2, panelWidth ,panelHeight/2 ));
g2.draw(new Line2D.Double(panelWidth/2,0, panelWidth/2 ,panelHeight));
g2.setFont(new Font("Times New Roman", Font.PLAIN, 13));
for(int i=1;i<getWidth();i+=9)
{
g2.draw(new Line2D.Double(i, (getHeight()/2)-2, i, (getHeight()/2)+2));
}
for(int i=1;i<getHeight();i+=9)
{
g2.draw(new Line2D.Double((getWidth()/2)-2, i,(getWidth()/2)+2,i));
}
}
}
在用户将等式输入到Text字段后,我只是想弄清楚如何绘制折线图..您的帮助将不胜感激:)
Im just struggling to figure out how to plot a line graph after the user has input his equation into the Text field.. Your help will be greatly appreciated:)
推荐答案
一些一般:
some general:
GraphingPanel p = new GraphingPanel();
JPanel[] Panel = new JPanel[2];
JLabel[] Label = new JLabel[100];
JTextField[] Field = new JTextField[100];
JButton[] Enter = new JButton[100];
JButton[] Clear = new JButton[100];
JComboBox Subjects = new JComboBox();
JComboBox Topics = new JComboBox();
为什么要声明这么大的组件字段?
请使用小写字母命名它们,否则对象名称将引起麻烦.
对您的问题:
您需要repaint()
您的JPanel.
why do you declare such big fields of components??
Please name them variables starting with lower case, otherwise you will get in trouble with object names.
To your question:
You need to repaint()
your JPanel.
// Let's start with instances of what we really need:
// I'm using hungarian notation, which is one way to get a clear variable naming.
// the "o" in front stands for "Object".
// by the way: this is a comment, you should use some to make clear what you do.
GraphingPanel oGraphPanel = new GraphingPanel();
JPanel oPanel = new JPanel();
JLabel oLabel = new JLabel();
JTextField oField = new JTextField();
JButton oEnter = new JButton();
JButton oClear = new JButton();
JComboBox oSubjects = new JComboBox();
JComboBox oTopics = new JComboBox();
/*
* Ok, we've got our Components ready and now
* you should change your code to fit to this.
*/
// {YOUR CODE of class Graphs }
// to make it easier, I will add a methode for updating the GraphingPanel:
private void updateGraphPanel(){ // always make sure the component is there and initalized
if(null != oGraphPanel){
oGraphPanel.setText(oField.getText()); // passing the text from Graphs to GraphingPanel
oGraphPanel.repaint(); // this triggers the paint method of GraphinPanel to be carried out again
}
}
// GraphingPanel
public class GraphingPanel extends JPanel
{
private String strText=null; // init with null (=not valid)
public void setText(String strText){
this.strText = strText;
}
// {Your code of GraphingPanel}
}
就是这样.将Text传递到GraphingPanel,然后触发重新绘制.
此处尚未使用该文本-取决于您要使用的文本.但是它可用-很有趣.
-------------------------------------------------- ---------------------------
这是一个快速入门.将其与您的代码进行比较(日食:在Package Explorer中右键单击->与...进行比较...彼此之间).
That''s about all. the Text is passed to GraphingPanel and then the repaint is triggered.
The text is not yet used there - depends on what you want to do with it. But it''s available - so have fun.
-----------------------------------------------------------------------------
Here is a jump start. compare this to your code (eclipse: right click in Package Explorer -> Compare with...each other).
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
public class GraphingPanel extends JPanel
{
public GraphingPanel()
{
setBounds(50, 50, 500, 400);
setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
setBackground(Color.WHITE);
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponents(g);
int panelWidth = getWidth();
int panelHeight = getHeight();
g.setColor(Color.WHITE);
g.fillRect(0, 0, panelWidth, panelHeight);
g.setColor(Color.BLACK);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(1f));
g2.draw(new Line2D.Double( 0, panelHeight/2, panelWidth ,panelHeight/2 ));
g2.draw(new Line2D.Double(panelWidth/2,0, panelWidth/2 ,panelHeight));
g2.setFont(new Font("Times New Roman", Font.PLAIN, 13));
for(int i=1;i<getWidth();i+=9)
{
g2.draw(new Line2D.Double(i, (getHeight()/2)-2, i, (getHeight()/2)+2));
}
for(int i=1;i<getHeight();i+=9)
{
g2.draw(new Line2D.Double((getWidth()/2)-2, i,(getWidth()/2)+2,i));
}
}
public void setValue(int iValue) {
// dosomethingFancyWiththisValue(iValue);
this.repaint();
System.out.println("repainted");
}
}
public class Graphs extends JFrame
{
// renaming variables
private GraphingPanel oGraphPanel = new GraphingPanel();
private JPanel oPanel = new JPanel();
private JLabel oLabel = new JLabel();
private JTextField oField = new JTextField();
private JButton oEnter = new JButton();
private JButton oClear = new JButton();
private JComboBox oSubjects = new JComboBox();
private JComboBox oTopics = new JComboBox();
public Graphs()
{
super();
setSize(600, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// setUndecorated(true);
setLocationRelativeTo(null);
loadUI();
}
public void loadUI()
{
oField = new JTextField();
oPanel = new JPanel(null);
oField.setBounds(240, 20, 120, 30);
oField.addKeyListener(new KeyAdapter(){
@Override
public void keyPressed(KeyEvent e)
{// always try to work with constant values - it's safer!
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
String strValue = oField.getText();
System.out.println("Enter pressed. Value: " + strValue);
//You probably want numbers entered in the TextBox - so you should check for the entered Value to be a number:
validateIsNumber(strValue);
oGraphPanel.setValue(Integer.parseInt(strValue));
// DrawGraph g = new DrawGraph(oField.getText());
}
}
}
);
oPanel.add(oField);
oPanel.add(oGraphPanel);
add(oPanel);
setVisible(true);
}
private void validateIsNumber(String strValue) {
try{
Integer.parseInt(strValue); // just trying to parse the number, exception pops when not possible
} catch(Exception oException){
JOptionPane.showMessageDialog(this, oException.getLocalizedMessage(),
"oooops", JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args)
{
new Graphs();
}
}
这篇关于在笛卡尔平面上绘制方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!