Java 我如何在秋千上画一幅JPanel
我正在swing中编写一个程序,它使用链接节点存储一个值,然后将包含的值以黄色条的形式绘制到JPanel中。这幅画最初是用AWT完成的。我读了一些关于swing中绘画的教程,但我仍然不知道如何在JPanel中绘画。程序应该在按下“随机”按钮时绘制条形图,但目前没有绘制任何内容 我已经包含了我认为相关的代码,但是如果您需要任何其他部分,请告诉我。提前感谢你能给我的任何帮助,或者你能指给我的任何教程Java 我如何在秋千上画一幅JPanel,java,swing,jpanel,Java,Swing,Jpanel,我正在swing中编写一个程序,它使用链接节点存储一个值,然后将包含的值以黄色条的形式绘制到JPanel中。这幅画最初是用AWT完成的。我读了一些关于swing中绘画的教程,但我仍然不知道如何在JPanel中绘画。程序应该在按下“随机”按钮时绘制条形图,但目前没有绘制任何内容 我已经包含了我认为相关的代码,但是如果您需要任何其他部分,请告诉我。提前感谢你能给我的任何帮助,或者你能指给我的任何教程 public class DataOrganizer extends JPanel {
public class DataOrganizer extends JPanel
{
private JFrame frame;
protected static final Color DEFAULT_COLOR = Color.YELLOW;
protected static final Color HIGHLIGHT_COLOR = Color.YELLOW.darker();
protected DataCollection<Item> collection; // To hold our items
protected DataCollection<Item> sortedCollection;
protected Item selected;
private final int COLLECTION_SIZE = 10, // Maximum number of items
MAXIMUM_ITEM_VALUE = 16; // Maximum value of an item
private int firstItemXCoord;
protected int firstItemYCoord;
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable() {
public void run()
{
try
{
DataOrganizer window = new DataOrganizer();
window.frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public DataOrganizer()
{
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize()
{
/*
* Create and set-up the JFrame.
*/
frame = new JFrame();
frame.setPreferredSize(new Dimension(550, 450));
frame.setBounds(100, 100, 600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setVisible(true);
/*
* Create btnRandom and add an action listener
* that calls randomAction().
*/
JButton btnRandom = new JButton("Random");
btnRandom.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
randomAction();
}
});
btnRandom.setBounds(6, 18, 117, 29);
frame.getContentPane().add(btnRandom);
/*
* Create btnMaximum and add an action listener
* that calls maximumAction().
*/
JButton btnMaximum = new JButton("Maximum");
btnMaximum.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
maximumAction();
}
});
btnMaximum.setBounds(6, 59, 117, 29);
frame.getContentPane().add(btnMaximum);
/*
* Create btnMinimum and add an action listener
* that calls minimumAction().
*/
JButton btnMinimum = new JButton("Minimum");
btnMinimum.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
minimumAction();
}
});
btnMinimum.setBounds(6, 100, 117, 29);
frame.getContentPane().add(btnMinimum);
/*
* Create btnRemove and add an action listener
* that calls removeAction().
*/
JButton btnRemove = new JButton("Remove");
btnRemove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
removeAction();
}
});
btnRemove.setBounds(6, 141, 117, 29);
frame.getContentPane().add(btnRemove);
/*
* Create btnSort and add an action listener
* that calls sort().
*/
JButton btnSort = new JButton("Sort");
btnSort.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sort();
}
});
btnSort.setBounds(6, 182, 117, 29);
frame.getContentPane().add(btnSort);
/*
* Create the JPanel, set the bounds,
* and add it to the frame.
*/
JPanel panel = new JPanel();
panel.setBounds(135, 0, 465, 378);
frame.getContentPane().add(panel);
//Initialize the unsorted collection
collection = new DataCollection<Item>(COLLECTION_SIZE);
//Initialize the sorted collection
sortedCollection = new DataCollection<Item>(COLLECTION_SIZE);
repaint();
}
public void paintComponent(Graphics panel) {
super.paintComponent(panel);
panel.drawString("Hello World", 140 , 10);
/*
* Display the Items when instantiated.
*/
if (collection != null)
{
Item item;
int xCoord = firstItemXCoord;
/*
* Reset the selected item to the start of the collection,
* ensuring it always starts at the first node
*/
collection.reset();
//While there is another node in the collection, loop through.
while (collection.hasNext())
{
/*
* Set the item to the selected node.
* Which since reset() was called on the
* collection, it should be the first node
* in the collection. Then set the next node in the
* collection a the selected one.
*/
item = collection.next();
/*
* Call the paint method in the item class
*/
item.paint(panel, xCoord, firstItemXCoord);
xCoord += Item.OVERALL_WIDTH;
}
}
/*
* Display the Items when instantiated.
*/
if (sortedCollection != null)
{
Item item;
int xCoord = firstItemXCoord + 200;
/*
* Reset the selected item to the start of the collection,
* ensuring it always starts at the first node
*/
sortedCollection.reset();
//While there is another node in the collection, loop through.
while (sortedCollection.hasNext())
{
/*
* Set the item to the selected node.
* Which since reset() was called on the
* sortedCollection, it should be the first node
* in the collection. Then set the next node in the
* collection a the selected one.
*/
item = sortedCollection.next();
/*
* Call the paint method in the item class
*/
item.paint(panel, xCoord, firstItemXCoord);
xCoord += Item.OVERALL_WIDTH;
}
}
}
//
// Random
//
public void randomAction()
{
collection.clear(); // We restart with nothing,
// then we add random items,
for (int i = 1; i <= COLLECTION_SIZE; i++)
{
collection.add(new Item((int) (1 + MAXIMUM_ITEM_VALUE
* Math.random()), DEFAULT_COLOR));
}
selected = null; // We make sure nothing is selected
}
公共类DataOrganizer扩展了JPanel
{
私有JFrame;
受保护的静态最终颜色默认值\u Color=Color.YELLOW;
受保护的静态最终颜色高亮显示_Color=Color.YELLOW.darker();
受保护的数据收集;//保存我们的项目
受保护的数据收集分类收集;
选择受保护的项目;
私有最终整数集合\u SIZE=10,//最大项数
最大\u项\u值=16;//项的最大值
私有int firstItemXCoord;
受保护的int firstItemYCoord;
/**
*启动应用程序。
*/
公共静态void main(字符串[]args)
{
invokeLater(新的Runnable(){
公开募捐
{
尝试
{
DataOrganizer窗口=新建DataOrganizer();
window.frame.setVisible(true);
}
捕获(例外e)
{
e、 printStackTrace();
}
}
});
}
/**
*创建应用程序。
*/
公共数据管理器()
{
初始化();
}
/**
*初始化框架的内容。
*/
私有void初始化()
{
/*
*创建并设置JFrame。
*/
frame=新的JFrame();
frame.setPreferredSize(新尺寸(550450));
框架.立根(100100600400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setVisible(true);
/*
*创建btnRandom并添加操作侦听器
*这将调用randomAction()。
*/
JButton btnRandom=新JButton(“随机”);
添加ActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
随机作用();
}
});
Btnram.setBounds(6,18,117,29);
frame.getContentPane().add(btnRandom);
/*
*创建btnMaximum并添加操作侦听器
*这将调用maximumAction()。
*/
JButton btnMaximum=新JButton(“最大”);
btnMaximum.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
最大作用();
}
});
Btn最大立根数(6,59,117,29);
frame.getContentPane().add(btnMaximum);
/*
*创建btnMinimum并添加操作侦听器
*它调用minimumAction()。
*/
JButton BTN最小值=新JButton(“最小值”);
btnMinimum.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
最小动作();
}
});
Btn最小立根(61001117,29);
frame.getContentPane().add(btnMinimum);
/*
*创建btnRemove并添加操作侦听器
*这将调用removeAction()。
*/
JButton btnRemove=新JButton(“删除”);
btnRemove.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
removeAction();
}
});
b.后退(6141117,29);
frame.getContentPane().add(btnRemove);
/*
*创建btnSort并添加操作侦听器
*这调用sort()。
*/
JButton btnSort=新JButton(“排序”);
addActionListener(新的ActionListener(){
已执行的公共无效操作(操作事件e){
排序();
}
});
b传感器立根(6,182,117,29);
frame.getContentPane().add(bSensor);
/*
*创建JPanel,设置边界,
*并将其添加到框架中。
*/
JPanel面板=新的JPanel();
面板立根(135,0465378);
frame.getContentPane().add(面板);
//初始化未排序的集合
集合=新数据集合(集合大小);
//初始化已排序的集合
sortedCollection=新数据收集(收集大小);
重新油漆();
}
公共组件(图形面板){
超级油漆组件(面板);
面板。拉丝(“你好,世界”,140,10);
/*
*实例化时显示项目。
*/
if(集合!=null)
{
项目;
int xCoord=firstItemXCoord;
/*
*将所选项目重置为集合的开始,
*确保它始终从第一个节点开始
*/
collection.reset();
//而