如何在JPanel绘制图形(如矩形).

yaya214 2003-05-04 11:09:46
如何在JPanel绘制图形(如矩形).比如:源文件为ex1.java,其中定义了一个public类ex1类(public class myztt extends JFrame),构造函数(public myztt())中定义了desktop为JPanel,设置为空布局.在desktop上添加了pnl1、pnl2、pnl3等JPanel的面板。想在其中的一个面板上绘制矩形,该怎么办?另外,在ex1.java中的main()方法中,ex1 frame=new ex1().该怎么画这个矩形,请教!
...全文
277 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
rocandroc 2003-05-05
  • 打赏
  • 举报
回复
import java.awt.*;
import java.awt.event.*;

// Java extension packages
import javax.swing.*;

public class DrawShapes extends JApplet {
private JButton choices[];
private String names[] = { "Line", "Rectangle", "Oval" };
private JPanel buttonPanel;
private DrawPanel drawingPanel;
private int width = 300, height = 200;

// initialize applet; set up GUI
public void init()
{
// set up DrawPanel
drawingPanel = new DrawPanel( width, height );

// create array of buttons
choices = new JButton[ names.length ];

// set up panel for buttons
buttonPanel = new JPanel();
buttonPanel.setLayout(
new GridLayout( 1, choices.length ) );

// set up buttons and register their listeners
ButtonHandler handler = new ButtonHandler();

for ( int count = 0; count < choices.length; count++ ) {
choices[ count ] = new JButton( names[ count ] );
buttonPanel.add( choices[ count ] );
choices[ count ].addActionListener( handler );
}

// attach components to content pane
Container container = getContentPane();
container.add( buttonPanel, BorderLayout.NORTH );
container.add( drawingPanel, BorderLayout.CENTER );
}

// enables application to specify width of drawing area
public void setWidth( int newWidth )
{
width = ( newWidth >= 0 ? newWidth : 300 );
}

// enables application to specify height of drawing area
public void setHeight( int newHeight )
{
height = ( newHeight >= 0 ? newHeight : 200 );
}

// execute applet as an application
public static void main( String args[] )
{
int width, height;

// check for command-line arguments
if ( args.length != 2 ) {
width = 300;
height = 200;
}
else {
width = Integer.parseInt( args[ 0 ] );
height = Integer.parseInt( args[ 1 ] );
}

// create window in which applet will execute
JFrame applicationWindow =
new JFrame( "An applet running as an application" );

applicationWindow.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );

// create one applet instance
DrawShapes appletObject = new DrawShapes();
appletObject.setWidth( width );
appletObject.setHeight( height );

// call applet's init and start methods
appletObject.init();
appletObject.start();

// attach applet to center of window
applicationWindow.getContentPane().add( appletObject );

// set the window's size
applicationWindow.setSize( width, height );

// showing the window causes all GUI components
// attached to the window to be painted
applicationWindow.setVisible( true );
}

// private inner class to handle button events
private class ButtonHandler implements ActionListener {

// determine button user pressed and set drawing area's
// current choice
public void actionPerformed( ActionEvent event )
{
for ( int count = 0; count < choices.length; count++ )

if ( event.getSource() == choices[ count ] ) {
drawingPanel.setCurrentChoice( count );
break;
}
}

} // end private inner class ButtonHandler

} // end class DrawShapes

// subclass of JPanel to allow drawing in a separate area
class DrawPanel extends JPanel {
private int currentChoice = -1; // don't draw first time
private int width = 100, height = 100;

// initialize width and height of DrawPanel
public DrawPanel( int newWidth, int newHeight )
{
width = ( newWidth >= 0 ? newWidth : 100 );
height = ( newHeight >= 0 ? newHeight : 100 );
}

// draw line, rectangle or oval based on user's choice
public void paintComponent( Graphics g )
{
super.paintComponent( g );

switch( currentChoice ) {

case 0:
g.drawLine( randomX(), randomY(),
randomX(), randomY() );
break;

case 1:
g.drawRect( randomX(), randomY(),
randomX(), randomY() );
break;

case 2:
g.drawOval( randomX(), randomY(),
randomX(), randomY() );
break;
}

} // end method paintComponent

// specify current shape choice and repaint
public void setCurrentChoice( int choice )
{
currentChoice = choice;
repaint();
}

// pick random x coordinate
private int randomX()
{
return ( int ) ( Math.random() * width );
}

// pick random y coordinate
private int randomY()
{
return ( int ) ( Math.random() * height );
}

} // end class DrawPanel
ruguo2003 2003-05-04
  • 打赏
  • 举报
回复
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Myztt extends JFrame{
public Myztt(){
super("Map");
setSize(350,350);
ExitWindow exit=new ExitWindow();
MapPane pn11=new MapPane();
getContentPane().add(pn11);
addWindowListener(exit);
}
public static void main(String[] args){
Myztt frame=new Myztt();
frame.show();
}
}

class MapPane extends JPanel{
public void paintComponent(Graphics comp){
Graphics2D comp2D=(Graphics2D)comp;
comp2D.drawRect(50,80,100,100);
}
}
class ExitWindow extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
Java拼图游戏面向对象课程设计报告 ———————————————————————————————— 作者: ———————————————————————————————— 日期: "姓 名 " "学 号 "20137045 "班 级 " " "Name " "Student No." "Class " " "代码总行数"222 "项目名称 " " "Code Lines" "Item " " "1.实验目的 " "本次课程设计旨在通过一个完整项目的开发,巩固面向对象程序设计、软件工程、 " "数据库技术等课程的相关知识,加深学生对Java语言的理解,尤其是对面向对象思" "想、Java编码规范、JDBC访问数据库的理解,使学生进一步掌握环境构建、代码编 " "写、文档阅读与理解、文档编写、软件测试、发行与部署等技能,进一步提高学生 " "的学习能力、理解能力、表达能力及沟通能力. " "2.实验内容 " "本次课程设计选定的实验项目是使用JAVA语言编写一个拼图游戏并实现其基本功能" "。不用自己手动切割图片,再进行加载.而是应用类方法在内存中进行图片切割和 " "加载,方便了用户的使用。 " "利用了Graphics中的public abstract boolean drawImage()方法把 img 中由 " "(sx1, sy1)(sx2, sy2)指定的矩形区域画到 observer 中由(dx1, dy1)(dx2, " "dy2)指定的矩形区域内进行构思拼图游戏的具体实现。 " "导入了一个可播放背景音乐的jar包,在玩游戏时可以欣赏背景音乐。 " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " "3.类图 " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " "4.运行界面 " " " "图1、游戏初始界面 " " " "图2、游戏运行界面 " " " " " " " "图3、拼图成功界面 " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " "5.关键代码(要求在类的定义以及方法定义的前面给出相关描述信息) " "//定义一个Pintu类 " "public class Jpgsaw extends JFrame{ " "private Image image, buff; //原始图片,缓存图片 " "private int[][] map = {{0, 1, 2}, " "{3, 4, 5}, " "{6, 7, 8} " "}; //定义数组map,给分割后的图片标号 " "private Point point = new Point(2, 2); //记录第九块图片位置 " "private int sx; //分割后每一个图片的宽度 " "private int sy; //分割后每一个图片的高度 " "private Canvas canvas; " "//加载图片容器,定义可以显式定位子元素使用坐标是相对 Canvas " "区域的区域,绘制图形 " "private Graphics gs; //gs画出Canvas图片坐标 " "private Graphics gb; //gb画出buff图像 " "private boolean isRunning = false; //游戏是否正在进行 " "private JButton start = new JButton("开始新的游戏"); // 按钮1 " "private JButton show = new JButton("显示正确图片"); //按钮2 " "private JTextArea showStep = new JTextArea("显示步骤");//设置步骤显示 " "private JPanel panel = new JPanel(); " "//一般轻量级面板容器,装在上面2个按钮 " "private int steps = 0; // 记录移动的步骤 " "public Jpgsaw(String title) { //构造方法,初始化变量 " "super(title);//调用了当前类Jpgsaw的父类JFrame的构造方法 " "try { //异常抛出 " "image = ImageIO.read(new File("gg。jpg")); /

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧