Java绘图问题,图像出不来
/***用上下左右控制小点移动,圆画不出来,哪里有问题?*现在最基本的一个点也出不来*/importjavax.swing.*;importjava.awt.*;impo...
/**
* 用上下左右控制小点移动,圆画不出来,哪里有问题?
* 现在最基本的一个点也出不来
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.ActionListener;
public class ActionHeard1 extends JFrame{
MyPanel mp=null;
public static void main(String[] args)
{
ActionHeard1 ah=new ActionHeard1();
}
public ActionHeard1()
{
mp=new MyPanel();
this.add(mp);
//this.addKeyListener(mp);
this.setSize(500,400);
this.setLocation(500, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class MyPanel extends JPanel //implements KeyListener
{
int x=10;
int y=10;
public void draw(Graphics g)
{
super.paint(g);
//g.setColor(Color.red);
g.fillOval(x, y, 15, 15);
}
/* @Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
y+=10;
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
x-=10;
}
if(e.getKeyCode()==KeyEvent.VK_UP)
{
y-=10;
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
x+=10;
}
this.repaint();
}
@Override
public void keyReleased(KeyEvent e)
{
}
@Override
public void keyTyped(KeyEvent e)
{
}*/
} 展开
* 用上下左右控制小点移动,圆画不出来,哪里有问题?
* 现在最基本的一个点也出不来
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.ActionListener;
public class ActionHeard1 extends JFrame{
MyPanel mp=null;
public static void main(String[] args)
{
ActionHeard1 ah=new ActionHeard1();
}
public ActionHeard1()
{
mp=new MyPanel();
this.add(mp);
//this.addKeyListener(mp);
this.setSize(500,400);
this.setLocation(500, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class MyPanel extends JPanel //implements KeyListener
{
int x=10;
int y=10;
public void draw(Graphics g)
{
super.paint(g);
//g.setColor(Color.red);
g.fillOval(x, y, 15, 15);
}
/* @Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
y+=10;
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
x-=10;
}
if(e.getKeyCode()==KeyEvent.VK_UP)
{
y-=10;
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
x+=10;
}
this.repaint();
}
@Override
public void keyReleased(KeyEvent e)
{
}
@Override
public void keyTyped(KeyEvent e)
{
}*/
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询