首先要做的事情 . 我不完全理解swing应用程序以及java如何完全工作,但我正在学习 . 这是一个单一的工作,我有一些问题,弄清楚如何在我的代码中实现重绘 . 我看了重绘是如何工作的,无法理解,所以这就是我在这里寻求帮助的原因 .
我需要解决的问题是:
- 如何在此代码中实现repaint()?
如果我在代码结构中犯了一些粗略的错误(可能是我的话),请指出它们 . 此外,如果有一个很好的地方看看重绘是如何工作,我会很乐意看到它 .
提前致谢!
编辑:抱歉留下一些东西 . 代码必须绘制一条贝塞尔线,然后一个对象必须从贝塞尔线的开始移动到它的末尾 . 我必须在代码中包含的一些东西是计时器,并通过在宽网上搜索我看到用定时器动画一个动作最好用重绘方法完成,这就是为什么我要实现重绘方法的原因 .
package moveTwo;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.Timer;
class Points {
public static int[] x = new int[3];
public static int[] y = new int[3];
public static double[] B = new double[3];
}
/**
*
* @author wildlander
*/
public class move extends javax.swing.JFrame {
public int xS;
public int yS;
public void rect() {
//coordinates for the start
xS = jPanel2.getWidth()/2;
yS = jPanel2.getHeight()/2;
}
public void blank() {
Graphics g = jPanel2.getGraphics();
//draws a blank panel
g.setColor(Color.white);
g.fillRect(0, 0, jPanel2.getWidth(), jPanel2.getHeight());
}
public void object() {
Graphics g = jPanel2.getGraphics();
//draws an object that is to be moved
g.setColor(Color.black);
g.drawRect(xS, yS, 20, 20);
g.fillRect(xS, yS, 20, 20);
}
public void bezier () {
Graphics g = jPanel2.getGraphics();
//draws the control points
int i;
for(i=0;i<3;i++){
g.setColor(Color.red);
g.drawOval(Points.x[i], Points.y[i], 3, 3);
g.drawOval(Points.x[i], Points.y[i], 3, 3);
}
//Bezier line
g.setColor(Color.green);
double t=0;
int j = 0;
while (t<1){
Points.B[0]= Math.pow(1-t,2);
Points.B[1]= 2*(1-t)*t;
Points.B[2]= Math.pow(t,2);
int x = (int)Math.round(Points.B[0] * Points.x[0] + Points.B[1] * Points.x[1] + Points.B[2] * Points.x[2]);
int y = (int)Math.round(Points.B[0] * Points.y[0] + Points.B[1] * Points.y[1] + Points.B[2] * Points.y[2]);
g.drawRect(x, y, 1, 1);
t = t + 0.0001;
}
}
Timer timerR;
/**
* Creates new form move
*/
public move() {
initComponents();
//timer for the animation
this.timerR = new javax.swing.Timer(100, new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
}
});
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel2 = new javax.swing.JPanel();
jToggleButton1 = new javax.swing.JToggleButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel2.setBackground(java.awt.Color.white);
jPanel2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jPanel2MouseClicked(evt);
}
});
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 801, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 500, Short.MAX_VALUE)
);
jToggleButton1.setText("pārvietot");
jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(127, 127, 127)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(482, 482, 482)
.addComponent(jToggleButton1)))
.addContainerGap(152, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jToggleButton1)
.addContainerGap(29, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jPanel2MouseClicked(java.awt.event.MouseEvent evt) {
// objects coordinates
Points.x[0] = xS + 10;
Points.y[0] = yS + 10;
// random control Point
Random num = new Random();
Points.x[1] = num.nextInt(jPanel2.getWidth());
Points.y[1] = num.nextInt(jPanel2.getHeight());
// mouse click coordinates
Points.x[2] = evt.getX();
Points.y[2] = evt.getY();
timerR.start();
}
1 回答
试试这个 :
就像这样,我无法完全完成你的代码,因为有一个神秘的变量“jPanel2”变量,但我希望这会有所帮助 .
谢谢 .