用Java画线
Posted
技术标签:
【中文标题】用Java画线【英文标题】:Draw with lines in Java 【发布时间】:2014-03-15 01:26:24 【问题描述】:我怎样才能像这样在java中绘制图形?
这是我的代码,它必须至少画出这个数字的一半
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class Main
public static void main(String[] a)
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(30, 30, 300, 300);
window.getContentPane().add(new MyCanvas());
window.setVisible(true);
class MyCanvas extends JComponent
private static final long serialVersionUID = 1L;
public void paint(Graphics g)
int i =0;
for ( i = 0; i < 100; i++)
int x=1+i*3;
g.drawLine(x, 200, 2+(x+(i/2)), 400-((i*i)/20));
我得到了这个。
【问题讨论】:
你能解释一下你的循环MyCanvas.paint(...)
背后的逻辑吗?
在我看来,你已经成功了一半。
【参考方案1】:
这是我想出来的,虽然有点不同:)
public void paint(Graphics g)
for (int i = 0; i < 100; i++)
int x = 1 + i * 3;
g.drawLine(x, 200, x + i, 400 - i * i / 20);
g.drawLine(600 - x, 200, 600 - (x + i), 400 - i * i / 20);
我们需要重新处理函数“400 - i * i / 20”。
【讨论】:
:D :D :D 更好 :D 我们将逐步解决这个问题 :D :D 我们需要对函数'400 - i * i / 20'进行返工。【参考方案2】:我这么多:D
float centerY = 250;
float x1 = 0;
float x2 = 0;
float y2 = 400;
float way2 = 0;
for (int i = 0; i < 125; i++)
x2 += cos(way2*PI/-180)*10;
y2 += sin(way2*PI/-180)*10;
way2 += centerY/y2*0.235*10;
x1 += y2/600*10;
g.drawLine(x1,centerY,x2,y2);
【讨论】:
【参考方案3】:一个小动画向您展示您需要在线条旋转方面寻找的逻辑。把这条线想象成时钟上的指针。如何为时钟上的手设置动画。这几乎是完全相同的概念。唯一的区别是x1
(时钟指针中心点的 x 点)在指针转动时沿x
轴(即y1
常数)移动,而不是保持静止.因此,对于时钟的每一个刻度(指针旋转),x 位置也会水平移动。我就是这么看的。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Main
public static void main(String[] a)
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
window.getContentPane().add(new MyCanvas());
window.pack();
window.setVisible(true);
class MyCanvas extends JPanel
int x1 = 0;
int rotate = 50;
List<Line> lines;
Timer timer = null;
public MyCanvas()
lines = new ArrayList<>();
timer = new Timer(75, new ActionListener()
public void actionPerformed(ActionEvent e)
if (rotate < -50)
((Timer) e.getSource()).stop();
else
lines.add(new Line(x1, rotate));
repaint();
x1 += 5;
rotate--;
);
JButton start = new JButton("Start the Magic");
start.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
timer.start();
);
add(start);
public Dimension getPreferredSize()
return new Dimension(502, 400);
private static final long serialVersionUID = 1L;
public void paintComponent(Graphics g)
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
for (Line line : lines)
line.drawLine(g);
class Line
int x1;
int rotate;
int y1 = 200;
public Line(int x1, int rotate)
this.x1 = x1;
this.rotate = rotate;
void drawLine(Graphics g)
int Radius = (int) (Math.min(getWidth(), getHeight()) * 0.4);
int sLength = (int) (Radius * 0.9);
int xSecond = (int) (x1 + sLength * Math.sin(rotate * (2 * Math.PI / 100)));
int ySecond = (int) (y1 - sLength * Math.cos(rotate * (2 * Math.PI / 100)));
g.setColor(Color.GREEN);
g.drawLine(x1, y1, xSecond, ySecond);
【讨论】:
Snx,这样画)线条怎么画得更丑? @Anton InMyCanvas
更改 int rotate = 60;
- if (rotate == -60)
- x1 += 5;
,在 Line
中将 Math.PI/60
都更改为 Math.PI/120
@Anton 我编辑了我的代码以反映您的要求。以上是关于用Java画线的主要内容,如果未能解决你的问题,请参考以下文章