程序一直都不画图形 找不到什么原因 求大牛指点~//test.java
package jwindows;import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;import javax.swing.JPanel;public class Test extends JPanel { int x1, x2, y1, y2; public static Random a = new Random(); public void paintComponent(Graphics g) { super.paintComponent(g);
//g.fillArc(x, y, width, height, startAngle, arcAngle)
g.setColor(Color.CYAN);
g.drawLine(1, 1, 30, 30);
int i = 0;
while (i <= hi.num) { x1 = a.nextInt(this.getWidth()); y1 = a.nextInt(this.getHeight()); hi.xing[i].draw(g, x1, y1); i++;
} } int type;}//hi.java
package jwindows;import java.awt.Color;import javax.swing.JFrame;
import javax.swing.JOptionPane;public class hi { public static Shape[] xing = new Shape[10];
public static int num = 0; public static void main(String args[]) {
int i = 0;
while (i < 10) {
String hi = JOptionPane
.showInputDialog("Please type 1 for Circle\nPlease type 2 for Rectangle\nPlease type 0 to exit\n"); int type = Integer.parseInt(hi); if (type == 0)
break; switch (type) {
case 1:
xing[num] = new Circle(); break;
case 2:
xing[num] = new Rectangle(); break;
default: {
JOptionPane.showMessageDialog(null, "Error!"); num--;
break;
}
}
// xing.draw(g); num++;
}
JFrame app = new JFrame();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setTitle("DrawBoard");
Test panel = new Test();
panel.setBackground(Color.WHITE);
app.add(panel);
app.setSize(200, 400);
app.setVisible(true);
}}//Shape.java 图形继承的基类 子类有rectangle circlepublic abstract class Shape {
abstract public void init();
//abstract public double area();
//abstract public void display();
public abstract void draw(Graphics g,int dx,int dy);
//public abstract void ram();
Scanner input=new Scanner(System.in);
//
}求解~
package jwindows;import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;import javax.swing.JPanel;public class Test extends JPanel { int x1, x2, y1, y2; public static Random a = new Random(); public void paintComponent(Graphics g) { super.paintComponent(g);
//g.fillArc(x, y, width, height, startAngle, arcAngle)
g.setColor(Color.CYAN);
g.drawLine(1, 1, 30, 30);
int i = 0;
while (i <= hi.num) { x1 = a.nextInt(this.getWidth()); y1 = a.nextInt(this.getHeight()); hi.xing[i].draw(g, x1, y1); i++;
} } int type;}//hi.java
package jwindows;import java.awt.Color;import javax.swing.JFrame;
import javax.swing.JOptionPane;public class hi { public static Shape[] xing = new Shape[10];
public static int num = 0; public static void main(String args[]) {
int i = 0;
while (i < 10) {
String hi = JOptionPane
.showInputDialog("Please type 1 for Circle\nPlease type 2 for Rectangle\nPlease type 0 to exit\n"); int type = Integer.parseInt(hi); if (type == 0)
break; switch (type) {
case 1:
xing[num] = new Circle(); break;
case 2:
xing[num] = new Rectangle(); break;
default: {
JOptionPane.showMessageDialog(null, "Error!"); num--;
break;
}
}
// xing.draw(g); num++;
}
JFrame app = new JFrame();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setTitle("DrawBoard");
Test panel = new Test();
panel.setBackground(Color.WHITE);
app.add(panel);
app.setSize(200, 400);
app.setVisible(true);
}}//Shape.java 图形继承的基类 子类有rectangle circlepublic abstract class Shape {
abstract public void init();
//abstract public double area();
//abstract public void display();
public abstract void draw(Graphics g,int dx,int dy);
//public abstract void ram();
Scanner input=new Scanner(System.in);
//
}求解~
解决方案 »
- import的使用问题
- DatagramPacket的两个小疑问
- 在Ubuntu下安装jdk,配置路径失败,求助。。
- 如何直接对硬件编写自己的jar包?
- java字符串转换为时间的问题
- 能获得java.io.InputStreamReader.read()所获得的字符吗? 在线等
- 作入门,求最简单且完整的一个servlet例子
- “Error opening registry key 'Software\JavaSoft\Java Runtime Environment'”是什么错误?
- 各位大侠都用JAVA开发些什么类型的软件,C/S用她吗?
- 求救: 简单问题,请问如何在jb6中导入一个自定义的类?
- java方法的签名
- GUI求助,在线等高手回答
Circle类,Rectangle类没有
import java.awt.Graphics;import javax.swing.JOptionPane;public class Rectangle extends Shape {
int width, height;
int dx,dy;
//Scanner input = new Scanner(System.in);
public Rectangle()
{
init();
}
public void init() {
String hi = JOptionPane
.showInputDialog("长度");
width = Integer.parseInt(hi);
hi = JOptionPane
.showInputDialog("宽度");
height = Integer.parseInt(hi);
//System.out.println(":");
//width = input.nextInt();
//height = input.nextInt();
if (width <= 0 || height <= 0) {
width = 0;
height = 0;
}
}
public void draw(Graphics g,int dx,int dy)
{
g.setColor(Color.BLACK);
g.drawRect(dx, dy, dx+this.width, dy+this.height);
}
}package jwindows;import java.awt.Color;
import java.awt.Graphics;import javax.swing.JOptionPane;//import Shape.Shape;public class Circle extends Shape {
int radius; // Scanner input = new Scanner(System.in);
int dx, dy; public Circle() {
init();
} public void init() {
String hi = JOptionPane.showInputDialog("请输入半径:");
radius = Integer.parseInt(hi); if (radius <= 0)
radius = 0;
} public void draw(Graphics g,int dx,int dy) {
g.setColor(Color.BLACK);
g.drawOval(dx, dy, dx + this.radius, dy + this.radius);
} }
为什么我画出来的不是圆形啊
我吧dx+radius 和dy+radius变成 dx+2×radius和dy+2×radius了啊