我试图制作一个可以让你选择所需图像数量的程序,然后会询问你想要使用的文件的名称。然后它将以适当的方式在屏幕上以网格打印它们。
现在我知道这段代码的很多部分,比如宽度和高度,行和列,以及其他的东西都是错的,我打算在我弄清楚这一点之后修复它们。不管我如何改变它,它都不会让我使用paintComponent。我想把它留在我的主要课堂上,这还有可能吗?很多时候我把它放在一个单独的图形类中,但是引入我对该类的输入是令人恼火的。
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Core extends JPanel{
public static void main(String[] args){
Scanner r = new Scanner(System.in);
System.out.println("How many selections will you have? 1, 2 or 4? ");
if(r.next().contains("1") | r.next().contains("2") | r.next().contains("4")){
String selections = r.next();
int number = Integer.parseInt(selections);
ArrayList<String> images = new ArrayList();
for(int j = 1; j <= number; j++){
System.out.println("Your options are boo, bae, skinny, bro...");
System.out.println("Name of image " + j + "? ");
if(r.next().contains("boo") | r.next().contains("bae") | r.next().contains("skinny") | r.next().contains("bro"))
images.add(r.next());
else{
System.out.println("Im sorry that was an improper input...");
System.out.println("Next time, remember, you can only input boo, bae, skinny, or bro.");
r.close();
}
}
JFrame theGUI = new JFrame();
theGUI.setTitle("Random Images");
theGUI.setSize(number * 100, number * 100);//ratio of 1:100
theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = theGUI.getContentPane();
pane.setLayout(new GridLayout(number, number));
for(int j = 1; j <= number; j++){
Color backColor = Color.white;
Image image = new ImageIcon((images.get(j)) + ".jpg").getImage();
public paintComponent(Graphics g){//error here(paintComponent cannot be resolved to a variable)(Illegal modifier for 'g')
super.paintComponent(Graphics g);//and error here (cannot use super)(Graphics cannot be resokved to a variable)
g.drawImage(image, 0, 0, 100, 100, null);}
ColorPanel panelz = new ColorPanel(backColor);
pane.add(panelz);
}
theGUI.setVisible(true);
}//end of the checker for the 1, 2 and 4.
else {
System.out.println("I'm sorry that was an improper input.");
System.out.println("Keep in mind your inputs may only be 1, 2 or 4.");
}
}
}
在评论中发布代码的道歉,我是新来的。但是,当我喜欢你说,我为变量声明id做了什么?
public class Core extends JPanel{
public static Image image;
public void paintComponent(g){//insert VariableDeclaratorID error
super.paintComponent(g);
g.drawImage(image, 0, 0, 100, 100, null);
}
public static void main(String[] args){
答案 0 :(得分:5)
paintComponent
方法的返回类型super.paintComponent(Graphics g);
,只需将正确类型的对象的实例传递给它,super.paintComponent(g);
在继续之前,请先看看:
拿这个......
public paintComponent(Graphics g){ error here(paintComponent cannot be resolved to a variable)(Illegal modifier for 'g')
super.paintComponent(Graphics g); and error here (cannot use super)(Graphics cannot be resokved to a variable)
g.drawImage(image, 0, 0, 100, 100, null);}
从for-loop
开始,将其作为Core
班级的方法......
@Override
public void paintComponent(Graphics g) {
// ^--- Need this...
super.paintComponent(g);
// ^--- Don't need Graphics, just an instance of it...
}
main
不是一个类,它是一种方法,一种Core
的方法;)
例如......
public class Core extends JPanel{
public static void main(String[] args){
//..
pane.setLayout(new GridLayout(number, number));
for(int j = 1; j <= number; j++){
Color backColor = Color.white;
Image image = new ImageIcon((images.get(j)) + ".jpg").getImage();
// This be bad...
//public paintComponent(Graphics g){//error here(paintComponent cannot be resolved to a variable)(Illegal modifier for 'g')
// super.paintComponent(Graphics g);//and error here (cannot use super)(Graphics cannot be resokved to a variable)
//
//g.drawImage(image, 0, 0, 100, 100, null);}
ColorPanel panelz = new ColorPanel(backColor);
pane.add(panelz);
}
theGUI.setVisible(true);
//...
}
// This be better...
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw the List of images here how ever you want...
}
}
<强>更新... 强>
好的,如果我理解你的评论,你想要做的是创建一个JPanel
的实例,但为每个图像提供它的paintComponent
方法的自定义实现...
for(int j = 1; j <= number; j++){
Color backColor = Color.white;
final Image image = new ImageIcon((images.get(j)) + ".jpg").getImage();
// ^--- This is important
panel.add(new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(image.getWidth(), image.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
});
}