我对Java还是很陌生,我试图创建一个程序,在该程序中计算机绘制随机的圆形,矩形和直线,然后计算显示在其下方的数字。
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;
public class Shapes extends JPanel{
` private int noOfCircles;
private int noOfLines;
private int noOfRect;
public void paintComponent(Graphics g){
Random rand = new Random();
super.paintComponent( g );
int x = 10;
int y = 10;
noOfCircles = 0;
noOfLines = 0;
noOfRect = 0;
for(int i = 0; i<10; i++){
int choice = rand.nextInt(3);
if(choice == 0){
noOfLines++;
g.drawLine(x, y, i*10+50, i+100);
}
else if(choice == 1){
noOfRect++;
g.drawRect(i*20, y*20, x*10, y *10);
}
else{
noOfCircles++;
g.drawOval(x*10, i*20, x*10, y*10);
}
x+=5;
y+=5;
}
System.out.println(status());
}
public String status(){
String message = String.format("Circles : %d; Rect : %d; Lines : %d;", noOfCircles, noOfRect, noOfLines);
return message;
}
}
和
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JFrame;
public class ShapesTest {
public static void main( String args[] ){
Shapes panel = new Shapes();
JFrame app = new JFrame();
JLabel statusBar = new JLabel();
String message = panel.status();
statusBar.setText(message);
statusBar.setSize(400, 20);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.add(panel);
app.add(statusBar, BorderLayout.SOUTH);
app.setSize( 400, 400 );
app.setVisible(true);
}
}
问题在于,Shape类在被调用时总是返回noOfCircles,noOfLines,noOfRect = 0。我不知道它是如何工作的
最佳答案
这有效,并且不会在调整大小时更新。
视图和逻辑也分开。
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JFrame;
public class ShapesTest {
public static void main( String args[] ){
Shapes panel = new Shapes();
JFrame app = new JFrame();
JLabel statusBar = new JLabel();
statusBar.setSize(400, 20);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.add(panel);
app.add(statusBar, BorderLayout.SOUTH);
app.setSize( 400, 400 );
app.setVisible(true);
statusBar.setText(panel.status());
}
}
import java.awt.Graphics;
import javax.swing.JPanel;
import java.util.Random;
public class Shapes extends JPanel{
private static final int TYPE_LINE = 0;
private static final int TYPE_RECT = 1;
// private static final int TYPE_OVAL = 2; // not used
private int[] forms = new int[10];
private int c = 0, r = 0, l = 0;
public Shapes() {
super();
Random rand = new Random();
for(int i = 0; i < forms.length; i++) {
int choice = rand.nextInt(3);
if(choice == TYPE_LINE)
c++;
else if(choice == TYPE_RECT)
r++;
else
l++;
forms[i] = choice;
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
int x = 10;
int y = 10;
for(int i = 0; i < forms.length; i++) {
int choice = forms[i];
if(choice == TYPE_LINE){
g.drawLine(x, y, i*10+50, i+100);
}
else if(choice == TYPE_RECT){
g.drawRect(i*20, y*20, x*10, y *10);
}
else {
g.drawOval(x*10, i*20, x*10, y*10);
}
x+=5;
y+=5;
}
}
public String status(){
String message = String.format("C:%d;R:%d;L:%d", c, r, l);
return message;
}
}