java绘制矩形
为什么我画不出矩形?代码哪里有问题?求大神指点importjava.awt.*;importjava.awt.event.*;importjava.awt.geom.Re...
为什么我画不出矩形?代码哪里有问题?求大神指点
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Rectangle2D;
import javax.swing.*;
public class GameTest {
private Frame f;
private MyComponent mc;
GameTest(){
init();
}
private void init() {
// TODO Auto-generated method stub
f = new Frame("my Game");
mc = new MyComponent();
f.setBounds(10, 10, 800, 500);
f.setVisible(true);
f.add(mc);
myEvent();
}
private void myEvent() {
// TODO Auto-generated method stub
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new GameTest();
}
}
class MyComponent extends JComponent{
public void paintComponrnt(Graphics g){
Graphics2D g2 = (Graphics2D) g;
double x1 = 100;
double y1 = 100;
double w1 = 200;
double h1 = 150;
Rectangle2D rect = new Rectangle2D.Double(x1,y1,w1,h1);
g2.setColor(Color.red);
g2.draw(rect);
}
} 展开
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Rectangle2D;
import javax.swing.*;
public class GameTest {
private Frame f;
private MyComponent mc;
GameTest(){
init();
}
private void init() {
// TODO Auto-generated method stub
f = new Frame("my Game");
mc = new MyComponent();
f.setBounds(10, 10, 800, 500);
f.setVisible(true);
f.add(mc);
myEvent();
}
private void myEvent() {
// TODO Auto-generated method stub
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new GameTest();
}
}
class MyComponent extends JComponent{
public void paintComponrnt(Graphics g){
Graphics2D g2 = (Graphics2D) g;
double x1 = 100;
double y1 = 100;
double w1 = 200;
double h1 = 150;
Rectangle2D rect = new Rectangle2D.Double(x1,y1,w1,h1);
g2.setColor(Color.red);
g2.draw(rect);
}
} 展开
2014-12-20
展开全部
里面有矩形,自己参考下:
package ziliao;
//鼠标画图1
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//鼠标事件处理
//事件接口: MouseListener, MouseMotionListener
//使用鼠标画图
//使用JPanel面板绘图
class Co13 extends JFrame
{
JPanel jp;
JRadioButton jrb1, jrb2, jrb3, jrb4;
Co13()
{
jrb1 = new JRadioButton("直线",true);
jrb2 = new JRadioButton("矩形");
jrb3 = new JRadioButton("椭圆");
jrb4 = new JRadioButton("圆");
ButtonGroup bg = new ButtonGroup(); //按钮组
bg.add(jrb1); //加入按钮组
bg.add(jrb2);
bg.add(jrb3);
bg.add(jrb4);
jp = new JPanel();
jp.add(jrb1);
jp.add(jrb2);
jp.add(jrb3);
jp.add(jrb4);
add(jp,BorderLayout.NORTH);
add(new MyPanel(this));
setBounds(120,125,300,200);
setTitle("使用鼠标绘图");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public int getJrb() //判断判断并返回选择的是哪个单选按钮
{
int jrb;
if(jrb1.isSelected())
jrb = 1;
else if(jrb2.isSelected())
jrb = 2;
else if(jrb3.isSelected())
jrb = 3;
else
jrb = 4;
return jrb;
}
}
class MyPanel extends JPanel implements MouseListener, MouseMotionListener
{ //将JPanel面板作为画布
int x1, y1, x2, y2;
Co13 jf; //窗体对象,用于传递窗体中选择的按钮
MyPanel(Co13 jf)
{
this.jf = jf;
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
//覆盖 paintComponent 方法
public void paintComponent(Graphics g)
{
super.paintComponent(g); //调用超类JPanel构造方法
String s = null;
g.setColor(Color.red);
switch (jf.getJrb()) //判断选择什么图形
{
case 1: //画直线
g.drawLine(x1, y1, x2, y2); //起始点和终点,两点固定一条直线
s = "直线绘制";
break;
case 2: //画矩形
g.drawRect(x1, y1, x2 - x1, y2 - y1); //以起始点为原点,移动距离为长宽
s = "矩形绘制";
break;
case 3: //画椭圆
g.drawOval(x1, y1, x2 - x1, y2 - y1); //以起始点为圆心,移动距离为半径
s = "椭圆绘制";
break;
case 4: //画圆
int dx = Math.abs(x2 - x1);
int dy = Math.abs(y2 - y1);
int r = (int)(Math.sqrt(dx * dx + dy * dy));
g.drawOval(x1 - r, y1 - r, r + r, r + r);
g.setColor(Color.gray);
g.drawLine(x1, y1, x2, y2);
s = "正圆绘制";
break;
}
g.setColor(Color.blue);
g.drawString(s, 10, 10);
}
public void mousePressed(MouseEvent e)
{
x1 = e.getX(); //获取鼠标按下时的坐标
y1 = e.getY();
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseDragged(MouseEvent e)
{
x2 = e.getX(); //获取鼠标拖动时的坐标
y2 = e.getY();
repaint(); //重绘图形
}
public void mouseMoved(MouseEvent e)
{
}
}
public class E6_13
{ public static void main(String args[])
{ new Co13();
}
}
提交回答
package ziliao;
//鼠标画图1
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//鼠标事件处理
//事件接口: MouseListener, MouseMotionListener
//使用鼠标画图
//使用JPanel面板绘图
class Co13 extends JFrame
{
JPanel jp;
JRadioButton jrb1, jrb2, jrb3, jrb4;
Co13()
{
jrb1 = new JRadioButton("直线",true);
jrb2 = new JRadioButton("矩形");
jrb3 = new JRadioButton("椭圆");
jrb4 = new JRadioButton("圆");
ButtonGroup bg = new ButtonGroup(); //按钮组
bg.add(jrb1); //加入按钮组
bg.add(jrb2);
bg.add(jrb3);
bg.add(jrb4);
jp = new JPanel();
jp.add(jrb1);
jp.add(jrb2);
jp.add(jrb3);
jp.add(jrb4);
add(jp,BorderLayout.NORTH);
add(new MyPanel(this));
setBounds(120,125,300,200);
setTitle("使用鼠标绘图");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public int getJrb() //判断判断并返回选择的是哪个单选按钮
{
int jrb;
if(jrb1.isSelected())
jrb = 1;
else if(jrb2.isSelected())
jrb = 2;
else if(jrb3.isSelected())
jrb = 3;
else
jrb = 4;
return jrb;
}
}
class MyPanel extends JPanel implements MouseListener, MouseMotionListener
{ //将JPanel面板作为画布
int x1, y1, x2, y2;
Co13 jf; //窗体对象,用于传递窗体中选择的按钮
MyPanel(Co13 jf)
{
this.jf = jf;
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
//覆盖 paintComponent 方法
public void paintComponent(Graphics g)
{
super.paintComponent(g); //调用超类JPanel构造方法
String s = null;
g.setColor(Color.red);
switch (jf.getJrb()) //判断选择什么图形
{
case 1: //画直线
g.drawLine(x1, y1, x2, y2); //起始点和终点,两点固定一条直线
s = "直线绘制";
break;
case 2: //画矩形
g.drawRect(x1, y1, x2 - x1, y2 - y1); //以起始点为原点,移动距离为长宽
s = "矩形绘制";
break;
case 3: //画椭圆
g.drawOval(x1, y1, x2 - x1, y2 - y1); //以起始点为圆心,移动距离为半径
s = "椭圆绘制";
break;
case 4: //画圆
int dx = Math.abs(x2 - x1);
int dy = Math.abs(y2 - y1);
int r = (int)(Math.sqrt(dx * dx + dy * dy));
g.drawOval(x1 - r, y1 - r, r + r, r + r);
g.setColor(Color.gray);
g.drawLine(x1, y1, x2, y2);
s = "正圆绘制";
break;
}
g.setColor(Color.blue);
g.drawString(s, 10, 10);
}
public void mousePressed(MouseEvent e)
{
x1 = e.getX(); //获取鼠标按下时的坐标
y1 = e.getY();
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseDragged(MouseEvent e)
{
x2 = e.getX(); //获取鼠标拖动时的坐标
y2 = e.getY();
repaint(); //重绘图形
}
public void mouseMoved(MouseEvent e)
{
}
}
public class E6_13
{ public static void main(String args[])
{ new Co13();
}
}
提交回答
展开全部
问题出在mc = new MyComponent();这一句上,
你实例化了一个myComponent对象,但是MyComponent类调用的是默认的无参的构造函数
public MyComponent(){
}
这个方法里并没有调用画矩形的方法paintComponrnt()
解决办法:1.在类MyComponent的构造函数中调用画矩形的方法
2.通过实例对象调用画矩形的方法,在mc = new MyComponent();这一句后面加上 mc.paintComponrnt(参数);
你实例化了一个myComponent对象,但是MyComponent类调用的是默认的无参的构造函数
public MyComponent(){
}
这个方法里并没有调用画矩形的方法paintComponrnt()
解决办法:1.在类MyComponent的构造函数中调用画矩形的方法
2.通过实例对象调用画矩形的方法,在mc = new MyComponent();这一句后面加上 mc.paintComponrnt(参数);
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
直接调用Graphics
的 drawRect方法就直接显示了。
的 drawRect方法就直接显示了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询