java编程:怎么画一个正方形?
不用applet,只是application编程,用java怎么画一个蓝色的正方形(最好实心和空心的各画一个).-----------------------------...
不用applet,只是application编程,用java怎么画一个蓝色的正方形(最好实心和空心的各画一个).
-------------------------------------------------------------------
希望给出完整代码,非常感谢!
--------------------------------------------------------------------
如果代码过长可发到我的邮箱: lzninchina@sina.com 展开
-------------------------------------------------------------------
希望给出完整代码,非常感谢!
--------------------------------------------------------------------
如果代码过长可发到我的邮箱: lzninchina@sina.com 展开
展开全部
import java.awt.*;
import javax.swing.*;
public class DrawRectTest extends JFrame
{
//定义方法
public DrawRectTest()
{
//实例化对象
GPanel g=new GPanel();
this.getContentPane().add(g);
this.setSize(500,500);
this.setVisible(true);
}
//主函数
public static void main(String[] args)
{
new DrawRectTest();
}
}
//声明一个GPanel
class GPanel extends JPanel
{
//Graphics这个是抽象类,它的对象是用来传给paint()方法作为画笔的
public void paintComponent(Graphics g)
{
int w;//横坐标
int h;//纵坐标
for(int i=1;i<=10;i++)
{
h=30;
w=10;
for(int j=1;j<=10;j++)
g.drawRect(w=w+20, h, 20, 20);
}
}
}
import javax.swing.*;
public class DrawRectTest extends JFrame
{
//定义方法
public DrawRectTest()
{
//实例化对象
GPanel g=new GPanel();
this.getContentPane().add(g);
this.setSize(500,500);
this.setVisible(true);
}
//主函数
public static void main(String[] args)
{
new DrawRectTest();
}
}
//声明一个GPanel
class GPanel extends JPanel
{
//Graphics这个是抽象类,它的对象是用来传给paint()方法作为画笔的
public void paintComponent(Graphics g)
{
int w;//横坐标
int h;//纵坐标
for(int i=1;i<=10;i++)
{
h=30;
w=10;
for(int j=1;j<=10;j++)
g.drawRect(w=w+20, h, 20, 20);
}
}
}
展开全部
没多少代码。如下。
---------------------------------------------------------------
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
public class Foo extends JFrame implements ActionListener {
JPanel panel = null;
public Foo() {
panel = new JPanel();
panel.setLayout(null);
getContentPane().add(panel, BorderLayout.CENTER);
JButton btnNewButton = new JButton("Click");
btnNewButton.setBounds(22, 10, 93, 23);
btnNewButton.addActionListener(this);
panel.add(btnNewButton);
setSize(200, 200);
setVisible(true);
}
public static void main(String[] args) {
new Foo();
}
public void actionPerformed(ActionEvent e) {
Graphics g = panel.getGraphics();
g.drawRect(10, 50, 50, 50);
g.fillRect(110, 50, 50, 50);
}
}
---------------------------------------------------------------
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
public class Foo extends JFrame implements ActionListener {
JPanel panel = null;
public Foo() {
panel = new JPanel();
panel.setLayout(null);
getContentPane().add(panel, BorderLayout.CENTER);
JButton btnNewButton = new JButton("Click");
btnNewButton.setBounds(22, 10, 93, 23);
btnNewButton.addActionListener(this);
panel.add(btnNewButton);
setSize(200, 200);
setVisible(true);
}
public static void main(String[] args) {
new Foo();
}
public void actionPerformed(ActionEvent e) {
Graphics g = panel.getGraphics();
g.drawRect(10, 50, 50, 50);
g.fillRect(110, 50, 50, 50);
}
}
更多追问追答
追问
如果不添加事件的话,是不是只要初始化一个JFrame和一个JPanel然后,用以下三句即可?
Graphics g = panel.getGraphics();
g.drawRect(10, 50, 50, 50);
g.fillRect(110, 50, 50, 50);
----------------------------------------------------------------------
但,它什么都没画出来啊?
追答
那是画面还没有加载画笔,
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.awt.*;
import javax.swing.*;
public class DrawReac extends JFrame
{
public DrawReac()
{
this.setTitle("画正方形");
this.setSize(600, 500);
this.setLocation(360, 100);
this.setVisible(true);
}
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.red);
//画填充颜色的正方形
g.fillRect(50, 50, 100, 100);
//画不带颜色的正方形
g.drawRect(200, 200, 100, 100);
}
public static void main(String[] args)
{
DrawReac draw = new DrawReac();
}
}
import javax.swing.*;
public class DrawReac extends JFrame
{
public DrawReac()
{
this.setTitle("画正方形");
this.setSize(600, 500);
this.setLocation(360, 100);
this.setVisible(true);
}
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.red);
//画填充颜色的正方形
g.fillRect(50, 50, 100, 100);
//画不带颜色的正方形
g.drawRect(200, 200, 100, 100);
}
public static void main(String[] args)
{
DrawReac draw = new DrawReac();
}
}
追问
如果不用继承JFrame,重写其paint的方法,只是想在JPanel上画图的话,该怎么做呢?
追答
JPanel里也有paint方法 只要重写就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询