Java 将图形(g)绘制到jpanel中
我试图在下面的java小程序中绘制一些带有随机整数的条形图,但是覆盖有一个错误我是java新手我试图在代码中绘制两个区域draw方法在最底部另一个是第二个面板区域 我的目标是什么 尺寸x,y不是坐标 -总尺寸=200250 -位置gui=200,50 -图像=140150 -precip=100,20 -温度=20100Java 将图形(g)绘制到jpanel中,java,swing,graphics,graphics2d,panels,Java,Swing,Graphics,Graphics2d,Panels,我试图在下面的java小程序中绘制一些带有随机整数的条形图,但是覆盖有一个错误我是java新手我试图在代码中绘制两个区域draw方法在最底部另一个是第二个面板区域 我的目标是什么 尺寸x,y不是坐标 -总尺寸=200250 -位置gui=200,50 -图像=140150 -precip=100,20 -温度=20100 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.util.*;
public class Final2 extends JFrame
{
//Panels
JPanel locationGui = new JPanel ();
JPanel temp = new JPanel ();
JPanel percip = new JPanel ();
JPanel image = new JPanel ();
//Location gui components
JButton Enter, Exit;
JTextField location;
JLabel city;
JRadioButton time;
JComboBox Seasons;
//bar # genertor
Random rand = new Random ();
int P = rand.nextInt (100) + 1; //Random Precipitation
int H = rand.nextInt (50) + 1; //Random Heat
public Final2 ()
{
init ();
}
public void init ()
{
Font font = new Font ("impact", Font.PLAIN, 20);
//________________________________________________new panel____________________
locationGui.setBackground (Color.RED);
JLabel guiLabel = new JLabel ("");
guiLabel.setFont (font);
Enter = new JButton ("Enter");
Exit = new JButton ("exit");
city = new JLabel ("What city?");
location = new JTextField (20); //location entry field
Seasons = new JComboBox ();
Seasons.addItem ("Summer");
Seasons.addItem ("Fall");
Seasons.addItem ("Winter");
Seasons.addItem ("Spring");
time = new JRadioButton ("check if night?");
locationGui.add (city);
locationGui.add (location);
locationGui.add (Seasons);
locationGui.add (time);
locationGui.add (guiLabel);
//________________________________________________new panel____________________
temp.setBackground (Color.BLUE);
temp.setLayout (new GridBagLayout ());
JLabel tempLabel = new JLabel ("Temp");
tempLabel.setFont (font);
temp.add (tempLabel);
//________________________________________________new panel____________________
percip.setBackground (Color.GREEN);
JLabel pLabel = new DrawingPanel (); //where it should be
percip.add (pLabel);
//________________________________________________new panel____________________
image.setBackground (Color.ORANGE);
image.setLayout (new GridBagLayout ());
JLabel imageLabel = new JLabel ("Image");
imageLabel.setFont (font);
image.add (imageLabel);
Container contentPane = getContentPane ();
contentPane.setLayout (new BorderLayout ());
contentPane.add (locationGui, BorderLayout.NORTH);
contentPane.add (temp, BorderLayout.EAST);
contentPane.add (percip, BorderLayout.SOUTH);
contentPane.add (image, BorderLayout.CENTER);
setContentPane (contentPane);
setDefaultCloseOperation (EXIT_ON_CLOSE);
setSize (400, 400);
setLocationRelativeTo (null);
setVisible (true);
}
public static void main (String[] args)
{
SwingUtilities.invokeLater (new Runnable ()
{
public void run ()
{
new Final2 ();
}
}
);
}
private class DrawingPanel extends javax.swing.JPanel {//area i have issues with atm
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
drawPercip(g);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 400);
}
}
public void drawPercip (Graphics g)
{
//Precipitation Bar
g.setColor (Color.black);
g.drawRect (40, 170, 100, 20); //outline of bar
g.setColor (Color.blue);
g.fillRect (40 + 1, 170 + 4, P, 14); //indicator bar (+4 puts space beetween outline bar)
}
public void drawTemp (Graphics g)
{
//Temparature Bar
g.setColor (Color.red);
g.fillRect (170 + 4, 50, 14, 100); //Covers in
g.setColor (Color.black);
g.drawRect (170, 50, 20, 100); //outline of bar
g.setColor (Color.white);
g.fillRect (170 + 4, 50 + 1, 16, 100 - H); //indicator bar (+4 puts space beetween outline bar)
}
}
我当前的错误(4)发生在@override的这段代码中。提供的唯一消息是“非法令牌”和“意外符号被忽略”idk为什么它不提供更多信息
private class DrawingPanel extends javax.swing.JPanel {//area i have issues with atm
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
drawPercip(g);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 400);
}
首先我没有看到小程序 其次,
DrawingPanel
不是JLabel
JLabel pLabel = new DrawingPanel(); //where it should be
将pLable
类型更改为JComponent
或JPanel
或DrawingPanel
,具体取决于您希望从每个类中访问的内容
第三,你不应该依赖神奇的数字
public void drawPercip(Graphics g) {
//Precipitation Bar
g.setColor(Color.black);
g.drawRect(40, 170, 100, 20); //outline of bar
g.setColor(Color.blue);
g.fillRect(40 + 1, 170 + 4, P, 14); //indicator bar (+4 puts space beetween outline bar)
}
public void drawTemp(Graphics g) {
//Temparature Bar
g.setColor(Color.red);
g.fillRect(170 + 4, 50, 14, 100); //Covers in
g.setColor(Color.black);
g.drawRect(170, 50, 20, 100); //outline of bar
g.setColor(Color.white);
g.fillRect(170 + 4, 50 + 1, 16, 100 - H); //indicator bar (+4 puts space beetween outline bar)
}
无法保证面板的高度和宽度与您认为的相同。使用getWidth
和getHeight
方法
第四,如果你想拥有更多的绘图面板
,你应该
int P = rand.nextInt(100) + 1; //Random Precipitation
int H = rand.nextInt(50) + 1; //Random Heat
绘图面板的一部分
你也应该抽出时间通读如果你有错误,你应该把它们贴出来。请注意,
paintComponent
应该受到保护,但这不应该带来问题。我是一个业余爱好者,所以我知道受保护的意思,错误是“非法令牌”和“忽略意外符号”。我使用Ready java作为编译器1)不要解释错误消息。而是将整个消息作为问题的编辑发布。在代码中指出错误发生的位置。这不是解释,而是@override上的legitamate消息在@override上显示非法标记,rest on override“Ready”Java编译器支持哪一级别的API支持。它可能不支持注释…我不太明白你在说什么,但我添加了更多的信息,围绕着最终的布局理念和每个项目我想要的尺寸,你不明白什么部分?1)你说要将plabel更改为DrawingPanel,它已经是2)我从未听说过“神奇数字”@CurtisSpence 1。否,pLabel
已定义,明确为JLabel
,JLabel pLabel=new DrawingPanel()//如果它应该是
,JLabel
和DrawingPanel
是两种不同类型的对象,它们不能相互分配。2.您无法保证绘图面板的大小。布局管理器可能需要为布局中的其他组件提供更多或更少的空间,从而更改绘图面板的大小,这意味着当绘制时,它将无法正确绘制。因此JLabel pLabel=new DrawingPanel();是否实际为DrawingPanel plabel=新DrawingPanel();