JavaJFrame画布绘制点而不是线
我一直想设计一个龙曲线发生器 (如果您想了解有关该退房的信息,但这与问题无关) 龙曲线是一个重复的数学构造。 我已经为画布应该绘制的内容编写了一个生成器,它通过返回一个由“r”或“l”组成的字符数组来工作,表示下一步是向左还是向右。在这里的代码中,是方法JavaJFrame画布绘制点而不是线,java,swing,jframe,awt,java-canvas,Java,Swing,Jframe,Awt,Java Canvas,我一直想设计一个龙曲线发生器 (如果您想了解有关该退房的信息,但这与问题无关) 龙曲线是一个重复的数学构造。 我已经为画布应该绘制的内容编写了一个生成器,它通过返回一个由“r”或“l”组成的字符数组来工作,表示下一步是向左还是向右。在这里的代码中,是方法input()。这部分工作得很好 问题是,每当我想在画布上绘制它时(使用drawLine),它只将前两条线绘制为实际线,其余的只是点 点在正确的位置上,如果你把东西弄大了,你再也看不出区别了,但无论如何,那里应该有线 图片: 这是我使用的代码:
input()
。这部分工作得很好
问题是,每当我想在画布上绘制它时(使用drawLine
),它只将前两条线绘制为实际线,其余的只是点
点在正确的位置上,如果你把东西弄大了,你再也看不出区别了,但无论如何,那里应该有线
图片:
这是我使用的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/**
*
* Description
*
* @version 1.0 from 4/20/2016
* @author
*/
public class CurveGen extends JFrame {
// start attributes
private Canvas display = new Canvas();
private JButton startButton = new JButton();
private JLabel jLabel1 = new JLabel();
private JTextArea outText = new JTextArea("");
private JScrollPane outTextScrollPane = new JScrollPane(outText);
private JLabel jLabel2 = new JLabel();
private JSlider xSlider = new JSlider();
private JSlider ySlider = new JSlider();
private JNumberField iterationsNF = new JNumberField();
private JNumberField sizeNF = new JNumberField();
// end attributes
public CurveGen(String title) {
// Frame-Init
super(title);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
int frameWidth = 1022;
int frameHeight = 731;
setSize(frameWidth, frameHeight);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - getSize().width) / 2;
int y = (d.height - getSize().height) / 2;
setLocation(x, y);
setResizable(false);
Container cp = getContentPane();
cp.setLayout(null);
// start components
display.setBounds(16, 64, 601, 601);
cp.add(display);
startButton.setBounds(736, 464, 241, 129);
startButton.setText("START!");
startButton.setMargin(new Insets(2, 2, 2, 2));
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
startButton_ActionPerformed(evt);
}
});
startButton.setFont(new Font("Dialog", Font.BOLD, 36));
cp.add(startButton);
jLabel1.setBounds(760, 96, 75, 41);
jLabel1.setText("Iterations:");
cp.add(jLabel1);
outTextScrollPane.setBounds(728, 392, 257, 57);
cp.add(outTextScrollPane);
jLabel2.setBounds(768, 144, 67, 41);
jLabel2.setText("Size:");
cp.add(jLabel2);
xSlider.setBounds(0, 8, 633, 49);
xSlider.setMinorTickSpacing(25);
xSlider.setMajorTickSpacing(100);
xSlider.setPaintTicks(true);
xSlider.setPaintLabels(true);
xSlider.setToolTipText("Starting point y-coordinate");
xSlider.setMaximum(600);
xSlider.setValue(300);
cp.add(xSlider);
ySlider.setBounds(624, 56, 65, 625);
ySlider.setMinorTickSpacing(25);
ySlider.setMajorTickSpacing(100);
ySlider.setPaintTicks(true);
ySlider.setPaintLabels(true);
ySlider.setOrientation(SwingConstants.VERTICAL);
ySlider.setMaximum(600);
ySlider.setInverted(true);
ySlider.setValue(300);
ySlider.setToolTipText("Starting point x-coordinate");
cp.add(ySlider);
iterationsNF.setBounds(856, 96, 81, 41);
iterationsNF.setText("");
cp.add(iterationsNF);
sizeNF.setBounds(856, 144, 81, 41);
sizeNF.setText("");
cp.add(sizeNF);
// end components
setVisible(true);
} // end of public CurveGen
// start methods
public static void main(String[] args) {
new CurveGen("CurveGen");
} // end of main
public char[] input(int iter) {
char oldOut[] = new char[0];
for (int i=1;i<=iter;i++) {
char newOut[] = new char[((int)Math.pow(2, i))-1];
for (int n=0;n<oldOut.length;n++) {
newOut[n] = oldOut[n];
if (oldOut[n]=='r') {
newOut[newOut.length-n-1] = 'l';
}
if (oldOut[n]=='l') {
newOut[newOut.length-n-1] = 'r';
} // end of if
} // end of for
newOut[oldOut.length]='l';
oldOut = newOut;
} // end of for
return oldOut;
}
public void startButton_ActionPerformed(ActionEvent evt) {
int iterations = iterationsNF.getInt();
int size = sizeNF.getInt();
char com[] = input(iterations);
outText.setText(String.valueOf(com));
int dir = 0;
int newDir = 0;
int lastPos[] = {xSlider.getValue(),ySlider.getValue()-size};
int newPos[] = {0,0};
Graphics g = display.getGraphics();
g.clearRect(0,0,601,601);
g.drawLine(xSlider.getValue(),ySlider.getValue(),xSlider.getValue(),ySlider.getValue()-size);
for (int i=0;i<=com.length-1;i++) {
dir = newDir;
if (dir==0) {
if (com[i]=='l') {
newPos[0] = lastPos[0]-size;
newPos[1] = lastPos[1];
newDir = 3;
}
if (com[i]=='r') {
newPos[0] = lastPos[0]+size;
newPos[1] = lastPos[1];
newDir = 1;
}
}
if (dir==1) {
if (com[i]=='l') {
newPos[0] = lastPos[0];
newPos[1] = lastPos[1]-size;
newDir = 0;
}
if (com[i]=='r') {
newPos[0] = lastPos[0];
newPos[1] = lastPos[1]+size;
newDir = 2;
}
}
if (dir==2) {
if (com[i]=='l') {
newPos[0] = lastPos[0]+size;
newPos[1] = lastPos[1];
newDir = 1;
}
if (com[i]=='r') {
newPos[0] = lastPos[0]-size;
newPos[1] = lastPos[1];
newDir = 3;
}
}
if (dir==3) {
if (com[i]=='l') {
newPos[0] = lastPos[0];
newPos[1] = lastPos[1]+size;
newDir = 2;
}
if (com[i]=='r') {
newPos[0] = lastPos[0];
newPos[1] = lastPos[1]-size;
newDir = 0;
}
}
g.drawLine(lastPos[0],lastPos[1],newPos[0],newPos[1]);
lastPos=newPos;
} // end of for
} // end of startButton_ActionPerformed
// end methods
} // end of class CurveGen
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.event.*;
/**
*
*描述
*
*@version 1.0自2016年4月20日起
*@作者
*/
公共类CurveGen扩展JFrame{
//开始属性
私有画布显示=新建画布();
私有JButton startButton=新JButton();
私有JLabel jLabel1=新JLabel();
私有JTextArea outText=新JTextArea(“”);
私有JScrollPane outTextScrollPane=新JScrollPane(outText);
私有JLabel jLabel2=新JLabel();
private-JSlider xSlider=new-JSlider();
私有JSlider ySlider=新JSlider();
私有JNumberField iterationsNF=新JNumberField();
private JNumberField sizeNF=new JNumberField();
//结束属性
公共曲线(字符串标题){
//帧初始化
超级(标题);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
int frameWidth=1022;
int frameHeight=731;
设置大小(帧宽度、帧高度);
维度d=Toolkit.getDefaultToolkit().getScreenSize();
intx=(d.width-getSize().width)/2;
int y=(d.height-getSize().height)/2;
设定位置(x,y);
可设置大小(假);
容器cp=getContentPane();
cp.setLayout(空);
//启动组件
显示.设置边界(16,64,601,601);
cp.add(显示);
开始按钮。后退(7364241129);
setText(“开始!”);
开始按钮设置边距(新插图(2,2,2,2));
startButton.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件evt){
已执行的启动按钮(evt);
}
});
setFont(新字体(“对话框”,Font.BOLD,36));
cp.add(开始按钮);
jLabel1.立根(760,96,75,41);
jLabel1.setText(“迭代:”);
cp.add(jLabel1);
outTextScrollPane.setBounds(7283925757);
cp.add(outTextScrollPane);
jLabel2.setBounds(768144,67,41);
jLabel2.setText(“大小:”);
cp.add(jLabel2);
xSlider.setBounds(0,8633,49);
xSlider.setMinorTickSpacing(25);
xSlider.setMajorTickSpacing(100);
xSlider.setPaintTicks(true);
xSlider.setPaintLabels(true);
setToolTipText(“起始点y坐标”);
xSlider.setMaximum(600);
xSlider.setValue(300);
cp.add(xSlider);
ySlider.立根(624,56,65,625);
ySlider.setMinorTickSpacing(25);
ySlider.setMajorTickSpacing(100);
ySlider.setPaintTicks(真);
ySlider.setPaintLabels(真);
Y滑块设置方向(旋转角度垂直);
ySlider.设置最大值(600);
ySlider.setinversed(真);
ySlider.设定值(300);
ySlider.setToolTipText(“起点x坐标”);
cp.add(ySlider);
迭代NF.setBounds(856,96,81,41);
iterationsNF.setText(“”);
cp.add(迭代nf);
西泽夫.立根(856144,81,41);
sizeNF.setText(“”);
cp.add(sizeNF);
//端部元件
setVisible(真);
}//公共曲线终点
//启动方法
公共静态void main(字符串[]args){
新CurveGen(“CurveGen”);
}//主管道末端
公共字符[]输入(int iter){
字符oldOut[]=新字符[0];
对于(int i=1;i绘图代码应位于paint(Graphics)方法内,以便与渲染循环正确同步。在事件处理程序中,更新组件的数据模型(计算线并将其保留在组件内的数据结构中),然后调用方法repaint()触发事件渲染循环,该循环将调用绘制方法
还有一些其他的变体,但一般的想法是您更改数据,然后请求渲染。在其他情况下,渲染引擎也可能调用您的paint方法,而不仅仅是在您更改数据时,因此理想情况下是paint()具有快速渲染所需的所有数据,这意味着除了在图形对象上渲染之外,它不应执行计算或繁重的操作
这意味着您必须在一个新类中对JComponent进行子类化,并在其中实现绘制。该类应具有一个内部数据结构,其中的线条可在任何时间点进行渲染。然后在JFrame中使用您的新类。绘图代码应在绘制(图形)中方法与渲染循环正确同步。在事件处理程序中,更新组件的数据模型(计算行并将其保留在组件内部的数据结构中),然后调用方法repaint()触发事件渲染循环,该循环将调用您的绘制方法
还有一些其他的变体,但一般的想法是您更改数据,然后请求渲染。在其他情况下,渲染引擎也可能调用您的paint方法,而不仅仅是在您更改数据时,因此理想情况下是paint()具有快速渲染所需的所有数据,这意味着除了在图形对象上渲染之外,它不应执行计算或繁重的操作
这意味着您必须在一个新类中对JComponent进行子类化,并在其中实现绘画
lastPos=newPos;
public class DragonModel {
private Point startPoint;
private int size;
private char[] values;
public DragonModel(Point startPoint, int size, char[] values) {
this.startPoint = startPoint;
this.size = size;
this.values = values;
}
public Point getStartPoint() {
return startPoint;
}
public int getSize() {
return size;
}
public char[] getValues() {
return values;
}
}
public class DragonPane extends JPanel {
private DragonModel model;
public void setModel(DragonModel model) {
this.model = model;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (model != null) {
Graphics2D g2d = (Graphics2D) g.create();
int size = model.getSize();
int dir = 0;
int newDir = 0;
Point lastPos = model.getStartPoint();
Point newPos = new Point(0, 0);
for (char value : model.values) {
if (dir == 0) {
if (value == 'l') {
newPos.x = lastPos.x - size;
newPos.y = lastPos.y;
newDir = 3;
}
if (value == 'r') {
newPos.x = lastPos.x + size;
newPos.y = lastPos.y;
newDir = 1;
}
}
if (dir == 1) {
if (value == 'l') {
newPos.x = lastPos.x;
newPos.y = lastPos.y - size;
newDir = 0;
}
if (value == 'r') {
newPos.x = lastPos.x;
newPos.y = lastPos.y + size;
newDir = 2;
}
}
if (dir == 2) {
if (value == 'l') {
newPos.x = lastPos.x + size;
newPos.y = lastPos.y;
newDir = 1;
}
if (value == 'r') {
newPos.x = lastPos.x - size;
newPos.y = lastPos.y;
newDir = 3;
}
}
if (dir == 3) {
if (value == 'l') {
newPos.x = lastPos.x;
newPos.y = lastPos.y + size;
newDir = 2;
}
if (value == 'r') {
newPos.x = lastPos.x;
newPos.y = lastPos.y - size;
newDir = 0;
}
}
g.drawLine(lastPos.x, lastPos.y, newPos.x, newPos.y);
dir = newDir;
lastPos = new Point(newPos);
}
}
}
}
public void startButton_ActionPerformed(ActionEvent evt) {
int iterations = Integer.parseInt(iterationsNF.getText());
int size = Integer.parseInt(sizeNF.getText());
char com[] = input(iterations);
outText.setText(String.valueOf(com));
DragonModel model = new DragonModel(new Point(xSlider.getValue(), ySlider.getValue()), size, com);
display.setModel(model);
} // end of startButton_ActionPerformed