0

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!");
  }
}
Brian Lee
  • 305
  • 4
  • 14
  • Where do you add the `JPanel` to the user interface (eg mainframe)? – copeg Nov 03 '16 at 20:37
  • Start by reading the [Swing tutorial](http://docs.oracle.com/javase/tutorial/uiswing/TOC.html) for Swing basics. Your code is a combination of AWT and Swing components. Don't use AWT components in a Swing application. Swing components start with "J". There is no need for custom painting. Just use a JLabel with an Icon. Check out the section on `How to Use Icons` from the tutorial. – camickr Nov 03 '16 at 22:11
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Nov 04 '16 at 05:43
  • 3) Don't mix Swing and AWT components without good reason. There is no good reason to do so in this case. 4) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. 5) Learn how to use loops! – Andrew Thompson Nov 04 '16 at 05:47
  • Dear @BrianLee. Looks like you're new on SO. There is no need to append all the code from your file in the question. Just write the code where you want us to help. :) – Arpit Porwal Nov 04 '16 at 10:45

1 Answers1

1

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:

  • Use a JLabel with an icon.
  • Use a JPanel containing an instance of JCustomComponent, which is a class that extends JComponent and overrides the paintComponent() method to draw the desired image.
Community
  • 1
  • 1
marcelovca90
  • 2,673
  • 3
  • 27
  • 34