import java.awt.geom.Ellipse2D;
import java.awt.event.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.RectangularShape;public class Test extends JFrame{ private JPanel contentPane, top;
private JButton start, stop;
private DrawingPane bottom;
private final int PANE_WIDTH = 400, PANE_HEIGHT = 400;
double x = 0;
double y = 0;
double width = PANE_WIDTH;
double height = PANE_HEIGHT;
boolean isShrinking = false;
Timer t;
public static void main(String[] args) { Test frame = new Test();
frame.setVisible(true); } /**
* Create the frame.
*/
public Test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 430, 490);
top = new JPanel();
start = new JButton("Start");
stop = new JButton("Stop"); top.add(start);
top.add(stop);
bottom = new DrawingPane();
bottom.setSize(PANE_WIDTH, PANE_HEIGHT);
bottom.setBorder(BorderFactory.createLineBorder(Color.BLACK)); contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout());
contentPane.add("North", top);
contentPane.add("Center", bottom);
setContentPane(contentPane);
exampleDraw();
isShrinking = true;
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
t = new Timer(1000, this);
t.start();
if(isShrinking){
shrink();
}else{
grow();
}
exampleDraw();
if(width < 10){
isShrinking = false;
}else if (width >399){
isShrinking = true;
}
}
});
stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
t.stop();
}
});
} void exampleDraw() {
Ellipse2D.Double circle;
circle = new Ellipse2D.Double(x, y, width, height);
bottom.drawShape(circle);
}
void shrink(){
if(width > 0 && height >0){
x += 5;
y += 5;
width -= 10;
height -= 10;
}
}
void grow(){
if(width < 400 && height < 400){
x -= 5;
y -= 5;
width += 10;
height += 10;
}
}
}
class DrawingPane extends JPanel { RectangularShape shape;
public void drawShape(RectangularShape shape) {
this.shape = shape;
repaint();
}
public void paintComponent(Graphics g) {
if (shape != null) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.draw(shape);
}
}
}小弟代码在此。我想在一个JPanel里画一个圆,如果一开始按start的话圆会慢慢缩小,等缩到一个点的时候就开始放大,等圆碰到边界的时候又继续缩小,如果按stop的话会停止变化,然后按start后会继续之前的变化
刚开始几秒还好,但一会儿后圆的变化就开始不规则了,甚至差点死机,而我按stop也没有反应(不会停下来)。我看网上说,如果要圆规律地变化就要用到graphics2d的scale方法,但是该怎么用?我在paintComponent加了下g2d.scale(XX,XX),貌似连JPanel本身也跟着变化了,真心不会用啊……
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.EmptyBorder;public class Part1 extends JFrame { private JPanel contentPane, top;
private JButton start, stop;
private DrawingPane bottom;
private final int PANE_WIDTH = 400, PANE_HEIGHT = 400;
double x = 0;
double y = 0;
double width = PANE_WIDTH;
double height = PANE_HEIGHT;
Timer t;
boolean isShrinking = true; /**
* Launch the application.
*/
public static void main(String[] args) { Part1 frame = new Part1();
frame.setVisible(true); } /**
* Create the frame.
*/
public Part1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 430, 490);
top = new JPanel();
start = new JButton("Start");
stop = new JButton("Stop");
start.setEnabled(true);
stop.setEnabled(false); top.add(start);
top.add(stop); bottom = new DrawingPane();
bottom.setSize(PANE_WIDTH, PANE_HEIGHT);
bottom.setBorder(BorderFactory.createLineBorder(Color.BLACK)); contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout());
contentPane.add("North", top);
contentPane.add("Center", bottom);
setContentPane(contentPane);
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(t==null){
t = new Timer(500,this);
}
t.start();
if(isShrinking){shrink();}
else{grow();}
exampleDraw();
if(width < 1){
isShrinking = false;
}else if (width >399){
isShrinking = true;
}
start.setEnabled(false);
stop.setEnabled(true);
}
});
stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(t.isRunning()){
t.stop();
}
start.setEnabled(true);
stop.setEnabled(false);
}
});
exampleDraw();
} void exampleDraw() {
Ellipse2D.Double circle;
circle = new Ellipse2D.Double(x, y, width, height);
bottom.drawShape(circle);
}
void grow(){
x -= 10;
y -= 10;
width += 20;
height += 20;
}
void shrink(){
x += 10;
y += 10;
width -= 20;
height -= 20;
}
}DrawingPane不变