Java 如何在秋千中画垂直线
我能画水平线,但不能画垂直线。请帮帮我Java 如何在秋千中画垂直线,java,swing,Java,Swing,我能画水平线,但不能画垂直线。请帮帮我 import javax.swing.*; import java.awt.*; import java.awt.geom.*; class Success extends JFrame{ public Success(){ JPanel panel=new JPanel(); getContentPane().add(panel); setSize(450,450);
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
class Success extends JFrame{
public Success(){
JPanel panel=new JPanel();
getContentPane().add(panel);
setSize(450,450);
JButton button =new JButton("press");
panel.add(button);
}
public void paint(Graphics g) {
super.paint(g); // fixes the immediate problem.
Graphics2D g2 = (Graphics2D) g;
Line2D lin = new Line2D.Float(20, 40, 850, 40);
g2.draw(lin);
}
public static void main(String []args){
Success s=new Success();
s.setVisible(true);
}
}
提前感谢。保持x坐标不变,并更改y坐标值,如下所示
Line2D lin=新的Line2D.Float(20,40,20,150)代码>
前两个值是直线起点的(x1,y1)值,最后两个值是直线终点的(x2,y2)。现在,我希望您理解为什么代码会生成一条水平线,以及绘制垂直线需要做什么。保持x坐标不变,并更改y坐标值,如下所示
Line2D lin=新的Line2D.Float(20,40,20,150)代码>
前两个值是直线起点的(x1,y1)值,最后两个值是直线终点的(x2,y2)。现在,我希望您理解为什么代码会产生一条水平线,以及绘制垂直线需要做些什么。我注意到了一些事情,其中一些已经被指出:
为了直接回答您的问题,这就是Swing组件的(x,y)坐标
保持垂直线的x坐标相同。如果在创建线构造函数时不知道x坐标在其中,请查看该构造函数的文档。如果您使用的是Eclipse,这意味着您应该将鼠标悬停在包含构造函数的代码上
你的线超出了JFrame的范围;相反,如果希望它从一端到另一端,请使用getWidth()
和getHeight()
方法
不应该每次重新绘制组件时都创建新行。相反,您应该在Success
类的某个地方创建行,实现ActionListener
,这样您就可以在每一帧更新代码,并在更新中调整行的大小,然后只将重新绘制保留到paintComponent
李>
在这种情况下,您不应该重写JFrame
,通常也不必重写
您应该重写paintComponent
方法,而不是paint
方法
我不认为你的双重缓冲是正确的,但我不能帮你李>
如果您想控制JPanel的getPreferredSize
方法的大小,那么重写该方法非常方便,但在这种情况下甚至不需要,因为将它添加到JFrame中会自动调整它的大小
在Swing的幕后有很多东西,这可能会让人困惑,因为通常你必须明确地说出来,但继续玩这个例子,你应该安全一段时间
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.*;
class Success extends JPanel implements ActionListener{
private final Timer timer = new Timer(20, this); // Create a timer that will go off every 20 ms
Line2D horizontalLine; // Declare your variables here, but don't initialize them
Line2D verticalLine; // That way, they can be accessed later in actionPerformed and repaint
// You might want to try frame.setResizable(false) if you want your frame
// and your panel to stay the same size.
private final Dimension prefPanelSize = new Dimension(450, 450);
public Success(){
super(); // Call the constructor of JPanel, the class this subclasses.
JButton button =new JButton("press");
this.add(button);
this.setSize(prefPanelSize);
horizontalLine = new Line2D.Float(0, 40, prefPanelSize.width, 40);
verticalLine = new Line2D.Float(prefPanelSize.width / 2, 0,
prefPanelSize.width / 2, prefPanelSize.height);
timer.start(); // Start the timer
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g); // fixes the immediate problem.
Graphics2D g2 = (Graphics2D) g;
g2.draw(horizontalLine);
g2.draw(verticalLine);
}
@Override
public Dimension getPreferredSize()
{
return prefPanelSize;
}
public static void main(String []args){
Success s = new Success();
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(new Dimension(450, 450));
frame.add(s);
}
// This method is called ever 20 ms because of the timer.
@Override
public void actionPerformed(ActionEvent e) {
int currWidth = getWidth();
int currHeight = getHeight();
horizontalLine.setLine(0, 40, currWidth, 40);
verticalLine.setLine(currWidth / 2, 0, currWidth / 2, currHeight);
}
}
我注意到一些事情,其中一些已经被指出:
为了直接回答您的问题,这就是Swing组件的(x,y)坐标
保持垂直线的x坐标相同。如果在创建线构造函数时不知道x坐标在其中,请查看该构造函数的文档。如果您使用的是Eclipse,这意味着您应该将鼠标悬停在包含构造函数的代码上
你的线超出了JFrame的范围;相反,如果希望它从一端到另一端,请使用getWidth()
和getHeight()
方法
不应该每次重新绘制组件时都创建新行。相反,您应该在Success
类的某个地方创建行,实现ActionListener
,这样您就可以在每一帧更新代码,并在更新中调整行的大小,然后只将重新绘制保留到paintComponent
李>
在这种情况下,您不应该重写JFrame
,通常也不必重写
您应该重写paintComponent
方法,而不是paint
方法
我不认为你的双重缓冲是正确的,但我不能帮你李>
如果您想控制JPanel的getPreferredSize
方法的大小,那么重写该方法非常方便,但在这种情况下甚至不需要,因为将它添加到JFrame中会自动调整它的大小
在Swing的幕后有很多东西,这可能会让人困惑,因为通常你必须明确地说出来,但继续玩这个例子,你应该安全一段时间
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.*;
class Success extends JPanel implements ActionListener{
private final Timer timer = new Timer(20, this); // Create a timer that will go off every 20 ms
Line2D horizontalLine; // Declare your variables here, but don't initialize them
Line2D verticalLine; // That way, they can be accessed later in actionPerformed and repaint
// You might want to try frame.setResizable(false) if you want your frame
// and your panel to stay the same size.
private final Dimension prefPanelSize = new Dimension(450, 450);
public Success(){
super(); // Call the constructor of JPanel, the class this subclasses.
JButton button =new JButton("press");
this.add(button);
this.setSize(prefPanelSize);
horizontalLine = new Line2D.Float(0, 40, prefPanelSize.width, 40);
verticalLine = new Line2D.Float(prefPanelSize.width / 2, 0,
prefPanelSize.width / 2, prefPanelSize.height);
timer.start(); // Start the timer
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g); // fixes the immediate problem.
Graphics2D g2 = (Graphics2D) g;
g2.draw(horizontalLine);
g2.draw(verticalLine);
}
@Override
public Dimension getPreferredSize()
{
return prefPanelSize;
}
public static void main(String []args){
Success s = new Success();
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(new Dimension(450, 450));
frame.add(s);
}
// This method is called ever 20 ms because of the timer.
@Override
public void actionPerformed(ActionEvent e) {
int currWidth = getWidth();
int currHeight = getHeight();
horizontalLine.setLine(0, 40, currWidth, 40);
verticalLine.setLine(currWidth / 2, 0, currWidth / 2, currHeight);
}
}
我猜你不明白图形上的X轴和Y轴是什么意思?不要覆盖JFrame,你没有给它添加任何UFC可选性。不要覆盖绘制,尤其是JFrame,它是一个非常复杂的组件,很容易填充绘制链,而且它不是双缓冲的。从JPanel扩展并替代paontComponent。然后你可以将它添加到JFrame的一个实例中……我猜你不明白图形上的X和Y轴是什么意思?不要覆盖JFrame,你没有向它添加任何UFC可选性。不要覆盖绘制,尤其是JFrame,它是一个非常复杂的组件,很容易填充绘制链,而且它不是双缓冲的。从JPanel扩展并替代paontComponent。然后可以将其添加到JFrame的实例中。。。