import java.awt.Graphics;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Panel extends JPanel implements ActionListener {
Timer t = new Timer(5, this);
public Panel(){
t.start();
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
public void paintComponent(Graphics g){
}
public void actionPerformed(ActionEvent e) {
repaint();
}
}
我想在绘画组件之外设置我的图形的形状和大小,但我不知道如何。我已经尝试过Oval o = new Oval但它不起作用请帮助我。
答案 0 :(得分:2)
@Jason的解决方案将起作用,但是如果你想进一步发展,你可以使用paintComponent
接口进行抽象,这样你的private java.util.List<Shape> shapesToFill = new ArrayList<>();
public Panel(java.util.List<Shape> shapes) {
this.shapesToFill.addAll(shapes);
//...
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
shapesToFill.forEach(g2d::fill);
}
方法就不需要知道它的绘画内容。
<p id="pop" value="$arrayName[$k]" onclick="popFunc(this.value)"><?php echo $arrayName[$k]; ?></p>
答案 1 :(得分:1)
你试过这个吗?
Ellipse2D.Double oval = new Ellipse2D.Double(x, y, width, height);
public void paintComponent(Graphics g)
{
g.fill(oval);
}
然后你可以用g2D填写它
答案 2 :(得分:1)
public class Panel
extends JPanel
implements ActionListener {
Timer t = new Timer(5, this);
Oval oval = new Oval(0, 0, 50, 100);
public void paintComponent(Graphics g) {
g.drawOval(oval.getX(), oval.getY(), oval.getWidth(), oval.getHeight());
}
static class Oval {
private int x;
private int y;
private int width;
private int height;
public Oval(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}