hi i'm triyng to do something like this example
but i'm always getting the cross or in top-west or it doesn't appear and don't know why. I try to see the borderLayout and some stackoverflow explanations examples but didn´t find anything related. can someone explain to me what i'm doing wrong and why please?
public Base() {
super("Menu");
JPanel p = createPanel();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
setSize(screen.width, screen.height);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
add(p);
}
private JPanel createPanel() {
JPanel P = new JPanel() {
public void paint(Graphics g) {
super.paint(g);
Graphic gr = new Graphicimpl();
g.drawLine(gr.PositionX(-25.0), gr.PositionY(0.0), gr.PositionX(25.0), gr.PositionY(0.0));
g.drawLine(gr.PositionX(0.0), gr.PositionY(25.0), gr.PositionX(0.0), gr.PositionY(-25.0));
}
};
//P.setSize(50, 50);
//P.setBorder(BorderFactory.createEmptyBorder(300, 300, 300, 300));
return P;
}
}
public class Graphicimpl implements Graphic{
int FACTOr_ESCALACION = 10;
public int PositionX(Double x) {
return (int) ((x * FACTOr_ESCALACION) + 320);
}
#Override
public int PositionY(Double y) {
return (int) ( - (y * FACTOr_ESCALACION ) +240);
}
}
public interface Graphic {
int PositionX(Double x);
int PositionY(Double y);
}
public class Main {
public static void main(String args[]) {
Base base=new Base();
}
}
The location of the cross depends on the coordinates you have inserted. Since you are inserting them manually. You can test the program with different coordinates until the cross shows where you want it to.
Related
I have 2 Java classes, Game and BaseComponent.
I wanted to draw multiple components in my JFrame, but that doesn't work, since it will only display the last added component.
I think the solution is to add an JPanel, but that still doesn't quite work for me, since no object are being drawn, not even one.
Game:
public class Game
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(300, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jpanel = new JPanel();
JComponent component = new BaseComponent(0,0);
JComponent component2 = new BaseComponent(1,1);
jpanel.add(component);
jpanel.add(component2);
frame.add(jpanel);
frame.setVisible(true);
}
}
BaseComponent:
public class BaseComponent extends JComponent
{
private int x, y;
BaseComponent(int i, int y) {
this.x = i;
this.y = y;
}
#Override
public Dimension getPreferredSize() {
return new Dimension((x * 50) + 50, (y * 50) + 50);
}
#Override
protected void paintComponent(Graphics g)
{
drawBaseComponent(g, x, y);
}
void drawBaseComponent(Graphics g, int x, int y)
{
g.setColor(Color.GREEN);
g.fillRect(x*50, y*50, 50, 50);
}
}
As you can see, this code adds one component to the the panel, which is being added to the JFrame, but it's completely empty;
EDIT:
When using prefered size, the application draws two green boxes but the position is not right, I expected them in the place of the two red boxes.
JPanel uses a FlowLayout by default. This will attempt to size any components to the preferredSize, which is, by default 0x0, which is why, your component "appears" empty.
You will need to supply sizing hints, via getPreferredSize, in order to allow the layout management API to determine the best way to layout your component, for example
public class BaseComponent extends JComponent
{
private int x, y;
BaseComponent(int i, int i0) {
this.x = i;
this.y = i0;
}
#Override
public Dimension getPreferredSize() {
return new Dimension((x * 50) + 50, (y * 50) + 50);
}
#Override
protected void paintComponent(Graphics g)
{
drawBaseComponent(g, x, y);
}
void drawBaseComponent(Graphics g, int xLeft, int yTop)
{
g.setColor(Color.GREEN);
g.fillRect(x*50, y*50, 50, 50);
}
}
EDIT: First question solved, see Roberto Attias 's answer and maybe read the comment . Still there's the second issue.
I have to do a small 2D game in Java and I want to stick to AWT as much as possible, ( so no SWING nor Java2D unless absolutely necessary).
I have a window and I can draw an image on it but there's two issues.
First I can't draw more than one Image. In fact with some of my test when in debug I can see that my program will draw my two images only to delete them and re-draw the first image.
Second, that first image which is re-drawn is not at the coordinate it should ( its slightly on the left and on below )
For now I have something like that:
public class AwtManager {
private Frame frame;
private Panel panel;
public AwtManager(){
frame = new Frame("a");
frame.setSize(800,600);
frame.setLocation(100, 100);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEnvent){
System.exit(0);
}
});
panel = new Panel();
// here I tried to setBounds to solve my second issue, it didn't work
//panel.setBounds(0, 0, 800, 600);
frame.add(panel);
frame.setVisible(true);
}
This part open my windows and works quite nicely but my second issue seems to be caused by the borders of my frame / panel so there might be some changement to do here.
public void showMytwoImagesFFS(ArrayList<ImageComponent> images){
for (int i = 0; i < images.size(); i++) {
panel.add(images.get(i);
//And this is where I'm lost
}
// or here maybe
frame.setVisible(true);
}
}
In this second part I tried every combination of Component.paint(g), Component.update(g), Component.repaint(), Component.setVisible(true) I could think of.
My object ImageComponent is simply this:
public class ImageComponent extends Component {
private static final long serialVersionUID = 1L;
private BufferedImage img;
private int x;
private int y;
public ImageComponent(String path,int x, int y){
try{
img = ImageIO.read(new File(path));
this.x = x;
this.y = y;
}
catch (IOException e){
e.printStackTrace();
}
}
This function getPreferredSize() disturbs me like hell, it should return the preferred size of the ImageComponent but apparently I have to put the size of my frame/ panel otherwise it won't work.
public Dimension getPreferredSize(){
return new Dimension(800,600);
}
And finally my paint, update, repaint:
public void paint(Graphics g){
g.drawImage(img, x, y,null);
}
public void update(Graphics g){
super.update(g);
}
public void repaint(){
this.getGraphics().drawImage(img, x, y, null);
}
}
I highly doubt that those three look like what they should but I find the documents on the subject very hard to understand.
So pleas, could you help me with those issues, and by the way if you know how Component.setVisible(boolean) works i would like to know because in debug mod, this function made me loose some hair.
EDIT:
Here's a screenshot of my window knowing that I asked for two red square (there are Images not Rectangle), one a 0,0, the other at 200, 200.
EDIT 2:
Here's a fully runnable code (i think):
import java.awt.*;
import java.util.*;
import java.awt.event.*;
public class AwtManager {
private Frame frame;
private Panel panel;
public static void main(String args[]) {
new AwtManager();
ArrayList<ImageComponent> images = new ArrayList<>();
images.add(new ImageComponent("myimage.jpg", 0, 0));
images.add(new ImageComponent("myimage.jpg", 200, 200));
showMytwoImagesFFS(images);
}
public AwtManager(){
frame = new Frame("a");
frame.setSize(800,600);
frame.setLocation(100, 100);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEnvent){
System.exit(0);
}
});
panel = new Panel();
frame.add(panel);
frame.setVisible(true);
}
public void showMytwoImagesFFS(ArrayList<ImageComponent> images){
for (int i = 0; i < images.size(); i++) {
panel.add(images.get(i));
}
frame.setVisible(true);
}
}
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
public class ImageComponent extends Component {
private static final long serialVersionUID = 1L;
private BufferedImage img;
private int x;
private int y;
public ImageComponent(String path,int x, int y){
try{
img = ImageIO.read(new File(path));
this.x = x;
this.y = y;
}
catch (IOException e){
e.printStackTrace();
}
}
public Dimension getPreferredSize(){
return new Dimension(800,600);
}
public void paint(Graphics g){
g.drawImage(img, x, y,null);
}
public void update(Graphics g){
super.update(g);
}
public void repaint(){
this.getGraphics().drawImage(img, x, y, null);
}
}
Just a quick thought, but do you know the visibility of each of your images in that ArrayList? I.e.
public void showMytwoImagesFFS(ArrayList<ImageComponent> images){
for (int i = 0; i < images.size(); i++) {
images.get(i).setVisible(true);//is the visibility actually true?
panel.add(images.get(i);
}
// or here maybe
frame.setVisible(true);
}
Also, do you have screenshots of how your program looks at the moment? With gui problems, I find they're easier to solve if I can see what's going on.
This is your code, slightly modified to run. In the future, please post fully runnable examples.
import java.awt.*;
import java.util.*;
import java.awt.event.*;
public class AwtManager {
private Frame frame;
private Panel panel;
public static void main(String args[]) {
new AwtManager();
}
public AwtManager(){
frame = new Frame("a");
frame.setSize(800,600);
frame.setLocation(100, 100);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEnvent){
System.exit(0);
}
});
panel = new Panel();
// here I tried to setBounds to solve my second issue, it didn't work
//panel.setBounds(0, 0, 800, 600);
frame.add(panel);
ArrayList<ImageComponent> images = new ArrayList<>();
images.add(new ImageComponent("myimage.jpg", 0, 0));
showMytwoImagesFFS(images);
frame.setVisible(true);
}
public void showMytwoImagesFFS(ArrayList<ImageComponent> images){
for (int i = 0; i < images.size(); i++) {
panel.add(images.get(i));
}
}
}
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
public class ImageComponent extends Component {
private static final long serialVersionUID = 1L;
private BufferedImage img;
private int x;
private int y;
public ImageComponent(String path,int x, int y){
try{
img = ImageIO.read(new File(path));
this.x = x;
this.y = y;
}
catch (IOException e){
e.printStackTrace();
}
}
public Dimension getPreferredSize(){
return new Dimension(800,600);
}
public void paint(Graphics g){
g.drawImage(img, x, y,null);
}
public void update(Graphics g){
super.update(g);
}
public void repaint(){
this.getGraphics().drawImage(img, x, y, null);
}
}
I have a problem, when i call repaint() on JDialog, I see nothing on the screen, but when i move the JDialog by my self, I see what i wanted to paint.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class dude extends JFrame {
private static int cnt = 0;
public dude() {
super("ff");
makeFrame();
}
public void makeFrame() {
new Dialog(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setSize(400, 400);
setVisible(true);
}
private class Dialog extends JDialog {
public Dialog(JFrame frame) {
super(frame, "ff", true);
makeFrame();
}
public void makeFrame() {
getContentPane().addMouseListener(new M(this));
setDefaultCloseOperation(HIDE_ON_CLOSE);
pack();
setLocation(200, 200);
setSize(400, 400);
setVisible(true);
}
private class M extends MouseAdapter {
private JDialog dialog;
public M(JDialog dialog) {
this.dialog = dialog;
}
public void mouseClicked(MouseEvent e) {
P p = new P(e.getX(), e.getY());
p.repaint();
dialog.add(p);
}
private class P extends JPanel {
private int x, y;
public P(int x, int y) {
this.x = x;
this.y = y;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.drawOval(x, y, 10, 10);
}
/*public void paint(Graphics g)
{
g.setColor(Color.black);
g.drawOval(x,y,10,10);
}*/
}
}
}
}
That's weirdest piece of code I've seen for a while, but. You're immeditate problem is with you mouseClicked event...
Replace your p.repaint call with a call to the dialogs revalidate method.
P p = new P(e.getX(), e.getY());
dialog.add(p);
dialog.revalidate();
Your repaint method would have done nothing any way, it was being called before you panel was realized (connected to the screen)
Seems like you need to look into the coding style you adhering to. Though, leave that for latter part, simply add this method to your M Class
public void setValues(int x, int y)
{
this.x = x;
this.y = y;
repaint();
}
And make p an Instance Variable of your Dialog Class. And inside your mouseClicked() method, simply call this method. And remove the constructor part, since you initializing a new JPanel for each drawing which I guess is not good in any sense. When you simply can draw the new thingy on the same JPanel
I want to see all the Points one after another but I see only able to see 1
point. What shold I change to see all the Points ?
In the System.out you can see 10 times "set" and then 2 times
"paintComponent". what should I change that after each time set is
called it change the "paintComponente" ?
==================================================================================
public class exampe extends JPanel
{
int x;
int y;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.fillOval(x-2,y-2,4,4);
System.out.println("paintComponent");
}
public void set(int X, int Y)
{
x = X;
y = Y;
System.out.println("set");
super.repaint();
}
public static void main(String args[])
{
int e=1;
JFrame frame = new JFrame("TEST");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
exampe ex= new exampe();
JScrollPane scroll = new JScrollPane(ex);
frame.getContentPane().add(scroll);
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
for(int i=0; i< 10; ++i)
ex.set(e+i,e+i);
}
}
*SIMPLE EXPLANATION AS TO WHY YOU COULD ONLY SEE THE LAST UPDATE : *
A quote taken from Filthy Rich Clients by Chet Haase and Romain Guy
It is important to note that repaint requests get “coalesced,” or combined.
So, for example, if you request a repaint and there is already one on the
queue that has not yet been serviced, then the second request is ignored
because your request for a repaint will already be fulfilled by the earlier
request. This behavior is particularly helpful in situations where many
repaint requests are being generated, perhaps by very different situations
and components, and Swing should avoid processing redundant requests and
wasting effort.
Try your hands on this, and ask what is not clear to you :
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class PointsExample
{
private CustomPanel contentPane;
private Timer timer;
private int x = 1;
private int y = 1;
private ActionListener timerAction = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
contentPane.set(x, y);
x++;
y++;
if (x == 450)
timer.stop();
}
};
/*
* This is just JFrame, that we be
* using as the Base for our Application.
* Though here we are calling our
* JPanel (CustomPanel), whose
* paintComponent(...) method, we had
* override.
*/
private void createAndDisplayGUI()
{
JFrame frame = new JFrame("Locate Mouse Position");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPane = new CustomPanel();
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
timer = new Timer(100, timerAction);
timer.start();
}
public static void main(String\u005B\u005D args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new PointsExample().createAndDisplayGUI();
}
});
}
}
class CustomPanel extends JComponent
{
private int x;
private int y;
public void set(int a, int b)
{
x = a;
y = b;
repaint();
}
#Override
public Dimension getPreferredSize()
{
return (new Dimension(500, 500));
}
#Override
public void paintComponent(Graphics g)
{
g.clearRect(0, 0, getWidth(), getHeight());
Graphics2D g2 =(Graphics2D) g;
g2.fillOval(x, y, 4, 4);
}
}
Here is the code, that will allow you to have a look at your points while iterating inside a for loop, though this approach is highly discouraged, for many cons associated with it. Though try your hands on this instead of calling repaint() call paintImmediately(int ...) or paintImmediately(Rectangle rect)
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class PointsExample
{
private CustomPanel contentPane;
private Timer timer;
private int x = 1;
private int y = 1;
/*
* This is just JFrame, that we be
* using as the Base for our Application.
* Though here we are calling our
* JPanel (CustomPanel), whose
* paintComponent(...) method, we had
* override.
*/
private void createAndDisplayGUI()
{
JFrame frame = new JFrame("Locate Mouse Position");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPane = new CustomPanel();
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
for (int i = 0; i < 500; i++)
{
contentPane.set(x, y);
x++;
y++;
if (x == 450)
break;
}
}
public static void main(String\u005B\u005D args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new PointsExample().createAndDisplayGUI();
}
});
}
}
class CustomPanel extends JComponent
{
private int x;
private int y;
public void set(int a, int b)
{
x = a;
y = b;
paintImmediately(0, 0, getWidth(), getHeight());
}
#Override
public Dimension getPreferredSize()
{
return (new Dimension(500, 500));
}
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.fillOval(x, y, 4, 4);
}
}
1: first line of paintComponent() should be your super.paintComponent()
2: why are you calling super.repaint(), make it simply repaint()
Your Drow should be like this.
public class drow extends JPanel {
...........
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 =(Graphics2D) g;
}
public void set_list(LinkedList <point> p){
Points =p;
repaint();
}
try with this.
i hope this is simply a structure, your paintComponent() isn't drawing anything.
EDIT
public void set_list(LinkedList <point> p){
Points =p;
System.out.println("set_ist");// 1:First this line will be displayed then..
repaint();//2: Then this is called, which in turn calls your `paintComponent()`
}
Now when your paintComponent() is called it has
system.out.println("paintComponent");
//3: so now this will be displayed.
Where is the problem here?
EDIT- SWING TIMER
Your code was ok, but the function processing is way faster than GUI updation, thats why you were unable to see the changes in front of you. The way you were doing, of calling thread.sleep() between function calls to slow down it's call, was not a good approach. For any timing thing's in swing, use swing timer, i changed your code for swing timer.
Using Swing Timer:
public class exampe extends JPanel implements ActionListener {
int x;
int y;
int temp = 0;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.fillOval(x - 2, y - 2, 4, 4);
}
public void set(int X, int Y) {
x = X;
y = Y;
}
public static void main(String args[]) {
JFrame frame = new JFrame("TEST");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
exampe ex = new exampe();
JScrollPane scroll = new JScrollPane(ex);
frame.getContentPane().add(scroll);
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Timer PointTimer = new Timer(1000, ex);
PointTimer.setInitialDelay(1000);
PointTimer.start();
System.out.println("started");
}
#Override
public void actionPerformed(ActionEvent e) {
// set(rand.nextInt(350), rand.nextInt(350));
set(temp+10,temp+10);
temp=temp+2;
repaint();
}
}
I want to change the look of a JScrollBar.
I do this with overwriting/extending ScrollBarUI.
It´s no problem to change the outlook of the arrowbuttons by overwriting createIncreaseButton and createDecreaseButton.
I change the width of the track by overwriting paintThumb and paintTrack Methods.
It looks now like <----o----> (a very thin trackline and an oval thumb/knob).
PROBLEM:
The knob can't move till the very end:
What it does look like: <---o------>
What it should look like: <---------o>
I know this is because I made the oval not stretching (the original rectangle stretches with the width).
I'm totally clueless as were to change the computing of the thumb move so it can move until the end.
I would be very thankful for help.
Heres the code:
public class TestScrollBarMain extends JFrame {
public TestScrollBarMain() {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
JPanel p = new JPanel();
p.setPreferredSize(new Dimension(500, 500));
JScrollPane s = new JScrollPane(p);
MyScrollBar b = new MyScrollBar();
s.setVerticalScrollBar(b);
getContentPane().add(s);
setSize(100, 100);
setVisible(true);
}
public static void main(String[] args) {
new TestScrollBarMain();
}
public class MyScrollBarUI extends BasicScrollBarUI {
#Override
protected void paintThumb(final Graphics g, final JComponent c, final Rectangle thumbBounds) {
if (thumbBounds.isEmpty() || !this.scrollbar.isEnabled()) {
return;
}
g.translate(thumbBounds.x, thumbBounds.y);
g.setColor(this.thumbDarkShadowColor);
g.drawOval(2, 0, 14, 14);
g.setColor(this.thumbColor);
g.fillOval(2, 0, 14, 14);
g.setColor(this.thumbHighlightColor);
g.setColor(this.thumbLightShadowColor);
g.translate(-thumbBounds.x, -thumbBounds.y);
}
#Override
protected void paintTrack(final Graphics g, final JComponent c, final Rectangle trackBounds) {
g.setColor(Color.black);
g.fillRect(trackBounds.width / 2, trackBounds.y, 3, trackBounds.height);
if (this.trackHighlight == BasicScrollBarUI.DECREASE_HIGHLIGHT) {
this.paintDecreaseHighlight(g);
} else if (this.trackHighlight == BasicScrollBarUI.INCREASE_HIGHLIGHT) {
this.paintIncreaseHighlight(g);
}
}
}
public class MyScrollBar extends JScrollBar {
MyScrollBar() {
super();
setUI(new MyScrollBarUI());
}
}
}
Include this on your MyScrollBarUI code:
protected void setThumbBounds(int x, int y,int width,int height)
{
super.setThumbBounds(x, y, 14, 14);
}
protected Rectangle getThumbBounds()
{
return new Rectangle(super.getThumbBounds().x,super.getThumbBounds().y,14,14);
}
protected Dimension getMinimumThumbSize()
{
return new Dimension(14,14);
}
protected Dimension getMaximumThumbSize()
{
return new Dimension(14,14);
}