如何使用 Java Swing 绘制图形
How to draw graphs using Java Swing
我正在学习Java Swing。我关注了一个 YouTube 讲座播放列表,该播放列表提供了用于绘制图形的 github 代码。我在这里提供基本结构:
package graphapp;
import com.sun.corba.se.impl.orbutil.graph.Graph;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class GraphApp extends JFrame {
int x,y;
int ax,by;
JComboBox cb,cb1;
String s="";
String se ="";
public GraphApp(){
setTitle("Graph App");
setSize(900,700);
String[] graphs = {"select..","parabola","ax^2+bx+c","ax^3","y=mx","y=mx+c","sin(x)","cos(x)","tan(x)","sinc function","signum(x)","X-graph","cubic function","sin+cos unequal amp","sin^3","cos^3","sin^3+cos^3","Amplitude Modulation"};
cb = new JComboBox(graphs);
cb.setBounds(700, 100, 120, 25);
add(cb);
cb.setEditable(false);
String[] select = {"Draw graph","Erase"};
cb1 = new JComboBox(select);
cb1.setBounds(700, 150, 120, 25);
add(cb1);
cb1.setEditable(false);
setLayout(null); //add it its very important otherwise Combo Box will occupy the whole screen.
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
x = 30; //x=200;
y = 300;
}
public void paint(Graphics g){
super.paint(g); //This method was not called in The Master Branch at github
g.setColor(Color.BLACK);
g.drawString("Design by ..", 700, 400);
g.drawString("Debasish Roy", 700, 420);
g.drawString("Y", 310, 40);
g.drawString("Y'", 310, 600);
g.drawString("X", 30, 314);
g.drawString("X'", 600, 314);
if(x==300&&y==300){
g.drawString("Origin(0,0)", 310, 314);
}
g.drawString("Select Graphs", 710, 100);
g.drawLine(300, 30, 300, 600);
g.drawLine(30,300,600,300);
if(x>599||y<40){
g.drawString((String) cb.getSelectedItem(), 200, 200);
s= String.valueOf(cb.getSelectedItem());
se = String.valueOf( cb1.getSelectedItem());
x=30;
y=300;
}
if(s.equals("parabola")&& se.equals("Draw graph")){
g.setColor(Color.GREEN);
run1(); // function to set x and y values
}
//Other checks to find the type of graph selected
else{
g.setColor(Color.white);
run();
}
g.fillOval(x, y, 3, 3);
repaint(); // When I run this code, the window keeps flickering. I think it is because this method called without any check.
//However I don't know much about this method
}
public void run(){
try{
Thread.sleep(1);
if(x<600&&y>30&&y!=600){
ax = x-300;
by = y-300;
ax++;
by = (int) (40*(1+1*Math.cos(.2*ax/3.14))*Math.cos(ax/3.14)+40*(40*Math.sin(.2*ax/3.14))/ax); // AM+sinc(x) function
x=300+ax;
y=300-by;
}
}catch(Exception e){
System.out.println("ERROR");
}
}
public static void main(String[] args){
new GraphApp();
Thread t1 = new Thread();
t1.start();
}
}
当我运行这段代码时,没有绘制任何图形。此外,当我更改 JComboBox 中的选择时,未检测到任何更改。请帮助我在这里抓住我想念的东西。
When I run this code, no graph gets drawn.
您不应覆盖 JFrame 上的 paint(...)。自定义绘画是通过覆盖 JPanel 的 paintComponent(...) 完成的,然后将面板添加到框架中。阅读有关 Custom Painting 的 Swing 教程部分,了解更多信息和工作示例以帮助您入门。
一种绘画方法,仅供绘画使用。你不应该调用 repaint()。 Swing 将确定组件何时需要重新绘制自身或您在外部方法中调用 repaint()。绘制代码应该只绘制组件的当前状态。如果您有更改的属性,那么您需要更改这些属性的方法,然后重新绘制()组件。
您不应在绘画方法中调用 Thread.sleep()。
您正在创建一个什么都不做的线程。如果要在线程启动时执行代码,则需要将可运行对象传递给线程。但是,在这种情况下,如果您想要图表的动画,那么您应该使用 Swing Timer 作为动画。当计时器触发时,您更新 class 的属性,然后调用 repaint().
Also, when I change selection in JComboBox no change is detected.
您需要向组合框添加一个ActionListener。阅读 How to Use Combo Boxes.
上的 Swing 教程部分
因此您需要花时间阅读 Swing tutorial
以学习一些 Swing 基础知识,然后再继续您的项目。
我正在学习Java Swing。我关注了一个 YouTube 讲座播放列表,该播放列表提供了用于绘制图形的 github 代码。我在这里提供基本结构:
package graphapp;
import com.sun.corba.se.impl.orbutil.graph.Graph;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class GraphApp extends JFrame {
int x,y;
int ax,by;
JComboBox cb,cb1;
String s="";
String se ="";
public GraphApp(){
setTitle("Graph App");
setSize(900,700);
String[] graphs = {"select..","parabola","ax^2+bx+c","ax^3","y=mx","y=mx+c","sin(x)","cos(x)","tan(x)","sinc function","signum(x)","X-graph","cubic function","sin+cos unequal amp","sin^3","cos^3","sin^3+cos^3","Amplitude Modulation"};
cb = new JComboBox(graphs);
cb.setBounds(700, 100, 120, 25);
add(cb);
cb.setEditable(false);
String[] select = {"Draw graph","Erase"};
cb1 = new JComboBox(select);
cb1.setBounds(700, 150, 120, 25);
add(cb1);
cb1.setEditable(false);
setLayout(null); //add it its very important otherwise Combo Box will occupy the whole screen.
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
x = 30; //x=200;
y = 300;
}
public void paint(Graphics g){
super.paint(g); //This method was not called in The Master Branch at github
g.setColor(Color.BLACK);
g.drawString("Design by ..", 700, 400);
g.drawString("Debasish Roy", 700, 420);
g.drawString("Y", 310, 40);
g.drawString("Y'", 310, 600);
g.drawString("X", 30, 314);
g.drawString("X'", 600, 314);
if(x==300&&y==300){
g.drawString("Origin(0,0)", 310, 314);
}
g.drawString("Select Graphs", 710, 100);
g.drawLine(300, 30, 300, 600);
g.drawLine(30,300,600,300);
if(x>599||y<40){
g.drawString((String) cb.getSelectedItem(), 200, 200);
s= String.valueOf(cb.getSelectedItem());
se = String.valueOf( cb1.getSelectedItem());
x=30;
y=300;
}
if(s.equals("parabola")&& se.equals("Draw graph")){
g.setColor(Color.GREEN);
run1(); // function to set x and y values
}
//Other checks to find the type of graph selected
else{
g.setColor(Color.white);
run();
}
g.fillOval(x, y, 3, 3);
repaint(); // When I run this code, the window keeps flickering. I think it is because this method called without any check.
//However I don't know much about this method
}
public void run(){
try{
Thread.sleep(1);
if(x<600&&y>30&&y!=600){
ax = x-300;
by = y-300;
ax++;
by = (int) (40*(1+1*Math.cos(.2*ax/3.14))*Math.cos(ax/3.14)+40*(40*Math.sin(.2*ax/3.14))/ax); // AM+sinc(x) function
x=300+ax;
y=300-by;
}
}catch(Exception e){
System.out.println("ERROR");
}
}
public static void main(String[] args){
new GraphApp();
Thread t1 = new Thread();
t1.start();
}
}
当我运行这段代码时,没有绘制任何图形。此外,当我更改 JComboBox 中的选择时,未检测到任何更改。请帮助我在这里抓住我想念的东西。
When I run this code, no graph gets drawn.
您不应覆盖 JFrame 上的 paint(...)。自定义绘画是通过覆盖 JPanel 的 paintComponent(...) 完成的,然后将面板添加到框架中。阅读有关 Custom Painting 的 Swing 教程部分,了解更多信息和工作示例以帮助您入门。
一种绘画方法,仅供绘画使用。你不应该调用 repaint()。 Swing 将确定组件何时需要重新绘制自身或您在外部方法中调用 repaint()。绘制代码应该只绘制组件的当前状态。如果您有更改的属性,那么您需要更改这些属性的方法,然后重新绘制()组件。
您不应在绘画方法中调用 Thread.sleep()。
您正在创建一个什么都不做的线程。如果要在线程启动时执行代码,则需要将可运行对象传递给线程。但是,在这种情况下,如果您想要图表的动画,那么您应该使用 Swing Timer 作为动画。当计时器触发时,您更新 class 的属性,然后调用 repaint().
Also, when I change selection in JComboBox no change is detected.
您需要向组合框添加一个ActionListener。阅读 How to Use Combo Boxes.
上的 Swing 教程部分因此您需要花时间阅读 Swing tutorial
以学习一些 Swing 基础知识,然后再继续您的项目。