Reputation: 305
I'm having trouble getting the program to paint an image on. I'm not sure how it's meant to work, but I've defined paintComponent(Graphics g), but the program doesnt seem to be detecting it. Am i doing something wrong?
Here's the code I have btw
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.Image;
//add actionlistener event later
public class ZodiacKiller extends JPanel{
private JFrame mainframe;
private Choice month;
private Choice day;
private Label desc;
private Label dayDesc;
private int monthSelect;
private int daySelect;
private Button calc;
private Image[] images;
private String[] zodiacNames = {"aquarius","pisces","aries","taurus","gemini","cancer","leo","virgo","libra","scorpio","saggitarus","capricorn"};
private Image imageSelect;
public ZodiacKiller(){
guiProgram();
}
public static void main(String[] args){
ZodiacKiller ted = new ZodiacKiller();
}
public void guiProgram(){
mainframe = new JFrame("Zodiac Days 2016");
mainframe.setSize(485,300);
mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainframe.setBackground(Color.WHITE);
//figure out how layouts work in general
mainframe.setLayout(new FlowLayout());
Container c = mainframe.getContentPane();
desc = new Label("Enter your month");
mainframe.add(desc);
month = new Choice();
month.add("1");
month.add("2");
month.add("3");
month.add("4");
month.add("5");
month.add("6");
month.add("7");
month.add("8");
month.add("9");
month.add("10");
month.add("11");
month.add("12");
mainframe.add(month);
month.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie){
monthSelect = Integer.parseInt(month.getSelectedItem());
dayGenerator(monthSelect);
buttonGenerator();
}
});
dayDesc = new Label("Enter your day");
mainframe.add(dayDesc);
mainframe.setVisible(true);
}
public void buttonGenerator(){
mainframe.setVisible(false);
if(calc != null){
mainframe.remove(calc);
calc = null;
oldChoiceDeleter();
}
calc = new Button("Calculate!");
mainframe.add(calc);
calc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
daySelect = Integer.parseInt(day.getSelectedItem());
Zodiac teddie = new Zodiac(daySelect, monthSelect);
imageMount(teddie.zodiacCalc());
repaint();
System.out.println("everything works!");
}
});
mainframe.setVisible(true);
}
public void dayGenerator(int monthSelect){
mainframe.setVisible(false);
if(day != null){
mainframe.remove(day);
day = null;
oldChoiceDeleter();
}
day = new Choice();
day.add("1");
day.add("2");
day.add("3");
day.add("4");
day.add("5");
day.add("6");
day.add("7");
day.add("8");
day.add("9");
day.add("10");
day.add("11");
day.add("12");
day.add("13");
day.add("14");
day.add("15");
day.add("16");
day.add("17");
day.add("18");
day.add("19");
day.add("20");
day.add("21");
day.add("22");
day.add("23");
day.add("24");
day.add("25");
day.add("26");
day.add("27");
day.add("28");
day.add("29");
switch(monthSelect){
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
day.add("30");
day.add("31");
case 4: case 6: case 9:
day.add("30");
default:
}
mainframe.add(day);
mainframe.setVisible(true);
}
//start here
public void imageMount(int output){
images = new Image[12];
for (int i = 1; i<=12;i++){
Image temp = (new ImageIcon("img/no" + i + ".jpeg")).getImage();
images[i-1] = temp;
}
imageSelect = images[output - 1];
System.out.println(zodiacNames[output-1] + "\n" + output);
//make sure the image selection process works.
}
public static void oldChoiceDeleter(){
return;
}
public static void cmdPrompt(){
Scanner kb = new Scanner(System.in);
System.out.println("Day?");
int day = kb.nextInt();
System.out.println("Month? (1 ~ 12)");
int month = kb.nextInt();
Zodiac tedCruz = new Zodiac(day, month);
System.out.println(tedCruz.zodiacCalc());
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(imageSelect, 50, 50, null);
System.out.println("I went here!");
}
}
Upvotes: 0
Views: 885
Reputation: 2804
A JFrame
is not the best place to draw an image.
According to this question and answer, you might want to consider one of the following approaches:
JLabel
with an icon. JPanel
containing an instance of JCustomComponent
, which is a class that extends JComponent
and overrides the paintComponent()
method to draw the desired image.Upvotes: 1