Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
747 views
in Technique[技术] by (71.8m points)

swing - Can't add Graphics into JPanel in Java

I'm writing the UI for the pet project I've been doing and I'm experimenting with java Graphics, drawing lines, shapes, and stuff. And, I've been trying all day to insert a simple line in a Jpanel but still haven't figured out what went wrong.

package thuake;


import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.Paint;
import java.awt.Polygon;
import java.awt.geom.Line2D;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Main extends JFrame{
     static Dimension DEFAULT_SIZE = new Dimension(530, 320);
    static JFrame Frame1 = new JFrame();
    static JScrollPane spanel = new JScrollPane();
     static JPanel Panel1 = new JPanel();
     static MenuBar menu = new MenuBar();
     static Menu menusub1 = new Menu("Open");
    public static void main(String[] args)
    {   

        start();


    }
    public static void start (){

        Frame1.setLayout(new FlowLayout(FlowLayout.CENTER,5,10));
spanel.add(new draw());
        Frame1.add(spanel);
        spanel.setBorder(BorderFactory.createLineBorder(Color.black));
        spanel.setPreferredSize(new Dimension(500, 500));
         Frame1.add(new JButton("ad"));
         Frame1.add(new JButton("ad"));
         Frame1.add(new JButton("ad"));
         Frame1.add(new draw());
        Frame1.setMenuBar(menu);
        menu.add(menusub1);
        Frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Frame1.pack();
        spanel.setVisible(true);
        Frame1.setVisible(true);     
        System.out.println();
    }
    static class draw extends Component {

        public void paint(Graphics g) {

            Graphics2D line = (Graphics2D)g;
            line.drawLine(0, 0, 120, 120);

        }

          }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is a basic framework you should follow when you are doing graphics.

Key points are:

  1. Don't extend JFrame, use a separate instance.
  2. Override paintComponent() and not paint()
  3. Start the process in the EDT. Do all swing stuff in the EDT.
  4. Do not paint outside the EDT or with a graphics context that you retrieved yourself. Always use the one from paintComponent()
  5. Set the width and height (dimension) for the panel and not the JFrame. The reason being that those dimensions on the JFrame include the thick borders. For the JPanel you get the full width.

At some point you can modify this and add additional components to the JFrame and/or JPanel. For now this should provde a basis for experimenting.

You should also read up on this and anything I omitted shown below. Check out the Java Tutorials for more on information on graphics and painting.

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Template extends JPanel  {
   final static int    height = 500;
   final static int    width  = 500;
   final static String title  = "title";
   JFrame              frame  = new JFrame(title);

   public static void main(String[] args) {
      // start on the EDT
      SwingUtilities.invokeLater(() -> new Template().start());
   }

   public Template() {
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.add(this); // add the panel
      setPreferredSize(new Dimension(width, height));
      frame.pack();
      // center on screen
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public void paintComponent(Graphics g) {
      super.paintComponent(g); // always do this
      Graphics2D g2d = (Graphics2D) g.create();

      // Optional.  It averages the edges of a figure to give a smoothing effect
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);

      // do something here.
      g2d.setColor(Color.red);
      g2d.fillRect(200,200,100,100);

      g2d.dispose();

   }

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...