自定义图形 - Java Swing [英] Custom graph - Java Swing
问题描述
我试图用Java创建一个自定义的动态直方图类型条形图。我搜索了很多,但coudn't找到一种方法来实现这一点。
我知道 JFreeChart 库,但它不符合我的需求。这就是JFreeChart直方图的样子:
但是我想要的是一个只有X轴的折射率直方图。
这张photoshopped图片会让你更容易理解。
JFrame将有固定的边界。如你所见,应该没有Y轴。酒吧的高度应根据这些值自动调整。
请帮我建立这个!
一个可怜的男人的直方图:
< img src =https://i.stack.imgur.com/uwX5A.pngalt =在这里输入图片描述>
import java.awt。*;
import java.util.List;
import java.util.ArrayList;
import javax.swing。*;
import javax.swing.border。*;
public class HistogramPanel extends JPanel
{
private int histogramHeight = 200;
private int barWidth = 50;
private int barGap = 10;
私人JPanel barPanel;
private JPanel labelPanel;
私人列表< Bar> bars = new ArrayList< Bar>();
$ b $ public HistogramPanel()
{
setBorder(new EmptyBorder(10,10,10,10));
setLayout(new BorderLayout());
barPanel = new JPanel(new GridLayout(1,0,barGap,0));
Border outer =新的MatteBorder(1,1,1,Color.BLACK);
内部边界=新的EmptyBorder(10,10,0,10);
边框复合=新的CompoundBorder(outer,inner);
barPanel.setBorder(复合);
labelPanel = new JPanel(new GridLayout(1,0,barGap,0));
labelPanel.setBorder(新的EmptyBorder(5,10,0,10));
add(barPanel,BorderLayout.CENTER);
add(labelPanel,BorderLayout.PAGE_END);
$ b $ public void addHistogramColumn(String label,int value,Color color)
{
Bar bar = new Bar(label,value,color);
bars.add(bar);
}
public void layoutHistogram()
{
barPanel.removeAll();
labelPanel.removeAll();
int maxValue = 0; $ bar
$ b maxValue = Math.max(maxValue,bar.getValue()); (bar bar:bars)
{
JLabel label = new JLabel(bar.getValue()+);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setHorizontalAlignment(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.TOP);
label.setVerticalAlignment(JLabel.BOTTOM);
int barHeight =(bar.getValue()* histogramHeight)/ maxValue;
图标图标=新ColorIcon(bar.getColor(),barWidth,barHeight);
label.setIcon(icon);
barPanel.add(label);
JLabel barLabel = new JLabel(bar.getLabel());
barLabel.setHorizontalAlignment(JLabel.CENTER);
labelPanel.add(barLabel);
}
}
私人班级Bar
{
私人字符串标签;
private int value;
私人颜色的颜色;
public Bar(字符串标签,int值,颜色)
{
this.label = label;
this.value = value;
this.color = color;
}
public String getLabel()
{
return label;
}
public int getValue()
{
返回值;
}
public Color getColor()
{
return color;
私有类ColorIcon实现图标
{
private int shadow = 3;
私人颜色的颜色;
private int width;
private int height;
$ b $ public ColorIcon(Color color,int width,int height)
{
this.color = color;
this.width = width;
this.height = height;
}
public int getIconWidth()
{
return width;
}
public int getIconHeight()
{
return height;
public void paintIcon(Component c,Graphics g,int x,int y)
{
g.setColor(color);
g.fillRect(x,y,width - shadow,height);
g.setColor(Color.GRAY);
g.fillRect(x + width - shadow,y + shadow,shadow,height - shadow);
private static void createAndShowGUI()
{
HistogramPanel panel = new HistogramPanel();
panel.addHistogramColumn(A,350,Color.RED);
panel.addHistogramColumn(B,690,Color.YELLOW);
panel.addHistogramColumn(C,510,Color.BLUE);
panel.addHistogramColumn(D,570,Color.ORANGE);
panel.addHistogramColumn(E,180,Color.MAGENTA);
panel.addHistogramColumn(F,504,Color.CYAN);
panel.layoutHistogram();
JFrame frame = new JFrame(Histogram Panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setLocationByPlatform(true);
frame.pack();
frame.setVisible(true);
public static void main(String [] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
I'm trying to create a custom dynamic histogram type bar graph in Java. I've searched a lot but coudn't find a way to achieve this.
I'm aware of the JFreeChart library but it doesn't meet my needs. This is what the JFreeChart histogram looks like :
But what I want is a dyanamic Histogram with just X-axis.
This photoshopped image will make it easier to understand.
The JFrame will have fixed boundaries. As you can see, there should be no Y-axis. The bars' height should adjust automatically based on the values.
Please help me build this! Thanks in advance.
A poor man's histogram:
import java.awt.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.*;
public class HistogramPanel extends JPanel
{
private int histogramHeight = 200;
private int barWidth = 50;
private int barGap = 10;
private JPanel barPanel;
private JPanel labelPanel;
private List<Bar> bars = new ArrayList<Bar>();
public HistogramPanel()
{
setBorder( new EmptyBorder(10, 10, 10, 10) );
setLayout( new BorderLayout() );
barPanel = new JPanel( new GridLayout(1, 0, barGap, 0) );
Border outer = new MatteBorder(1, 1, 1, 1, Color.BLACK);
Border inner = new EmptyBorder(10, 10, 0, 10);
Border compound = new CompoundBorder(outer, inner);
barPanel.setBorder( compound );
labelPanel = new JPanel( new GridLayout(1, 0, barGap, 0) );
labelPanel.setBorder( new EmptyBorder(5, 10, 0, 10) );
add(barPanel, BorderLayout.CENTER);
add(labelPanel, BorderLayout.PAGE_END);
}
public void addHistogramColumn(String label, int value, Color color)
{
Bar bar = new Bar(label, value, color);
bars.add( bar );
}
public void layoutHistogram()
{
barPanel.removeAll();
labelPanel.removeAll();
int maxValue = 0;
for (Bar bar: bars)
maxValue = Math.max(maxValue, bar.getValue());
for (Bar bar: bars)
{
JLabel label = new JLabel(bar.getValue() + "");
label.setHorizontalTextPosition(JLabel.CENTER);
label.setHorizontalAlignment(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.TOP);
label.setVerticalAlignment(JLabel.BOTTOM);
int barHeight = (bar.getValue() * histogramHeight) / maxValue;
Icon icon = new ColorIcon(bar.getColor(), barWidth, barHeight);
label.setIcon( icon );
barPanel.add( label );
JLabel barLabel = new JLabel( bar.getLabel() );
barLabel.setHorizontalAlignment(JLabel.CENTER);
labelPanel.add( barLabel );
}
}
private class Bar
{
private String label;
private int value;
private Color color;
public Bar(String label, int value, Color color)
{
this.label = label;
this.value = value;
this.color = color;
}
public String getLabel()
{
return label;
}
public int getValue()
{
return value;
}
public Color getColor()
{
return color;
}
}
private class ColorIcon implements Icon
{
private int shadow = 3;
private Color color;
private int width;
private int height;
public ColorIcon(Color color, int width, int height)
{
this.color = color;
this.width = width;
this.height = height;
}
public int getIconWidth()
{
return width;
}
public int getIconHeight()
{
return height;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
g.setColor(color);
g.fillRect(x, y, width - shadow, height);
g.setColor(Color.GRAY);
g.fillRect(x + width - shadow, y + shadow, shadow, height - shadow);
}
}
private static void createAndShowGUI()
{
HistogramPanel panel = new HistogramPanel();
panel.addHistogramColumn("A", 350, Color.RED);
panel.addHistogramColumn("B", 690, Color.YELLOW);
panel.addHistogramColumn("C", 510, Color.BLUE);
panel.addHistogramColumn("D", 570, Color.ORANGE);
panel.addHistogramColumn("E", 180, Color.MAGENTA);
panel.addHistogramColumn("F", 504, Color.CYAN);
panel.layoutHistogram();
JFrame frame = new JFrame("Histogram Panel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( panel );
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
这篇关于自定义图形 - Java Swing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!