好的,所以我制作了这个程序,将图片移动到最后,当你点击一个按钮时,它会从图片中绘制一条线到此点。我可以制作整个按钮的东西,但是,我很喜欢布局和东西。因此,当我运行我的程序时,它会打开2个java程序,一个用于动画发生,一个用于按钮..我不想要那个..我希望按钮与动画在同一个java程序中。此外,我试图设置背景颜色,但它不工作..我相信它与框架或其他东西.. idk。请帮忙。这是我的主要方法课程:
$ LD_LIBRARY_PATH=/home/xy/Desktop/paho.mqtt.c/build/output/ ./MQTTTest
和图像动画:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class example extends JFrame implements Runnable, ActionListener{
private int W=1000;
private int H=1500;
private Thread thread;
private int c=1;
int q=0;
Image image= new ImageIcon("Pictures/car.png").getImage();
private move greenCar;
public example(){
setBounds(100,100,W,H);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setBackground(Color.CYAN);
thread= new Thread(this);
greenCar=new move(image,30,70,98,40);
thread.start();
}
public void paint(Graphics g){
super.paint(g);
//draws the image on the screen
g.drawImage(greenCar.getImg(), greenCar.getX(), greenCar.getY(), this);
//c becomes 2 if the image has reached x=699 and if the button is pressed, q becomes greater than 0
if(c==2 &&q>0)
{
//and when that happens, a line gets created from stretching from the picture to a point
g.drawLine(greenCar.getX(), greenCar.getY(), 10, 20);
g.setColor(Color.BLACK);
}
}
public static void main(String[] args) {
//new example();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new example().createGui();
}
});
}
protected void createGui() {
JFrame frame = new JFrame("Button");
frame.setSize(30, 30);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridBagLayout());
frame.add(panel);
frame.getContentPane().add(panel, BorderLayout.WEST);
GridBagConstraints c = new GridBagConstraints();
JButton button1 = new JButton("Profile");
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(40, 40, 40, 40);
panel.add(button1, c);
button1.addActionListener(this);
}
public void run() {
while(true)
{
//starting the animation
greenCar.animate();
repaint();
//if the picture is above 698, set c as 2
if(greenCar.getX()>698)
{
c=2;
}
try{
thread.sleep(13);
}
catch(InterruptedException e){
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
//q, which was initially 0, gets incremented
q++;
}
}