用Java中的线绘制 [英] Draw with lines in Java
问题描述
如何在java中绘制图形? alt =
这是我的代码,它必须至少画出一半的数字
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);
}
}
类MyCanvas扩展JComponent {
private static final long serialVersionUID = 1L;
public void paint(Graphics g){
int i = 0; (i = 0; i <100; i ++){
int x = 1 + i * 3;
; $ x $ b g.drawLine(x,200,2+(x +(i / 2)),400 - ((i * i)/ 20));
}
}
}
我得到这个。
一个小动画向你展示你需要寻找线性旋转的逻辑。想想这条线就像一只手在钟上。如何在时钟上动画一只手?这几乎是完全相同的概念。唯一的区别是 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;
列表< Line>线;
定时器计时器=空;
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.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
timer.start();
}
});
add(start);
$ b 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());
(Line line:lines){
line.drawLine(g);
}
}
class行{
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);
}
}
}
How can I draw in java figure like this?
Here is my code which has to draw at least half of this figure
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));
}
}
}
And I get this one.
A little animation to show you the logic you need to be looking for in terms of line rotation. Think of the line like a hand on a clock. How would to animate a hand on a clock. It's pretty much the exact same concept. The only difference is that the x1
(the x point for the center point of the clock hand), instead of remaining still, it moves along the x
axis (which is the y1
constant) while the hand is turning. So for every tick of the clock (hand rotation), the x location is also moved horizontally. That's the way I looked at it.
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);
}
}
}
这篇关于用Java中的线绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!