Kot*_*ite -2 java graphics user-interface swing
我正在尝试制作一个程序,让你选择你想要的图像数量,然后它将询问你想要使用的文件的名称是什么.然后它将以适当的方式在屏幕上以网格打印它们.
现在我知道这段代码的很多部分,比如宽度和高度,行和列,以及其他的东西都是错的,我打算在我弄清楚这一点后修复它们.无论我如何改变它,都不会让我使用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.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
在评论中发布代码的道歉,我是新来的.但是,当我喜欢你说,我为变量声明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){
Run Code Online (Sandbox Code Playgroud)
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);}
Run Code Online (Sandbox Code Playgroud)
出你的,for-loop并使它成为你Core班级的一种方法......
@Override
public void paintComponent(Graphics g) {
// ^--- Need this...
super.paintComponent(g);
// ^--- Don't need Graphics, just an instance of it...
}
Run Code Online (Sandbox Code Playgroud)
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...
}
}
Run Code Online (Sandbox Code Playgroud)
更新...
好的,如果我理解你的评论,你想要做的是创建一个实例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);
}
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
62 次 |
| 最近记录: |