| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1241 人关注过本帖
标题:如何在这个类中实现这个方法
只看楼主 加入收藏
Dreamawaker
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2006-10-12
收藏
 问题点数:0 回复次数:18 
如何在这个类中实现这个方法

目前程序如下,请问,如何在Ex14中实现DrawPanel中的画图功能?


package Ex14;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.color.*;

public class DrawPanel extends JPanel
{
private MyColorChooser mcc;
public Color mycolor;

public DrawPanel()
{
mcc = new MyColorChooser();
}

public void set()
{
mycolor = mcc.mycolorchooser;
repaint();
}

public void paintComponent( Graphics g )
{
super.paint( g );
g.setColor( mycolor );
g.fillRect( 200, 100, 200, 100 );

}
}


package Ex14;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Ex14 extends JApplet
{
private MyColorChooser mcc;
private DrawPanel dp;
private JButton button;
private FlowLayout layout;

public Ex14()
{
Container container = getContentPane();
layout = new FlowLayout();
container.setLayout( layout );

mcc = new MyColorChooser();
dp = new DrawPanel();
container.add( mcc );

dp.repaint();

button = new JButton("OK");
button.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent actionEvent )
{
dp.repaint();
}
}

);
container.add( button );

setSize( 800, 600 );
setVisible( true );
}

}

搜索更多相关主题的帖子: mcc void private package public 
2006-11-29 13:44
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
你这不是已经实现了吗?
你想实现什么效果?

可惜不是你,陪我到最后
2006-11-29 14:55
Dreamawaker
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2006-10-12
收藏
得分:0 

还有一个类,MyColorChooser。
要实现用set方法来设置当前颜色的红、绿、蓝值。并在调用DrawPanel的set方法时要自动重绘自身。
现在好像连图像都画不出来………………
package Ex14;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.color.*;

public class MyColorChooser extends JPanel
{
private JSlider RSlider, GSlider, BSlider;
private JTextField Rtext, Gtext, Btext;
private JLabel RLabel, GLabel, BLabel;
private int RED=0, GREEN=0, BLUE=0;
public Color mycolorchooser;

public MyColorChooser( )
{
RSlider = new JSlider( SwingConstants.HORIZONTAL, 0, 255, 0 );
RSlider.setMajorTickSpacing( 10 );
RSlider.setPaintTicks( false );
RSlider.addChangeListener
(
new ChangeListener()
{
public void stateChanged( ChangeEvent e )
{
RED = RSlider.getValue();
Rtext.setText( Integer.toString( RED ) );
repaint();
}
}
);

GSlider = new JSlider( SwingConstants.HORIZONTAL, 0, 255, 0 );
GSlider.setMajorTickSpacing( 10 );
GSlider.setPaintTicks( false );
GSlider.addChangeListener
(
new ChangeListener()
{
public void stateChanged( ChangeEvent e )
{
GREEN = GSlider.getValue();
Gtext.setText( Integer.toString( GREEN ) );
repaint();
}
}
);

BSlider = new JSlider( SwingConstants.HORIZONTAL, 0, 255, 0 );
BSlider.setMajorTickSpacing( 10 );
BSlider.setPaintTicks( false );
BSlider.addChangeListener
(
new ChangeListener()
{
public void stateChanged( ChangeEvent e )
{
BLUE = BSlider.getValue();
Btext.setText( Integer.toString( BLUE ) );
repaint();
}
}
);

Rtext = new JTextField( 5 );
Rtext.setText( Integer.toString( RED ) );
Rtext.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent actionEvent )
{
RSlider.setValue( Integer.valueOf( Rtext.getText()) );
repaint();
}
}

);

Gtext = new JTextField( 5 );
Gtext.setText( Integer.toString( BLUE ) );
Gtext.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent actionEvent )
{
GSlider.setValue( Integer.valueOf( Gtext.getText()) );
repaint();
}
}

);

Btext = new JTextField( 5 );
Btext.setText( Integer.toString( GREEN ) );
Btext.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent actionEvent )
{
BSlider.setValue( Integer.valueOf( Btext.getText()) );
repaint();
}
}

);


RLabel = new JLabel( "RED:" );
GLabel = new JLabel( "GREEN:" );
BLabel = new JLabel( "BLUE:" );

add( RLabel );
add( Rtext );
add( RSlider );

add( GLabel );
add( Gtext );
add( GSlider );

add( BLabel );
add( Btext );
add( BSlider );
}

public void paintComponent( Graphics g )
{
super.paintComponent( g );
mycolorchooser = new Color( RED, GREEN, BLUE );
}

public Dimension getPreferredSize()
{
return new Dimension( 200, 180 );
}
}


2006-11-29 20:17
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 

你这个写实现不了的

我帮你改了一个MyColorChooser类
package Ex14;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.color.*;

public class MyColorChooser extends JPanel
{
private JSlider RSlider, GSlider, BSlider;
private JTextField Rtext, Gtext, Btext;
private JLabel RLabel, GLabel, BLabel;
private int RED=0, GREEN=0, BLUE=0;
public Color mycolorchooser;

public MyColorChooser( )
{
RSlider = new JSlider( SwingConstants.HORIZONTAL, 0, 255, 0 );
RSlider.setMajorTickSpacing( 10 );
RSlider.setPaintTicks( false );
RSlider.addChangeListener
(
new ChangeListener()
{
public void stateChanged( ChangeEvent e )
{
RED = RSlider.getValue();
Rtext.setText( Integer.toString( RED ) );
repaint();
}
}
);

GSlider = new JSlider( SwingConstants.HORIZONTAL, 0, 255, 0 );
GSlider.setMajorTickSpacing( 10 );
GSlider.setPaintTicks( false );
GSlider.addChangeListener
(
new ChangeListener()
{
public void stateChanged( ChangeEvent e )
{
GREEN = GSlider.getValue();
Gtext.setText( Integer.toString( GREEN ) );
repaint();
}
}
);

BSlider = new JSlider( SwingConstants.HORIZONTAL, 0, 255, 0 );
BSlider.setMajorTickSpacing( 10 );
BSlider.setPaintTicks( false );
BSlider.addChangeListener
(
new ChangeListener()
{
public void stateChanged( ChangeEvent e )
{
BLUE = BSlider.getValue();
Btext.setText( Integer.toString( BLUE ) );
repaint();
}
}
);

Rtext = new JTextField( 5 );
Rtext.setText( Integer.toString( RED ) );
Rtext.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent actionEvent )
{
RSlider.setValue( Integer.valueOf( Rtext.getText()) );
repaint();
}
}

);

Gtext = new JTextField( 5 );
Gtext.setText( Integer.toString( BLUE ) );
Gtext.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent actionEvent )
{
GSlider.setValue( Integer.valueOf( Gtext.getText()) );
repaint();
}
}

);

Btext = new JTextField( 5 );
Btext.setText( Integer.toString( GREEN ) );
Btext.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent actionEvent )
{
BSlider.setValue( Integer.valueOf( Btext.getText()) );
repaint();
}
}

);


RLabel = new JLabel( "RED:" );
GLabel = new JLabel( "GREEN:" );
BLabel = new JLabel( "BLUE:" );

add( RLabel );
add( Rtext );
add( RSlider );

add( GLabel );
add( Gtext );
add( GSlider );

add( BLabel );
add( Btext );
add( BSlider );
}

public void paintComponent( Graphics g )
{
super.paintComponent( g );
mycolorchooser = new Color( RED, GREEN, BLUE );
g.setColor(mycolorchooser);
g.fillRect(0,0,this.getWidth(),this.getHeight());
}

public Dimension getPreferredSize()
{
return new Dimension( 200, 180 );
}
}


可惜不是你,陪我到最后
2006-11-29 20:32
Dreamawaker
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2006-10-12
收藏
得分:0 

可是这样的话救没有调用set方法了吧?


2006-11-29 20:37
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
收藏
得分:0 
嘿嘿..学习了..

我的msn: myfend@
2006-11-29 20:38
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 

你知道你错在哪里吗?
1,是你两个类里面有两个MyColorChooser,所以互相调用不到
2,你在paintComponent里面调用super.paint方法,这是绝对错误的,这样会引起互相永远调用,最后栈溢出
3,你根本没有把dp加进来


可惜不是你,陪我到最后
2006-11-29 20:38
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 

我还是把我修改后的程序发给你看吧


可惜不是你,陪我到最后
2006-11-29 20:39
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 

package Ex14;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Ex14 extends JApplet
{
private MyColorChooser mcc;
private DrawPanel dp;
private JButton button;
private FlowLayout layout;

public Ex14()
{
Container container = getContentPane();
layout = new FlowLayout();
container.setLayout( layout );

mcc = new MyColorChooser();
dp = new DrawPanel(mcc);
dp.setPreferredSize(new Dimension(100,100));
container.add( mcc );
container.add(dp);

dp.repaint();

button = new JButton("OK");
button.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent actionEvent )
{
dp.set();
dp.repaint();
}
}

);
container.add( button );

setSize(800,600);
setVisible( true );
}

}


可惜不是你,陪我到最后
2006-11-29 20:42
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 

package Ex14;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.color.*;

public class DrawPanel extends JPanel
{
private MyColorChooser mcc;
public Color mycolor;

public DrawPanel(MyColorChooser mcc)
{
this.mcc = mcc;
}

public void set()
{
mycolor = mcc.mycolorchooser;
repaint();
}

public void paintComponent( Graphics g )
{
super.paintComponent( g );
g.setColor( mycolor );
g.fillRect( 0, 0, 100, 100 );

}
}


可惜不是你,陪我到最后
2006-11-29 20:42
快速回复:如何在这个类中实现这个方法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.042553 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved