paintcomponent

paintComponent not working

不想你离开。 提交于 2019-12-08 04:39:10
问题 this may be a silly question but How do i call the paintComponent? Its not displaying the object at all. its within the, public class Ball extends JPanel implements Runnable. public class Balls { public static void main(String[] args) { new Balls(); } public Balls() { EventQueue.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame("Balls!"); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); frame.add(new ballAdder()); frame.setSize(1000, 1000); frame.setVisible(true);

Java - JPanel with background image AND normal functionality

痞子三分冷 提交于 2019-12-08 03:43:38
问题 I've been looking around and working on making a JPanel able to have an image background. I know there is a ton of information reguarding this ( example 1 and example 2 ). Neither of these are exactly what I'm looking for. What I am looking for, is a way to just add a function or the functionality to set a background image on a JPanel while maintaining everything else. The problem that I have right now is that I have a few classes that extend JPanel and when I change them to extend my new

How to make star shape in Java?

半世苍凉 提交于 2019-12-07 04:14:05
问题 I'm trying to make some shapes with Java. I created two rectangles with two different colors but I want to create a star shape and I can't find useful source to help me doing this. Here is my code: import java.awt.*; import javax.swing.*; public class shapes extends JPanel{ @Override public void paintComponent(Graphics GPHCS){ super.paintComponent(GPHCS); GPHCS.setColor(Color.BLUE); GPHCS.fillRect(25,25,100,30); GPHCS.setColor(Color.GRAY); GPHCS.fillRect(25,65,100,30); GPHCS.setColor(new

How can I set the stroke color at runtime in JAVA?

孤街浪徒 提交于 2019-12-06 22:18:26
I'm trying to create an interface where the user can change the color of a line to mark what is background or what is foreground of an image. So far I have coded this: private class ImageLine extends JComponent { java.awt.Point p1,p2; BufferedImage show; ArrayList <Shape> shapes = new ArrayList<Shape>(); int flag = 0; Color color = Color.ORANGE; public ImageLine(BufferedImage img) { show = img; setPreferredSize(new Dimension(img.getWidth(), img.getHeight())); this.addMouseListener ( new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { p1 = e.getPoint(); } @Override public

Paint foreign component (i.e. belonging to a different frame's content pane) in another component

本秂侑毒 提交于 2019-12-06 14:06:44
问题 Hallo all. I want to paint a foreign component (i.e. belonging to a different frame's content pane), lets call it frame B, inside a component in frame A. The problem is that when I paint the component it is painted also in the content pane of the frame A, also it flickers or all gets ugly when the frame is re-sized (i.e. painted few times inside the component, some blue squares are appearing, etc.). The issue becomes more visible if I try to, for example, scale or translate the foreign

Drawing with paintComponent in Applet Upon Event

佐手、 提交于 2019-12-06 09:43:51
I'm trying to create an applet that will produce as many ovals as the number specified within a textbox. The textbox appears, but upon hitting enter, my paintComponent does not draw. Thank you in advance. import java.awt.*; import java.awt.event.*; import javax.swing.*; import net.miginfocom.layout.*; import net.miginfocom.swing.MigLayout; import java.awt.geom.*; public class OvalDrawer extends JApplet { private JLabel numberL; private JTextField numberTF; private NumHandler numHandler; public static final int WIDTH = 500; public static final int HEIGHT = 500; //Create Layout public void init(

How to make the background gradient of a JPanel

不羁岁月 提交于 2019-12-05 14:42:21
I want to know how to make background gradient which is in another JPanel. Many articles found in internet,but all of them had demostrated how to overide the paintComponent() of the JPanel not how to do for a jPanel which is inside it. I use Netbeans IDE. I created a new JPanel class and could overide its paintComponent(). I have another jpanel on it (dragged & dropped on to the parent JPanel). I want to make its background gradient. Here is how I tried for parent. It worked. How can I overide this for child jpanel ? public void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g;

How to make star shape in Java?

流过昼夜 提交于 2019-12-05 09:09:57
I'm trying to make some shapes with Java. I created two rectangles with two different colors but I want to create a star shape and I can't find useful source to help me doing this. Here is my code: import java.awt.*; import javax.swing.*; public class shapes extends JPanel{ @Override public void paintComponent(Graphics GPHCS){ super.paintComponent(GPHCS); GPHCS.setColor(Color.BLUE); GPHCS.fillRect(25,25,100,30); GPHCS.setColor(Color.GRAY); GPHCS.fillRect(25,65,100,30); GPHCS.setColor(new Color(190,81,215)); GPHCS.drawString("This is my text", 25, 120); } } You could try using a polygon and

How do you draw shapes on a JPanel, which is within another JPanel?

寵の児 提交于 2019-12-05 07:12:48
问题 I'm currently trying to draw shapes on a JPanel , which is within another JPanel , within a JFrame . I've searched Google and Youtube and found out how to draw shapes within a JFrame that has one panel, but have found nothing which can help me with what I'm doing. (maybe I'm not seeing something). Code I've seen so far: public class GameScreen { public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.red); g.drawRect(100, 10, 30, 40); } public static void main

Simulating rain

本小妞迷上赌 提交于 2019-12-05 01:30:38
I am making a game in java and I want to create a simulation of a cloud that is pouring rain. The cloud is supposed to move to the right while raining. Moving the cloud is no problem. It's the rain that I am struggling with. What I was thinking of doing was with a timer to draw a rectangle, thats supposed to look like falling rain at a random x value inside of the cloud. And then add 1 to the y value of the drop each 100 millisecond. But I don't want to create 100 different rectangles, x variables and y variables for each rain drop. Any idea how I can accomplish this? Suggestions appreciated!