如何在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().该怎么画这个矩形,请教!
...全文
299 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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);
}
}

62,634

社区成员

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

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